42

In css how can I define multiple class' hover events to use the same properties. This doesn't seem to work:

.my_div:hover .my_td:hover {
    border: 1px solid red;
}

Thanks

1
  • 3
    The answers got it. Also note that a space implies that the second element is a child of the first; so you are actually applying a CSS rule here to <td class="my_td">'s that only occur within <div class="my_div"> Commented Aug 26, 2012 at 18:55

6 Answers 6

72

You should separate with a comma, like this:

.my_div:hover, .my_td:hover {
      border: 1px solid red;
}
.contact-dpd:hover .contact-content, .work-dpd:hover .work-content{
     display:block
}
Sign up to request clarification or add additional context in comments.

3 Comments

@omega When an answer resolves your issue, click the green tick next to it to mark it as "accepted."
can we have only one :hover for both TD and DIV?
Strangel!!. 4 correct and similar answers, answered on the same day, same time! Luckily the mouse has "hovered" to your answer and marked it as "accepted" : ) +1
11

Add a comma in between: .my_div:hover, .my_td:hover.

Comments

9

This should work

.my_div:hover, .my_td:hover {
        border: 1px solid red;
}

Comments

8

try

.my_div:hover, .my_td:hover {
    border: 1px solid red;
}

Comments

7

Take look at CSS Selectors Level 4:

The :where() CSS pseudo-class function takes a selector list as its argument, and selects any element that can be selected by one of the selectors in that list.

The difference between :where() and :is() is that :where() always has 0 specificity, whereas :is() takes on the specificity of the most specific selector in its arguments.

:where(.a, .b):hover {
  outline: 1px solid red;
}
<div class="a">a</div>
<div class="b">b</div>
<div class="c">c</div>

It also works with selecting child elements:

:where(header, footer) p:hover {
  outline: 1px solid red;
}

:where(main, aside) p:hover {
  outline: 1px solid blue;
}

:where(header, footer) a {
  color: red;
}

b, i {
  color: green;
}

main :where(b, i) {
  outline: 1px solid;
  color: black;
}
<header>
  <p>header <a href="#">link</a></p>
</header>
<main>
  <p>main <a href="#">link</a> <b>bold</b> <i>italic</i></p>
</main>
<aside>
  <p>aside <a href="#">link</a> <b>bold</b> <i>italic</i></p>
</aside>
<footer>
  <p>footer <a href="#">link</a></p>
</footer>

3 Comments

i know this isn't the specific answer that the OP was looking for, but this is how you SHOULD right it to DRY up code. Upvote.
This is exactly the answer I was looking for! Thank you
I don't understand what where does that a simple comma doesn't do.
-3

You should separate CSS classes and events this way :

.my_div, .my_td {
  &:hover {
    border: 1px solid red;
  }
}

2 Comments

This isn't valid CSS
Specifically it's Sass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.