Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Daniel Maia
Daniel Maia
2,559 Points

Change child element with a different CSS as parent element is hovered. How?

How do I change a child element and the parent element when the parent element is selected, and both are going to be assigned to different properties' values? For example:

HTML

<footer id="gameStart">
    <div>
        <p> START </p>
    </div>
</footer>

What's supposed to happen on CSS

1. If footer#gameStart is hovered, then{
    footer#gameStart background-color: #787878;
    footer div background-color: #CCCCCC;
}

If possible, a way to solve it without scripting. Thanks :)

2 Answers

Hi Daniel,

You can use the :hover selector.

For example:

/* default state */
footer#gameStart {  }
footer#gameStart div {  }

/* when #gameStart is hovered */
footer#gameStart:hover { background-color: #787878; }
footer#gameStart:hover div { background-color: #CCCCCC; }
Daniel Maia
Daniel Maia
2,559 Points

It worked :) Thanks. It was so obvious, and I was never going to guess it. I thought it had something to do with the selector x:hover + y{ } Thanks you a lot! You saved me a lot of headache!

Daniel Maia
Daniel Maia
2,559 Points

It worked :) Thanks. It was so obvious, and I was never going to guess it. I thought it had something to do with the selector x:hover + y{ } Thanks you a lot! You saved me a lot of headache!

Daniel Maia
Daniel Maia
2,559 Points

It worked :) Thanks. It was so obvious, and I was never going to guess it. I thought it had something to do with the selector x:hover + y{ } Thanks you a lot! You saved me a lot of headache!

Michael Smart
Michael Smart
16,633 Points
#parent:hover {
    background: green;
}

#parent:hover #child {
    background: yellow;
}

This will achieve the desired effect, there will be a DRY'er way with Js, but for vanilla CSS this will do the job.