我有一个有渐变轮廓的div <div class="base-action-card"></div>.当处于:hover状态时,div会获得渐变背景.

transition似乎不起作用.我在MDN文档中看不出是什么原因导致了这一点.

我如何在div中淡入淡出渐变背景?

我try 将transition: background 0.3s;添加到.base-action-card类 Select 器中,希望浏览器在0.3s上为从whitelinear-gradient(180deg, #1FC1FA 0%, #4b67f2 100%);的转换设置关键帧.

不会渲染任何关键帧.

代码片段:

.base-action-card {
  padding: 1rem;
  position: relative;
  z-index: 0;
  height: 100%;
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: 10px;
  cursor: pointer;
  background: white;
  height: 200px;
  width: 200px;
  background: linear-gradient(180deg, white 0%, white 100%); /* Solid white gradient */
  transition: background 0.3s;

  &::before {
    content: "";
    position: absolute;
    z-index: -1;
    inset: 0;
    padding: 2px; /* Set the width of the "border" */
    border-radius: 10px;
    background: linear-gradient(180deg, #1FC1FA 0%, #4b67f2 100%);

    mask:
      linear-gradient(#fff 0 0) content-box,
      linear-gradient(#fff 0 0);
    mask-composite: exclude;
  }

  &:hover, &:active {
    background: linear-gradient(180deg, #1FC1FA 0%, #4b67f2 100%);
  }
}
<div class="base-action-card"></div>

推荐答案

CSS中的渐变被视为图像,而不是 colored颜色 ,不能直接在它们之间转换.相反,一种解决办法是在After伪元素上设置不透明度动画,为边框保留之前元素.就像这样.

我们还可以删除基本元素上重复的高度和宽度,以及重复的背景 colored颜色

.base-action-card {
  padding: 1rem;
  position: relative;
  z-index: 0;
  height: 200px; /* only one height definition is needed */
  width: 200px; /* only one width definition is needed */
  aspect-ratio: 16/9;
  border-radius: 10px;
  cursor: pointer;
  background: white;


  &::after {
    content: "";
    position: absolute;
    z-index: -1;
    inset: 0;
    padding: 2px; /* Set the width of the "border" */
    border-radius: 10px;
    background: linear-gradient(180deg, #1FC1FA 0%, #4b67f2 100%);
    opacity: 0; /* Start fully transparent */
    transition: opacity 0.3s; /* Transition the opacity */
    visibility: hidden; /* Start as hidden */
  }

  /* Keep your existing ::before for the gradient border */
  &::before {
    content: "";
    position: absolute;
    z-index: -2;
    inset: 0;
    padding: 2px; /* Set the width of the "border" */
    border-radius: 10px; /* Match the border-radius of the container */
    background: linear-gradient(180deg, #1FC1FA 0%, #4b67f2 100%);
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask-composite: exclude;
  }

  &:hover::after, &:active::after {
    opacity: 1; /* Fully visible on hover */
    visibility: visible; /* Make visible on hover */
  }
}
<div class="base-action-card"></div>

Html相关问答推荐

我如何 Select 外部html文件元素作为选项,并显示在一个div与jQuery?

创建具有圆锥渐变的水平CSS聚光灯

为什么这个高度为100%的页面会出现滚动条?

在css嵌套 Select 器中,尾随的`&;‘是什么意思?

如何在伪元素背景中定位多个CSS径向梯度

调整一组IMG的大小以适应容器DIV

背景可点击,而不是文本

我怎样才能在我的网站上制作花哨的角落

类型';联系人组件';上不存在属性';name FormControl';

为什么 Select 元素在带有数据绑定的Blazor上行为怪异?

我如何确保我的网格永远不会小于它的子网格

如何在小屏幕中制作水平滚动div

Tailwind CSS 忽略我的元素的填充

并排放置两个 div,同时 div2 环绕 div1

表单中如何分别禁用/启用多个提交按钮?

如何使容器的大小适合其 position:absolute; 子元素的大小

如何将两个平行 div 的页面内的表格居中?

删除按钮组件时 bootstrap col-auto 布局高度对齐中断

CSS 变量不适用于自定义属性回退值

如何使文本显示在页眉/页脚之外?