1

I've used example in the spec Calculating a selector's specificity to determine selectors weight. Both selectors have specificity: 11 points. 11 = 10 (class name) + 1 (element name)

According to specification w3c cascading rules the 2nd selector should override the 1st one because it appears later in the file. Therefore, my question is why the paragraph is still red?

p.c11 {             
  color: darkred;
}

p .c11 {              
  color: magenta;
}  
<p>
    <p class="c11">
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sed, eius iusto     provident laudantium voluptates adipisci voluptatem amet repudiandae ex aspernatur rem voluptatibus recusandae vero corporis distinctio quia reprehenderit dolores facere.
    </p>
</p>

1 Answer 1

5

Therefore, my question is why the paragraph is still red?

Your HTML is invalid. You can't nest p elements. See the relevant spec for a list of elements that the p element can contain.

Since it's invalid, this is how the element(s) are being rendered:

<p class="c11">Lorem ipsum dolor...</p>
<p></p>

enter image description here

The paragraph is still red because the selector p .c11 will select an element with a class of c11 that is a descendant of a p element. The space in the selector p .c11 is a descendant combinator. Since you can't nest p elements, the selector p .c11 isn't selecting anything.

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

6 Comments

Why I can nest div's but cannot do the same for 'p'?
@ieXcept Because a document division can contain smaller document divisions, but it makes no sense for a paragraph to contain other paragraphs.
Also, are you sure a p can't be nested in a p. According to the spec, p can exist within flow content, of which p is a part.
@Oriol, I understand it doesn't make sense, but I'm not sure it's invalid.
@Michael_B What Oriol said. Also, it doesn't validate. The following error is thrown: "No p element in scope but a p end tag seen."
|

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.