Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting block level elements inside the <p> tag... right or wrong?

Tags:

html

css

Is it syntactically and semantically correct to nest <div> or any other block level element inside the <p> tag. I am talking about HTML4 Transitional DTD.

If not then is it OK to instead use <span style="display: block"> instead?

like image 844
Salman A Avatar asked Nov 27 '10 11:11

Salman A


People also ask

Can you nest P tags?

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

Is P tag a block level element?

The p element is an example of a block level element. Each new paragraph tag will appear on its own line vertically. Paragraphs with longer content will stretch all the way to the edge of the page.

Can block level elements be nested?

Yes, you can have nested block elements. You may need to use floats or positioning to keep them from stacking though. I think you may be confusing having a block element child of a block element (good) with a block element that is a child of an inline element (will probably render fine; but violates standards).

Can P tags be inside tags?

This is not allowed. An <a> element may not be a descendant of another <a> element. It is weird and it shall be forbidden. html block element inside inline element.


1 Answers

Syntactically, a div inside a p is invalid in all standards of HTML. Moreover, when using a conforming HTML parser, it is impossible to place a <div> element inside a <p> in the DOM because the opening <div> tag will automatically close the <p> element.

Semantically, the correct choice depends on the content that you are marking up. You will need to show at least a sample full paragraph and possibly the content surrounding it to be sure of providing sufficient information for the correct semantic mark-up to be determined.

However, given that both <div> and <span> are semantics free, and that CSS in no way can ever change that, if you are certain that the contents of the <p> tag truly form a paragraph, and that <span style="display: block"> gets you the presentational effect that you are seeking, then that is valid HTML and would be a wholly appropriate solution.

like image 86
Alohci Avatar answered Sep 19 '22 05:09

Alohci