1

I have:

#signUpForm h1, #signUpForm p{
    color: #fff;
}

Is there a way to shorten this such as:

#signUpForm h1, p{
    color: #fff;
}

Or do I need to create a new comma separated selector each time, as in my first example?

Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
Paul Dessert
  • 6,280
  • 8
  • 42
  • 73

3 Answers3

4

Have you thought of using Sass, you could do something like

#signUpForm{ 
    h1, p {
        color: #fff;
    } 
} 
Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
Bobadevv
  • 366
  • 1
  • 8
3

It depends on the properties you are specifying and the elements you want to target, if its jut about color then you can write

#signUpForm {
    color: #fff;
}

Because color property will be inherited by h1 as well as p but think of a scenario where you want to apply font-weight so here you cannot use the above selector as font-weight won't be inherited by h1 tag.

If you want, you can use CSS pre processors like LESS or SASS where you can write like

#signUpForm {
    h1, p {
       font-weight: 100;
    }
}

But again, as I said, it depends on the properties you are writing, because some are inherited by the child elements and some are not, so even if you use color like

#signUpForm {
    h1, p {
       color: #fff;
    }
}

Makes no sense because that is equivalent to #signUpForm { color: #fff; } unless you've specified a different color for h1 or p

Mr. Alien
  • 147,524
  • 33
  • 287
  • 271
1

Use :any.

#signUpForm :any(h1,p) { }

Browser support varies. You may need to do -webkit-any etc.

See https://developer.mozilla.org/en-US/docs/Web/CSS/:any. See also the proposal for the similar :matches CSS4 pseudo-class at http://dev.w3.org/csswg/selectors4/#matches.

  • Good idea, but you're right about browser support. Thanks! – Paul Dessert Jul 31 '14 at 07:16
  • note that using pseudo also affects the specificity of your selectors – Mr. Alien Jul 31 '14 at 07:18
  • Using `:any()` is pointless due to the prefixes - see http://stackoverflow.com/questions/10753174/css3-combining-selectors-with-or-instead-of-and/10753216#10753216 Also, it's a pseudo-class, not a pseudo-element. – BoltClock Jul 31 '14 at 10:13
  • @Mr. Alien: It shouldn't - the specificity of `:matches()` is whatever selector matches the element. The pseudo-class is not counted, similar to `:not()`. – BoltClock Jul 31 '14 at 10:18
  • http://the-echoplex.net/csscrush/ supports `:any`. You could use it until the browser support is better, without drinking all the LESS/SASS Kool-aid. –  Jul 31 '14 at 11:36