Zach Starnes
Zach Starnes

Reputation: 3198

Child change when parent is hovered over

I am trying to get a child element to change when the parent is hovered. I also want an attribute of that parent to change as well. I am trying to get the background color of #action to change and the color of the a or h1 to change when action is hovered over. Is this possible?

here is the html

<section id="action" class="general">
    <div class="container">
        <a href="#"><h1>This text</h1></a>
    </div>
</section>

and here is the css. CSS is built using SASS that is why it is structured like that.

#action {
    background-color: $bgLight;
    border-top: 1px solid #252525;
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    -ms-transition: all 0.2s ease-out;
    -o-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;

    a {
        text-decoration: none;

        h1 {
            margin: 0;
            color: $colorLight;
            font-weight: 300;
            text-align: center;
        }
    }
}

#action:hover a {
    background-color: #76A7D1;
    color: $colorDark;
}

Upvotes: 3

Views: 3769

Answers (3)

fmquaglia
fmquaglia

Reputation: 407

You can do the same as @Alessandro Minoccheri suggested but in a less verbose way which I like particularly:

#action {
    background-color: $bgLight;
    border-top: 1px solid #252525;
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    -ms-transition: all 0.2s ease-out;
    -o-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;

    a {
        text-decoration: none;

        h1 {
            margin: 0;
            color: $colorLight;
            font-weight: 300;
            text-align: center;
        }
    }
    &:hover{
        background-color: #76A7D1;
        a{
           h1{
              color: $colorDark;
           }
         }
    }

}

The & within #action refers to the parent element, in other words to #action itself.

I like this approach because everything gets self contained within one style declaration and is less repetitive.

It is like saying: "... and when this element is hovered apply these styles to it, and these styles to a and h1".

One small comment regarding your markup @zachstames: a (anchor element) is an inline content element, while h1 (header of level 1) is a block element. According to the W3C specifications an inline element should not contain block elements but only data.

Hope it helps.

Cheers!

Upvotes: 1

leoMestizo
leoMestizo

Reputation: 1499

This is what you want? DEMO

You are able to make this:

#action:hover {
    background: yellow;
}

#action:hover a {
    background-color: #76A7D1;
    color: white;
}

As you can see I repeat the use of the pseudo-class #action:hover. I'm saying:

"When action is hover, change it's backgroud AND when action is hover, change the background and the font color of the a element".

Hope I've helped.

Be good, Leonardo

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

Try this:

#action {
    background-color: $bgLight;
    border-top: 1px solid #252525;
    -webkit-transition: all 0.2s ease-out;
    -moz-transition: all 0.2s ease-out;
    -ms-transition: all 0.2s ease-out;
    -o-transition: all 0.2s ease-out;
    transition: all 0.2s ease-out;

    a {
        text-decoration: none;

        h1 {
            margin: 0;
            color: $colorLight;
            font-weight: 300;
            text-align: center;
        }
    }
}

#action:hover{
    background-color: #76A7D1;
    a{
       h1{
          color: $colorDark;
       }
     }
}

Upvotes: 4

Related Questions