For <p> in particular, the rule is explained very clearly here here: A p element's end tag can be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, ...
You cannot nest P elements it is illegal. The P element represents a paragraph. It cannot contain block-level elements (including P itself). DIV however is a generic container where content can be inline e.g SPAN or block-level.
No, a paragraph element may not contain other block elements. A paragraph tag is intended for a block of text. If your elements is a part of the text (and not block elements), it would be semantically correct, otherwise not.
Answer. In this case, this is done purely for demonstrative purpose. In the future you will find that <div> elements are often used to group related content. This can serve to give a page some added structure and to allow for more modular styling.
Because a paragraph is a paragraph .. and that's how HTML is defined (and HTML is not XML).
Any <p>
(or other block-level element) will implicitly close any open <p>
.
Per 9.3.1 Paragraphs: the P element of the HTML 4.01 specification:
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
Note that this is how the HTML is parsed and that even a <div>
would have implicitly closed the paragraph!
However, a <span>
with display:block;
would not have closed the <p>
as a <span>
is not a block-level element.
That is, the CSS is irrelevant at this stage of the HTML processing and the CSS is irrelevant to the DOM/parser when determining if an element is a block-level element or not. Consider the case when CSS is applied dynamically or through a not-yet-loaded-stylesheet: the applied CSS does not alter the DOM.
While the HTML5 (working-draft) specification does not include the language above in the HTML4 specification, it does go on to define a paragraph as a container for phasing content and further has a section on paragraphs.
The accepted answer to List of HTML5 elements that can be nested inside P element? says that <p>
elements cannot nest in HTML5. The key phrase from the documentation is: "Runs of phrasing content [which does not include <p>
elements] form paragraphs". Furthermore, HTML5, trying to be backwards-compatible in many aspects, has a rationale on "Restrictions on content models and on attribute values":
Certain elements are parsed in somewhat eccentric ways (typically for historical reasons), and their content model restrictions are intended to avoid exposing the author to these issues.
This behavior is referenced from a HTML5 WG wiki entry on flow content:
HTML5's restrictions on nesting of p elements and on what p elements may contain, are due to, quote: “peculiarities of the parser” that causes p to be auto-closed ..
From the HTML 4.01 specification section 9.3.1
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
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