Home >Web Front-end >CSS Tutorial >How Can I Create a Sliding Background Color Animation on Hover with CSS?
Sliding Background Color Animation
You've encountered a challenge in creating a sliding background color animation on hover using CSS transitions. While you can achieve a fading effect, you're looking to make the background scroll up from the bottom.
Solution: Using a Background Image
To achieve the slide effect, CSS transitions alone are not sufficient. Instead, you'll need to create a background image, such as a gradient, and adjust its position over time. Here's how you can do it:
Code Example:
.box { width: 200px; height: 100px; background-size: 100% 200%; background-image: linear-gradient(to bottom, red 50%, black 50%); -webkit-transition: background-position 1s; -moz-transition: background-position 1s; transition: background-position 1s; } .box:hover { background-position: 0 -100%; }
HTML Markup:
<div class="box"></div>
Explanation:
Alternative Approach:
While the gradient image approach works well, you could also consider creating a separate element with the desired background and using CSS transitions to scroll it up. However, this method may involve additional markup and complexity.
The above is the detailed content of How Can I Create a Sliding Background Color Animation on Hover with CSS?. For more information, please follow other related articles on the PHP Chinese website!