P粉2266425682023-08-24 11:27:23
One solution is to transform the background position to create a gradient change effect: http://sapphion.com/2011/10/css3-Gradient transition and 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>
P粉7879344762023-08-24 09:43:10
Gradients do not yet support transitions (although the current specification says they should support gradient-like to gradient-like transitions via interpolation.).
If you want a fade-in effect with a background gradient, you must set the opacity on the container element and "transition" the opacity.
(There are already some browser versions that support gradient transitions (e.g. IE10. I tested gradient transitions in IE in 2016 and they seemed to work at the time, but my test code no longer works.)
Updated: October 2018 Gradient transitions with new syntax without prefix [e.g. Radial-gradient(...)] are now confirmed to work (again?) on Microsoft Edge 17.17134. I don't know when this was added. Still not working on latest Firefox and Chrome / Windows 10.
Updated: December 2021 The @property workaround is now available in recent Chromium-based browsers (but does not work in Firefox). See (and vote for) @mahozad's answer below (or YMMV above).