0

I have css declaration like this

#first, #second, #third {
 font-size:1em;
 background: url('path/to/image') no-repeat;

}

Next I want to remove background only for #third;

I have tried

#third {
background:none;
}

But it doesn`t work.

4
  • 2
    Is your second declaration below the original declaration? Commented Mar 22, 2011 at 12:59
  • Are you sure you want a descendant selector? Commented Mar 22, 2011 at 13:02
  • Your first selector: #first, #second #third. Are you sure you don't mean #first, #second, #third, with the extra comma? Commented Mar 22, 2011 at 13:03
  • It is typo. It should looks #first, #second, #third Commented Mar 22, 2011 at 13:59

4 Answers 4

2

Your first selector is more specific than the second one, so it has priority.

To solve this, you can make the second selector equally specific by changing it to #second #third.
If that won't select the same elements, your design is flawed; element IDs should be unique.

Alternatively, you can add !important to the second background rule to force it to override the first one.

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

Comments

2

Why not just do this?

#first, #second {
 font-size:1em;
 background: url('path/to/image') no-repeat;

}
#third {
font-size:1em;
}

3 Comments

That's not the same. Read his selector carefully.
@SLaks I am pretty sure he mean #first, #second, #third if not, I agree with you
My mistake. It is #fist, #second, #third
1

So why not just remove third from the first style declarations and make a seperate

#third {
   font-size: 1em;
}

Comments

1

It's not working because #second #third is more specific than #third. Did you mean to group #second and #third together, or did you forget a comma?

It sounds like that wasn't what you intended, so this should fix it:

#first, #second, #third {
    font-size:1em;
    background: url('path/to/image') no-repeat;
}

Note the comma - that's all it takes. Your #second element should also be styled correctly with this edit.

Also, did you place the separate declaration for #third after the other set of code? Order matters, and it would need to come after the declaration of #first, #second and #third.

Comments

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.