How to Fill Background Color Left to Right with CSS Hover Effects?

Linda Hamilton
Release: 2024-10-30 02:46:02
Original
557 people have browsed it

How to Fill Background Color Left to Right with CSS Hover Effects?

Filling Background Color Left to Right Using CSS

In CSS, you can create captivating hover effects by utilizing linear gradients and animating background positioning. This approach enables you to fill the background of an element from left to right with a new color upon hover.

Linear Gradient and Background Size

The key is to use a linear gradient background consisting of two colors and set the background size to be twice the element's width. This allows you to create a seamless transition between the two colors.

Background Positioning and Animation

Initially, position the background to the right to start with the leftmost color. Upon hover, change the background position to the left to reveal the rightmost color. To make the transition gradual, add the transition:all 2s ease; property.

Code Example

<code class="css">div {
    background: linear-gradient(to left, red 50%, blue 50%) right;
    background-size: 200% 100%;
    transition:all 2s ease;
}
div:hover {
    background-position: left;
}</code>
Copy after login

Customizing the Transition

To control the starting and ending points of the transition, adjust the percentages in the linear gradient. For instance, here's a configuration that transitions from 34% to 65% of the element's width:

<code class="css">background: linear-gradient(to left, red 34%, blue 65%) right;
background-size: 300% 100%;</code>
Copy after login

By implementing this technique, you can effortlessly create elegant and eye-catching hover effects that enhance the user experience of your web applications and enhance your designs' overall visual appeal.

The above is the detailed content of How to Fill Background Color Left to Right with CSS Hover Effects?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template