7

If I have a dozen CSS selectors, and want to assign :hover properties to all of them, I'm used to doing this:

selector, selector2, someOtherSelector, someSelector div {
    //some properties
}
selector:hover, selector2:hover, someOtherSelector:hover, someSelector div:hover {
    //some properties
}

Typing :hover four times seems redundant. Is there a way to group the selectors like

(selector, selector2, someOtherSelector, someSelector div):hover {
     //some properties
}

instead?

j08691
  • 197,815
  • 30
  • 248
  • 265
Isaac Lubow
  • 3,545
  • 3
  • 34
  • 53
  • An interesting related question, btw: http://stackoverflow.com/questions/800779/why-cant-you-group-descendants-in-a-css-selector?rq=1 – Jason C Jul 18 '16 at 19:29

3 Answers3

5

Not natively in CSS. By using something like SCSS, you can write:

selector, selector2, someOtherSelector, someSelector div {
  // some properties

  &:hover {
    // some more properties
  }
}
Sophie Alpert
  • 133,880
  • 36
  • 215
  • 235
4

If they all share the same hover properties you could create a class that is shared for all that defines your :hover

So you'd get:

allSelectors, selector, selector2, someOtherSelector, someSelector div {
    //some properties
}
allSelectors:hover {
    //some properties
}

Re-usable classes makes for cleaner and less code.

Mark
  • 5,590
  • 2
  • 24
  • 25
3

Sadly, there's not really any easier way of doing what you're trying to do, unfortunately. Unless you want to move the styles to jQuery or something (but that's not a good solution).

Brad Christie
  • 98,427
  • 16
  • 148
  • 198