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

Animate part of title when mouse is over

I’m trying to animate a title composed of two part: "HARD" and "WARE", the second one has a more transparent color.

When the mouse is over the "HARD" word the colors are reversed correctly but when the mouse is over the "WARE" word this doesn’t happen, only one color changes.
I have already tried to use the combinators selector but there is no selector for the "previous siblings".

body {
  background-color: #171717;
}

#title {
  font-size: 10vmax;
  font-family: 'Open Sans', Calibri;
}

#hard {
    color: rgba(255, 255, 255, 1);
    transition: all 0.3s ease-in-out;
}

#hard:hover {
    color: rgba(255, 255, 255, 0.18);
}

#hard:hover + #ware {
    color: rgba(255, 255, 255, 1);
}

#ware {
    color: rgba(255, 255, 255, 0.18);
    transition: all 0.3s ease-in-out;
}

#ware:hover {
    color: rgba(255, 255, 255, 1);
}

#title span:last-child:hover + #title > span:first-child {
    color: rgba(255, 255, 255, 0.18);
}
<div id="title">
  <span id="hard">HARD</span>
  <span id="ware">WARE</span>
</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 :

Here’s a solution which uses the :hover state of the parent element in two rules which have combined selectors with the first and last child:

body {
  background-color: #171717;
}

#title {
  font-size: 10vmax;
  font-family: 'Open Sans', Calibri;
}

#hard {
  color: rgba(255, 255, 255, 1);
  transition: all 0.3s ease-in-out;
}

#ware {
  color: rgba(255, 255, 255, 0.18);
  transition: all 0.3s ease-in-out;
}

#title:hover span:first-child {
  color: rgba(255, 255, 255, 0.18);
}

#title:hover span:last-child {
  color: rgba(255, 255, 255, 1);
}
<div id="title">
  <span id="hard">HARD</span>
  <span id="ware">WARE</span>
</div>

Addition: Instead of first and last child you could also use the IDs in the selectors:

body {
  background-color: #171717;
}

#title {
  font-size: 10vmax;
  font-family: 'Open Sans', Calibri;
}

#hard {
  color: rgba(255, 255, 255, 1);
  transition: all 0.3s ease-in-out;
}

#ware {
  color: rgba(255, 255, 255, 0.18);
  transition: all 0.3s ease-in-out;
}

#title:hover #hard {
  color: rgba(255, 255, 255, 0.18);
}

#title:hover #ware {
  color: rgba(255, 255, 255, 1);
}
<div id="title">
  <span id="hard">HARD</span>
  <span id="ware">WARE</span>
</div>
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