1

I have a very specific question on CSS Specificity, something which I could not clearly understand on; http://coding.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

If I have 2 selectors which define non-contradicting properties/attributes, will both of the attributes still get applied OR the way it works is, it just compares the selectors, without bothering what is defined inside them.

So if we have;

.menu1 {color:red}
p.menu1 {font-size:10px}

Here both the selectors refer to "menu1", but define unrelated attribues (color/font-size)

So my question is does the Specificity still matter and only 1 of the 2 will be considered ? My question is more about how the actual implementation happens.

2 Answers 2

1

No, in this case the specificity does not matter as you are simply adding an extra property to the menu1 class, which isn't changing any existing rules. However, if you reverse the order of the rules and attempt to override color:

p.menu1 {color:blue}
.menu1 {color:red}

then the less specific rule .menu1 {color:red} will not override the more specific p.menu1 {color:blue}, even though the 'red' rule appears after the 'blue' rule (try it out).

Of course, if you change the second rule to p.menu1 {color:red} it will override, as the two rules would then have the same specificity.

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

1 Comment

You seem to have hit the nail on the head...Your answer is exactly what I was looking for...Thx a lot..
1

In the article's overview, point 11 says:

11. The last rule defined overrides any previous, conflicting rules.

(where "last rule" may be replaced with "last or most specific rule")

So if there are different properties then nothing is overridden. The two rules are combined, so p.menu1 will all have a font size of 10 pixels and be red in color. The second rule still has a more specific selector, but it's simply not relevant until you specify a common style that will potentially be overridden.

How all of this is done in implementation is, well, an implementation detail, so I don't know.

1 Comment

Bear in mind that elements with class="menu1" but are not p elements will only be red in color; their font size may not be 10 pixels because they won't respond to the p.menu1 selector.

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.