使用带有渐变背景的CSS3过渡

我试图在悬停时用css过渡缩略图,这样在悬停时,背景渐变就会淡入。转换没有工作,但如果我简单地将其更改为rgba()值,它就可以工作。不支持渐变吗?我也尝试使用图像,它也不会过渡图像。

我知道这是可能的,因为在另一篇文章中有人做到了,但我不知道具体是怎么做到的。这里有一些CSS工作:

#container div a {
-webkit-transition: background 0.2s linear;
-moz-transition: background 0.2s linear;
-o-transition: background 0.2s linear;
transition: background 0.2s linear;
position: absolute;
width: 200px;
height: 150px;
border: 1px #000 solid;
margin: 30px;
z-index: 2
}


#container div a:hover {
background: -webkit-gradient(radial, 100 75, 100, 100 75, 0, from(rgba(0, 0, 0, .7)), to(rgba(0, 0, 0, .4)))
}
404852 次浏览

渐变还不支持过渡(尽管目前的规范说它们应该通过插值支持类似渐变到类似渐变的过渡)。

如果你想要一个背景渐变的渐隐效果,你必须在容器元素上设置不透明度,并“过渡”不透明度。

(已经有一些浏览器版本支持渐变过渡(例如IE10。我在2016年在IE中测试了梯度转换,当时它们似乎可以工作,但我的测试代码不再工作了。)

更新:2018年10月 带有无前缀新语法的渐变过渡[例如径向渐变(…)]现在确认在Microsoft Edge 17.17134上工作(再次?)我不知道这是什么时候加入的。仍然没有工作在最新的Firefox &Chrome / Windows 10.

更新:2021年12月 在最近的基于Chromium的浏览器中,使用@property的变通方法可以实现这一点(但在Firefox中不起作用)。请参见(并upvote) @mahozad的回答以下(或以上YMMV)

一个解决方法是转换背景位置,以产生渐变变化的效果: http://sapphion.com/2011/10/css3-gradient-transition-with-background-position/ < / p >

CSS3渐变过渡与背景位置

虽然你不能直接使用CSS的transition属性来动画渐变,但是你可以使用background-position属性来实现简单的渐变动画:

它的代码非常简单:

#DemoGradient{
background: -webkit-linear-gradient(#C7D3DC,#5B798E);
background: -moz-linear-gradient(#C7D3DC,#5B798E);
background: -o-linear-gradient(#C7D3DC,#5B798E);
background: linear-gradient(#C7D3DC,#5B798E);
  

-webkit-transition: background 1s ease-out;
-moz-transition: background 1s ease-out;
-o-transition: background 1s ease-out;
transition: background 1s ease-out;
  

background-size:1px 200px;
border-radius: 10px;
border: 1px solid #839DB0;
cursor:pointer;
width: 150px;
height: 100px;
}
#DemoGradient:Hover{
background-position:100px;
}  
<div id="DemoGradient"></div>  

如上所述。渐变目前不支持CSS过渡。但在某些情况下,你可以通过将其中一种颜色设置为透明来解决这个问题,这样其他一些包装元素的背景色就会穿透,并进行过渡。

你可以伪造渐变之间的过渡,在一些堆叠渐变的不透明度中使用过渡,正如这里的一些答案所描述的:

CSS3动画与梯度

你也可以转换位置,如下所述:

CSS3渐变过渡与背景位置

这里还有一些技巧:

动画CSS3渐变

解决方案是使用background-position来模拟梯度过渡。 这个解决方案是在几个月前的Twitter Bootstrap中使用的

更新

http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614 < a href = " http://codersblock.blogspot.fr/2013/12/gradient-animation-trick.html?showComment=1390287622614 " > < / >

这里有一个简单的例子:

链接状态

 .btn {
font-family: "Helvetica Neue", Arial, sans-serif;
font-size: 12px;
font-weight: 300;
position: relative;
display: inline-block;
text-decoration: none;
color: #fff;
padding: 20px 40px;
background-image: -moz-linear-gradient(top, #50abdf, #1f78aa);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#50abdf), to(#1f78aa));
background-image: -webkit-linear-gradient(top, #50abdf, #1f78aa);
background-image: -o-linear-gradient(top, #50abdf, #1f78aa);
background-image: linear-gradient(to bottom, #50abdf, #1f78aa);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff50abdf', endColorstr='#ff1f78aa', GradientType=0);
background-repeat: repeat-y;
background-size: 100% 90px;
background-position: 0 -30px;
-webkit-transition: all 0.2s linear;
-moz-transition: all 0.2s linear;
-o-transition: all 0.2s linear;
transition: all 0.2s linear;
}

悬停状态

.btn:hover {
background-position: 0 0;
}

在下面的例子中,一个锚标记有一个子标记和一个孙子标记。孙子有远背景梯度。近背景中的子元素是透明的,但是有渐变要过渡。在悬停时,子物体的不透明度在1秒内从0过渡到1。

下面是CSS:

.bkgrndfar {
position:absolute;
top:0;
left:0;
z-index:-2;
height:100%;
width:100%;
background:linear-gradient(#eee, #aaa);
}


.bkgrndnear {
position:absolute;
top:0;
left:0;
height:100%;
width:100%;
background:radial-gradient(at 50% 50%, blue 1%, aqua 100%);
opacity:0;
transition: opacity 1s;
}


a.menulnk {
position:relative;
text-decoration:none;
color:#333;
padding: 0 20px;
text-align:center;
line-height:27px;
float:left;
}


a.menulnk:hover {
color:#eee;
text-decoration:underline;
}


/* This transitions child opacity on parent hover */
a.menulnk:hover .bkgrndnear {
opacity:1;
}

这是HTML:

<a href="#" class="menulnk">Transgradient
<div class="bkgrndfar">
<div class="bkgrndnear">
</div>
</div>
</a>

以上仅在最新版本的Chrome中进行测试。这些是悬停前,悬停中途和完全过渡的悬停图像:

Before 半路 After .

尝试使用:before和:after (ie9+)

#wrapper{
width:400px;
height:400px;
margin:0 auto;
border: 1px #000 solid;
position:relative;}
#wrapper:after,
#wrapper:before{
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
content:'';
background: #1e5799;
background: -moz-linear-gradient(top, #1e5799 0%, #2989d8 50%, #207cca 51%, #7db9e8 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#1e5799), color-stop(50%,#2989d8), color-stop(51%,#207cca), color-stop(100%,#7db9e8));
background: -webkit-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
background: -o-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
background: -ms-linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
background: linear-gradient(to bottom, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
opacity:1;
z-index:-1;
-webkit-transition: all 2s ease-out;
-moz-transition: all 2s ease-out;
-ms-transition: all 2s ease-out;
-o-transition: all 2s ease-out;
transition: all 2s ease-out;
}
#wrapper:after{
opacity:0;
background: #87e0fd;
background: -moz-linear-gradient(top, #87e0fd 0%, #53cbf1 40%, #05abe0 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#87e0fd), color-stop(40%,#53cbf1), color-stop(100%,#05abe0));
background: -webkit-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
background: -o-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
background: -ms-linear-gradient(top, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
background: linear-gradient(to bottom, #87e0fd 0%,#53cbf1 40%,#05abe0 100%);
}
#wrapper:hover:before{opacity:0;}
#wrapper:hover:after{opacity:1;}

渐变过渡的部分解决方法是使用嵌入框阴影——你可以转换框阴影本身,也可以转换背景颜色——例如,如果你创建的嵌入框阴影与背景颜色相同,然后在背景颜色上使用过渡,它会产生一种错觉,即普通背景正在变为径向渐变

.button SPAN {
padding: 10px 30px;
border: 1px solid ##009CC5;


-moz-box-shadow: inset 0 0 20px 1px #00a7d1;
-webkit-box-shadow: inset 0 0 20px 1px#00a7d1;
box-shadow: inset 0 0 20px 1px #00a7d1;


background-color: #00a7d1;
-webkit-transition: background-color 0.5s linear;
-moz-transition: background-color 0.5s linear;
-o-transition: background-color 0.5s linear;
transition: background-color 0.5s linear;
}


.button SPAN:hover {
background-color: #00c5f7;
}

我在工作中使用这个:)IE6+ https://gist.github.com/GrzegorzPerko/7183390 < / p >

如果你使用一个文本元素,不要忘记<element class="ahover"><span>Text</span></a>

.ahover {
display: block;
/** text-indent: -999em; ** if u use only only img **/
position: relative;
}
.ahover:after {
content: "";
height: 100%;
left: 0;
opacity: 0;
position: absolute;
top: 0;
transition: all 0.5s ease 0s;
width: 100%;
z-index: 1;
}
.ahover:hover:after {
opacity: 1;
}
.ahover span {
display: block;
position: relative;
z-index: 2;
}

发布另一个视图也无妨,因为仍然没有一个官方的方法来做到这一点。写了一个轻量级的jQuery插件,你可以用它来定义背景径向渐变和过渡速度。这个基本的用法会让它淡入,用requestAnimationFrame优化(非常流畅):

$('#element').gradientFade({


duration: 2000,
from: '(20,20,20,1)',
to: '(120,120,120,0)'
});

< a href = " http://codepen.io/Shikkediel/pen/xbRaZz?编辑= 001 " rel = " nofollow”> http://codepen.io/Shikkediel/pen/xbRaZz?编辑= 001 < / >

保持原始背景和所有属性完整。也有高亮跟踪作为设置:

< a href = " http://codepen.io/Shikkediel/pen/VYRZZY?编辑= 001 " rel = " nofollow”> http://codepen.io/Shikkediel/pen/VYRZZY?编辑= 001 < / >

在codepend上发现了一个很好的黑客,可以修改opacity属性,但通过利用伪元素实现从一个渐变到另一个渐变。他所做的就是设置一个:after,这样当你改变实际元素的不透明度时,:after元素就会显示出来,这样它看起来就像一个渐变。我觉得分享一下会有用。

原始代码源:http://codepen.io/sashtown/pen/DfdHh

.
.button {
display: inline-block;
margin-top: 10%;
padding: 1em 2em;
font-size: 2em;
color: #fff;
font-family: arial, sans-serif;
text-decoration: none;
border-radius: 0.3em;
position: relative;
background-color: #ccc;
background-image: linear-gradient(to top, #6d8aa0, #8ba2b4);
-webkit-backface-visibility: hidden;
z-index: 1;
}
.button:after {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 0.3em;
background-image: linear-gradient(to top, #ca5f5e, #d68584);
transition: opacity 0.5s ease-out;
z-index: 2;
opacity: 0;
}
.button:hover:after {
opacity: 1;
}
.button span {
position: relative;
z-index: 3;
}
body {
text-align: center;
background: #ddd;
}
<a class="button" href="#"><span>BUTTON</span></a>

我知道这是一个老问题,但也许有人喜欢我在纯CSS解决方案的方式。从左到右渐变。

.contener{
width:300px;
height:200px;
background-size:cover;
border:solid 2px black;
}
.ed {
width: 0px;
height: 200px;
background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
position: relative;
opacity:0;
transition:width 20s, opacity 0.6s;
}


.contener:hover .ed{
width: 300px;
background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));
position: relative;
opacity:1;
transition:width 0.4s, opacity 1.1s;
transition-delay: width 2s;
    

animation-name: gradient-fade;
animation-duration: 1.1s;
-webkit-animation-name: gradient-fade; /* Chrome, Safari, Opera */
-webkit-animation-duration: 1.1s; /* Chrome, Safari, Opera */
}








/* ANIMATION */
@-webkit-keyframes gradient-fade {
0%   {background:linear-gradient(to right, rgba(0,0,255,0), rgba(255,0,0,0));}
2%  {background:linear-gradient(to right, rgba(0,0,255,0.01875), rgba(255,0,0,0));}
4%  {background:linear-gradient(to right, rgba(0,0,255,0.0375), rgba(255,0,0,0.0));}
6%  {background:linear-gradient(to right, rgba(0,0,255,0.05625), rgba(255,0,0,0.0));}
8% {background:linear-gradient(to right, rgba(0,0,255,0.075), rgba(255,0,0,0));}
10%  {background:linear-gradient(to right, rgba(0,0,255,0.09375), rgba(255,0,0,0));}
12%   {background:linear-gradient(to right, rgba(0,0,255,0.1125), rgba(255,0,0,0));}
14%  {background:linear-gradient(to right, rgba(0,0,255,0.13125), rgba(255,0,0,0));}
16%  {background:linear-gradient(to right, rgba(0,0,255,0.15), rgba(255,0,0,0));}
18%  {background:linear-gradient(to right, rgba(0,0,255,0.16875), rgba(255,0,0,0));}
20% {background:linear-gradient(to right, rgba(0,0,255,0.1875), rgba(255,0,0,0));}
22%  {background:linear-gradient(to right, rgba(0,0,255,0.20625), rgba(255,0,0,0.01875));}
24%   {background:linear-gradient(to right, rgba(0,0,255,0.225), rgba(255,0,0,0.0375));}
26%  {background:linear-gradient(to right, rgba(0,0,255,0.24375), rgba(255,0,0,0.05625));}
28%  {background:linear-gradient(to right, rgba(0,0,255,0.2625), rgba(255,0,0,0.075));}
30%  {background:linear-gradient(to right, rgba(0,0,255,0.28125), rgba(255,0,0,0.09375));}
32% {background:linear-gradient(to right, rgba(0,0,255,0.3), rgba(255,0,0,0.1125));}
34%  {background:linear-gradient(to right, rgba(0,0,255,0.31875), rgba(255,0,0,0.13125));}
36%   {background:linear-gradient(to right, rgba(0,0,255,0.3375), rgba(255,0,0,0.15));}
38%  {background:linear-gradient(to right, rgba(0,0,255,0.35625), rgba(255,0,0,0.16875));}
40%  {background:linear-gradient(to right, rgba(0,0,255,0.375), rgba(255,0,0,0.1875));}
42%  {background:linear-gradient(to right, rgba(0,0,255,0.39375), rgba(255,0,0,0.20625));}
44% {background:linear-gradient(to right, rgba(0,0,255,0.4125), rgba(255,0,0,0.225));}
46%  {background:linear-gradient(to right, rgba(0,0,255,0.43125),rgba(255,0,0,0.24375));}
48%   {background:linear-gradient(to right, rgba(0,0,255,0.45), rgba(255,0,0,0.2625));}
50%  {background:linear-gradient(to right, rgba(0,0,255,0.46875), rgba(255,0,0,0.28125));}
52%  {background:linear-gradient(to right, rgba(0,0,255,0.4875), rgba(255,0,0,0.3));}
54%   {background:linear-gradient(to right, rgba(0,0,255,0.50625), rgba(255,0,0,0.31875));}
56%  {background:linear-gradient(to right, rgba(0,0,255,0.525), rgba(255,0,0,0.3375));}
58%  {background:linear-gradient(to right, rgba(0,0,255,0.54375), rgba(255,0,0,0.35625));}
60%  {background:linear-gradient(to right, rgba(0,0,255,0.5625), rgba(255,0,0,0.375));}
62% {background:linear-gradient(to right, rgba(0,0,255,0.58125), rgba(255,0,0,0.39375));}
64%  {background:linear-gradient(to right,rgba(0,0,255,0.6), rgba(255,0,0,0.4125));}
66%   {background:linear-gradient(to right, rgba(0,0,255,0.61875), rgba(255,0,0,0.43125));}
68%  {background:linear-gradient(to right, rgba(0,0,255,0.6375), rgba(255,0,0,0.45));}
70%  {background:linear-gradient(to right, rgba(0,0,255,0.65625), rgba(255,0,0,0.46875));}
72%  {background:linear-gradient(to right, rgba(0,0,255,0.675), rgba(255,0,0,0.4875));}
74% {background:linear-gradient(to right, rgba(0,0,255,0.69375), rgba(255,0,0,0.50625));}
76%  {background:linear-gradient(to right, rgba(0,0,255,0.7125), rgba(255,0,0,0.525));}
78%   {background:linear-gradient(to right, rgba(0,0,255,0.73125),,rgba(255,0,0,0.54375));}
80%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.5625));}
82%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.58125));}
84%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.6));}
86% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.61875));}
88%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.6375));}
90%   {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.65625));}
92%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.675));}
94%  {background:linear-gradient(to right, rgba(0,0,255,0.75),rgba(255,0,0,0.69375));}
96%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.7125));}
98% {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.73125),);}
100%  {background:linear-gradient(to right, rgba(0,0,255,0.75), rgba(255,0,0,0.75));}
}
<div class="contener" style="">
<div class="ed"></div>
</div>

我想有一个div看起来像一个3D球体和过渡的颜色。 我发现渐变背景色还不能过渡。 我在元素前面放置了一个径向梯度背景(使用z-index)和一个过渡的固体背景

/* overlay */
z-index : 1;
background : radial-gradient( ellipse at 25% 25%, rgba( 255, 255, 255, 0 ) 0%, rgba( 0, 0, 0, 1 ) 100% );

然后下面的div.ball:

transition : all 1s cubic-bezier(0.25, 0.46, 0.45, 0.94);

然后改变div.ball的背景颜色,瞧!

< a href = " https://codepen。io/keldon/pen/dzPxZP" rel="nofollow noreferrer">https://codepen.io/keldon/pen/dzPxZP

基于你的问题中的css代码,我已经尝试代码如下,它为我工作(运行代码片段),请自己尝试:

#container div a {
display: inline-block;
margin-top: 10%;
padding: 1em 2em;
font-size: 2em;
color: #fff;
font-family: arial, sans-serif;
text-decoration: none;
border-radius: 0.3em;
position: relative;
background-color: #ccc;
background-image: linear-gradient(to top, #C0357E, #EE5840);
-webkit-backface-visibility: hidden;
z-index: 1;
}
     

#container div a:after {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 0.3em;
background-image: linear-gradient(to top, #6d8aa0, #343436);
transition: opacity 0.5s ease-out;
z-index: 2;
opacity: 0;
}
    

#container div a:hover:after {
opacity: 1;
}
#container div a span {
position: relative;
z-index: 3;
}
<div id="container"><div><a href="#"><span>Press Me</span></a></div></div>

Based on the css code in your question, I have try code as follows and it works for me, and please try by yourself :

    #container div a {
display: inline-block;
margin-top: 10%;
padding: 1em 2em;
font-size: 2em;
color: #fff;
font-family: arial, sans-serif;
text-decoration: none;
border-radius: 0.3em;
position: relative;
background-color: #ccc;
background-image: linear-gradient(to top, #C0357E, #EE5840);
-webkit-backface-visibility: hidden;
z-index: 1;
}


#container div a:after {
position: absolute;
content: '';
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 0.3em;
background-image: linear-gradient(to top, #6d8aa0, #343436);
transition: opacity 0.5s ease-out;
z-index: 2;
opacity: 0;
}


#container div a:hover:after {
opacity: 1;
}
#container div a span {
position: relative;
z-index: 3;
}

这对你有用吗? 根据您的需要更改颜色:)

不管怎样,这里有一个Sass mixin:

用法:

@include gradientAnimation(red, blue, .6s);

混合:

@mixin gradientAnimation( $start, $end, $transTime ){
background-size: 100%;
background-image: linear-gradient($start, $end);
position: relative;
z-index: 100;
&:before {
background-image: linear-gradient($end, $start);
content: "";
display: block;
height: 100%;
position: absolute;
top: 0; left: 0;
opacity: 0;
width: 100%;
z-index: -100;
transition: opacity $transTime;
}
&:hover {
&:before {
opacity: 1;
}
}
}

选自Dave Lunny在Medium上的这篇很棒的帖子:https://medium.com/@dave_lunny/animating-css-gradients-using-only-css-d2fd7671e759

2021:现在可以动画渐变了(还没有火狐Safari)

随着铬85边缘歌剧添加了对@ property规则的支持,现在我们可以在CSS中这样做:

@property --myColor1 {
syntax: '<color>';
initial-value: magenta;
inherits: false;
}


@property --myColor2 {
syntax: '<color>';
initial-value: green;
inherits: false;
}
其余的是常规CSS。
将变量设置为初始渐变颜色,并设置这些变量的过渡:

div {
/* Optional: change initial value of the variables */
/* --myColor1: #f64; --myColor2: brown; */


background: linear-gradient(var(--myColor1), var(--myColor2));
transition: --myColor1 3s, --myColor2 3s;
}

然后,在所需的规则上,为变量设置新的值:

div:hover {
--myColor1: #f00;
--myColor2: yellow;
}

@property --myColor1 {
syntax: '<color>';
initial-value: #0f0;
inherits: false;
}


@property --myColor2 {
syntax: '<color>';
initial-value: rgb(40, 190, 145);
inherits: false;
}


div {
width: 200px;
height: 100px;
background: linear-gradient(var(--myColor1), var(--myColor2));
transition: --myColor1 3s, --myColor2 3s;
}


div:hover {
--myColor1: red;
--myColor2: #E1AF2F;
}
<div>Hover over me</div>

See the full description and example here and refer here for @property specification.
The @property rule is part of the CSS Houdini technology. For more information refer here and here and see this video.

::之前, CSS伪元素可以很容易地做到这一点!

.element {
position: relative;
width: 200px;
height: 150px;
background-image: linear-gradient(45deg, blue, aqua);
z-index: 2;
}


.element::before {
position: absolute;
content: "";
inset: 0; /* same as { top: 0; right: 0; bottom: 0; left: 0; } */
background-image: linear-gradient(to bottom, red, orange);
z-index: 1;
opacity: 0;
transition: opacity 0.25s linear;
}


.element:hover::before {
opacity: 1;
}
<body>
<div class="element"></div>
</body>

你所要做的就是使用::之前伪元素,不透明度为零。

:徘徊上,将::之前的不透明度切换为1,如果你遵循几个简单的步骤,你应该可以让你的转换工作。

  1. 使用background-image设置元素的背景渐变 使用带有不透明度 0的::之前伪元素来设置下一个渐变
  2. :徘徊::中设置不透明度1
  3. 确保你的::之前有:
    • 绝对位置
    • 内容:“
    • a 低z - index而不是默认元素
    • 0 正确的(或简单地插图:0)
    • 过渡目标不透明度与您的偏好的时间间隔

就是这样!现在你应该可以用你喜欢的任何持续时间 / 延迟 / 定时功能来调整你的过渡了!

一个更简洁的解决方案是设置背景色并使用蒙版图像。

#container div a {
background-color: blue;
transition: background 0.2s linear;
width: 200px;
height: 150px;
mask-image: radial-gradient(circle at 50% 50%, rgba(0, 0, 0, .7), rgba(0, 0, 0, .4));
}


#container div a:hover {
background-color: red;
}

非常感谢

.element {
position: relative;
width: 200px;
height: 150px;
background-image: linear-gradient(45deg, blue, aqua);
z-index: 2;
}


.element::before {
position: absolute;
content: "";
inset: 0; /* same as { top: 0; right: 0; bottom: 0; left: 0; } */
background-image: linear-gradient(to bottom, red, orange);
z-index: 1;
opacity: 0;
transition: opacity 0.25s linear;
}


.element:hover::before {
opacity: 1;
}
<body>
<div class="element"></div>
</body>