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 do I change the styles of child elements when hovering over the parent when using SCSS?

There is a block with several nested elements:

<div class="block">
  <h1 class="block__title"></h1>
  <p class="block__text"></p>
  <a href="" class="block__link"></a>
</div>

I use this code, but it doesn’t work:

.block:hover {
   &__title {
     text-decoration: underline;  
   }
}

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 :

Your current code is being interpreted as .block:hover__title which will not produce your desired effect.

To get your child element reacting to a hover you’ll probably want to do something like:

.block {
  &:hover {
    .block__title {
      text-decoration: underline;
    }
  }
}

(compiles as .block:hover .block__title which will correctly affect the child element)

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