Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background from bottom to top on hover

How I can change the background-color on a:hover using a transition from the bottom to the top with a duration of 0.3s?

<ul>
  <li><a></a></li>
  <li><a></a></li>
  <li><a></a></li>
</ul>

Is that possible?

Thanks

like image 334
user1878413 Avatar asked Dec 18 '13 13:12

user1878413


People also ask

How can I change background of image on hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How do I change the background color on hovering?

To change the color when hovering in CSS, you will use the CSS selector called :hover . The :hover is a CSS pseudo-class that will select the HTML element when the user hovers over with the mouse. The hover selector will work on almost all HTML elements.


1 Answers

There is no way to (generically) apply a transition direction in CSS. However, we can work around that limitation by using a pseudo element (or other method like how this example uses gradients).

By using a pseudo element, which initially has a visible height of 0, we can transition the height from and to a desired direction when the link is hovered. It's best to use transform: scale for performance reasons, which means that we need to set our transform-origin to bottom center in this case to make sure it goes from bottom to top.

This approach is probably the most general, working with non-solid backgrounds and such, but does require a pseudo element or child element.

The CSS for that approach would be:

li {
    background: red;
}
a {
    position: relative;
    z-index: 1;
}
a::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    transform: scaleY(0);
    transform-origin: bottom center;
    background: blue;
    z-index: -1;
    transition: transform 0.3s;
}
a:hover::after {
    transform: scaleY(1);
}

Demo

like image 180
Zach Saucier Avatar answered Oct 06 '22 03:10

Zach Saucier