Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use :hover with multiple classes

I've got a css buttons style and some predefined colour styles. I use colour classes to colour things the same colour and a button style to make the buttons round.

How do I add a hover style to my buttons to change the colour to a lighter shade? I thought it would be as simple as .class class2:hover {etc} but it doesn't work for some reason.

Here's a fiddle I prepared to demonstrate: http://jsfiddle.net/7n4Wy/

HTML

<p class="red button">Test</p>
<p class="blue button">Test</p>
<p class="red"> Not a button </p>

CSS

.red {
    background: red;
}

.blue {
    background: blue;    
}

.button {
    border-radius: 6px;
}

.button:hover .red:hover {
    background: pink;
}
like image 558
ac_ Avatar asked Apr 04 '13 17:04

ac_


2 Answers

What you have is trying to match .red:hover that is inside .button:hover, which implies a nested element in your markup.

Since you're selecting the same element, you need to combine both classes with a single :hover:

.red.button:hover {
    background: pink;
}

Updated fiddle

like image 191
BoltClock Avatar answered Sep 19 '22 13:09

BoltClock


You can apply a CSS-rule to multiple selectors (classes like «.button», or states like «:hover») by separating them with a comma.

therefore just add a comma:

.button:hover, .red:hover {
    background: pink;
}
like image 39
albuvee Avatar answered Sep 21 '22 13:09

albuvee