Home >Web Front-end >CSS Tutorial >How to Create a Sliding Background-Color Transition from the Bottom with CSS?

How to Create a Sliding Background-Color Transition from the Bottom with CSS?

Linda Hamilton
Linda HamiltonOriginal
2024-12-19 03:52:08449browse

How to Create a Sliding Background-Color Transition from the Bottom with CSS?

How to Transition Background-Color from the Bottom

When attempting to change the background color of an element on hover using CSS transitions, one may encounter the issue of the background fading in rather than sliding up. While it is possible to accomplish the fading effect using the provided code, a different approach is required to achieve the desired sliding animation.

One solution involves utilizing a background image or gradient, and progressively adjusting the background position. This method creates the illusion of the background sliding up from below:

.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%;
}
<div class="box"></div>

In this scenario, the element is given a background image with a linear gradient, creating the desired vertical transition. As the element is hovered over, the background position is shifted upwards, producing the effect of the background sliding up from the bottom.

The above is the detailed content of How to Create a Sliding Background-Color Transition from the Bottom 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