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?
You cannot nest P elements it is illegal. The P element represents a paragraph. It cannot contain block-level elements (including P itself).
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With