0

I have what seems to be a pretty common CSS inheritance situation, but I don't understand why it isn't working as I thought it would.

Here is what the inheritance looks like as seen from the Chrome web debugger

Inherit

So I would expect my style for ".homeBox p" would override the style for "#mainContent p". And yet ...

Cancelled Style

The #mainContent p style overrides .homeBox p. What gives?

2
  • 2
    This is the correct behaviour #mainContent p is more specific than .homeBox p so it overrides the less specific one. Commented Feb 8, 2012 at 15:05
  • 1
    Worth reading: coding.smashingmagazine.com/2007/07/27/… Commented Feb 8, 2012 at 15:05

4 Answers 4

2

ID styles override class styles.

For consistency purposes, I try to avoid using ID styles where possible.

Its also useful to avoid using these when you start working with server side scripting which can sometimes re-render an ID (like ASP.NET does for example).

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

1 Comment

Such a simple thing I never knew. Thanks!
1

The order is first determined by specificity, and an id is more specific than a class.

Comments

1

It's because the #mainContent holds greater specificity than your other selectors: it's determined to be the most specific description of that element, and is chosen above class.

The rules for selector dominance are fairly complex, demonstrated in the W3 doc: http://www.w3.org/TR/CSS2/cascade.html#specificity

Comments

0

specificity, as mentioned in the earlier answers, is correct. The simplest fix is to not use ids! if you have to, take that into account when writing your css for nested elements, eg:

#mainContent .homeBox p{font-size: 15px;}

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.