Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS

Cosimo Scarpa
Cosimo Scarpa
14,047 Points

How manage the selection of a specific class in CSS or SASS

Hi there, I would like select a specific class or ID inside my code but I have some issue. Basically what I'm trying to do is this:

  • hover .bg1
  • change the color of #eye

This is the HTML code

<div class="main">
  <div id="A" class="bg1"></div>
    <div id="B"class="bg2"></div>
    <div id="C" class="bg3"></div>
    <div id="D" class="bg4"></div>
    <div class="container">
      <div id="eye"></div>
   </div>
</div>

I'm using SCSS in my project.

.bg4 {
  &:hover .container {
    #eye {
      background: red;
    }
  }
}
Charles Kenney
Charles Kenney
15,604 Points

Cosimo,

What you're trying to do won't work because the class .bg4 is a sibling to .container. The code you have written selects a .container element which is a dependent of .bg4 (child) while the mouse is hovered over .bg4. This would only work if the container div was a child of .bg4.

1 Answer

Cosimo Scarpa
Cosimo Scarpa
14,047 Points

Charles Kenney thank you for your answer. You are right but also for some reason I wasn't able to select .bg1 class. I mean the reason it was because the class has 'z-index: -1'. I also tried just to hover .bg1 to change the colour but without results.

By the way the correct code is

.bg1 {
  &:hover ~ .container #eye {
    background: red;
  }
}