Home >Web Front-end >CSS Tutorial >How Can I Create a Sliding Background Color Change on Hover with CSS?

How Can I Create a Sliding Background Color Change on Hover with CSS?

DDD
DDDOriginal
2024-11-27 00:04:11580browse

How Can I Create a Sliding Background Color Change on Hover with CSS?

Transforming Background Color with Sliding Animation

Seeking to alter the background hue of an element upon hover via CSS transitions, you face the challenge of achieving an upward scrolling effect. The current CSS code fades the background, but you desire a slick slide animation.

One approach is to harness a background image or gradient and meticulously adjust the background position. This technique enables you to slide the background from the bottom.

Consider the following example:

.box {
    width: 200px; height: 100px;
    background-size: 100% 200%;
    background-image: linear-gradient(to bottom, red 50%, black 50%);
    transition: background-position 1s;
}

.box:hover {
    background-position: 0 -100%;
}
<div class="box"></div>

With this approach, the background initially displays red in the upper half and black in the lower half. Upon hovering, the background slides up, smoothly transitioning the red color to the entire element's height.

The above is the detailed content of How Can I Create a Sliding Background Color Change on Hover with CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn