Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paragraph ignores style because of another nested paragraph

Tags:

html

css

Probably my CSS knowledge is limited, but I don't uderstand this:

<p style="color: green">
    <p style="color: red">This is red</p>
    This should be green. But it's not.
</p>

The second line will render in black ignoring the "color:green". Why? I tested it in Chrome 28 and Firefox 22.

like image 341
Robert Veres Avatar asked Aug 03 '13 11:08

Robert Veres


People also ask

Can paragraph be nested?

You cannot nest P elements it is illegal. The P element represents a paragraph. It cannot contain block-level elements (including P itself).

What is the correct for paragraph in CSS?

The <p> tag defines a paragraph. Browsers automatically add a single blank line before and after each <p> element. Tip: Use CSS to style paragraphs.


1 Answers

You can't nest paragraphs.

Paragraph is an auto-closing element, the </p> is optional - any block element will automatically close the last open <p>.

This is what's happening:

<p style="color: green">
</p> <!-- auto-closed paragraph -->

<p style="color: red">
    This is red
</p>

This should be green. But it's not.

</p> <!-- here you have syntax error -->
like image 73
MightyPork Avatar answered Oct 11 '22 13:10

MightyPork