Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to animate background-color on hovering from bottom to top and again from bottom to top

I am trying to animate background-color property on hovering from bottom to top and again from bottom to top when mouse is out. I have this pen, but it seems like width: 100% and height: 100% in ::after is calculated based on <p> tag, not <span>.

So how can I fix it? I want background-color animation just on underlined text (dolor), not for the whole paragraph.

div {
  display: flex;
  align-items: center;
  justify-content: center;
}

p {
  position: relative;
  font: 2em sans-serif;
}

.underlined {
  font-weight: bold;
  text-decoration: underline solid black 0.1em;
  transition: color 0.25s ease-out;
}
.underlined::after {
  content: "";
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%; /* 100% of <p>, not <span> */
  height: 100%; /* 100% of <p>, not <span> */
  background: black;
  z-index: -1;
  transform: scaleY(0);
  transform-origin: top;
  transition: transform 0.25s ease-out;
}

.underlined:hover {
  color: white;
}
.underlined:hover::after {
  transform: scaleY(1);
  transform-origin: bottom;
}
.underlined:hover::selection {
  color: black;
  background: white;
}
<div>
  <p>Lorem ipsum <span class="underlined">dolor</span> sit amet</p>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

add a position to <span> with class .underlined

.underlined {
    position: relative;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading