3

Please say there's a simpler/shorter way to write this CSS:

.ads h3{
color: #f7f7f7;
}
.ads p{
color: #f7f7f7;
}
.ads blockquote{
color: #f7f7f7;
}

It's a right pain at the moment and takes up space in the CSS file...

7 Answers 7

7

You can group selectors that you wish to share common rules by separating them with commas. So, the following will work:

.ads h3, .ads p, .ads blockquote {
   color: #f7f7f7;
}

See the CSS 2.1 Specification, Section 5.2.1.

Sign up to request clarification or add additional context in comments.

Comments

6

God says: "Yes"

"Your prayers have been answered my child"

.ads h3, .ads p, .ads blockquote {
    color: #F7F7F7;
}

And even taking it further (if there are no child elements which should not be colored as well):

.ads * {
    color: #F7F7F7;
}

And if you're OK with the text inside the paragraph itself to also have this same gray color:

.ads {
    color: #f7f7f7;
}

This will be overridden by any other styles that have been set on p, blockquote or h3, so you might want to take it further:

.ads, .ads * {
    color: #f7f7f7;
}

4 Comments

+1 for the entertainment, but -1 because .ads * will still be overridden by .ads h3 (and others), so it'll be useless. The universal selector * holds no value in terms of specificity. In other words, .ads * is specifically equivalent to .ads.
@BoltClock that's not quite right. See example: jsfiddle.net/4GqaF color gets overwritten. But thanks for the input
.ads * overrides h3 because class is more specific than element so it wins out. If you change h3 to .ads h3 it overrides .ads *. See jsfiddle.net/JNPXm
yes, exactly. hmm, I see that I misread your comment. I thought you were saying that .ads * will get overridden by h3. sorry my bad. But you said two wrong things.. ".ads * is specifically equivelent to .ads" - that's not right because .ads * will color only the element inside of it, not the text within .ads, and you said that "* holds no value in terms of specifity" which is also not true.
1

You can write it like this to make it simpler/shorter.

.ads h3, p, blockquotes{
color: #f7f7f7;
}

Comments

1

Unless there are any other overriding rules you haven't shown, this will be fine:

.ads 
{
color: #f7f7f7;
}

1 Comment

Does not maintain equal specificity but if there are no other elements in .ads then this is fine. +1
0
.ads h3, .ads p, .ads blockquote{
  color: #f7f7f7;
}

Comments

0
.ads h3,
.ads p,
.ads blockquote {
  color: #f7f7f7;
}

Comments

0

You don't need the trailing ; either (per YUI CSS Min)

.ads h3,.ads p,.ads blockquote{color:#f7f7f7}

1 Comment

For development, sure. If you're looking to reduce space, it's not needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.