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
  • 204,283
  • 31
  • 260
  • 272
Isaac Lubow
  • 3,557
  • 5
  • 37
  • 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
  • 139,698
  • 36
  • 220
  • 238
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,680
  • 2
  • 25
  • 26
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
  • 100,477
  • 16
  • 156
  • 200