When is the <p> tag considered empty?
The HTML4 specification defines an empty element as one that has no content. Self-closing tags definitely define empty elements, such as <br/> and <img/> . I think itโs very reasonable to expect that the start tag, immediately followed by the end tag, such as <p></p> , also defines an empty element. Regardless of whether the <p> </p> "empty", most likely, to the user agent, but let the definition of the model of inline content from the specification:
<!-- %inline; covers inline or "text-level" elements --> <!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;"> An โemptyโ content is one that does not contain any of the following elements:
So, any space character? Yes, this is #PCDATA , so I would deduce that <p> </p> not an empty P element. And regardless of the rejection of the HTML4 specification, <p></p> , <p> </p> and <p/> are absolutely valid HTML snippets .
Such elements are now called the "void" HTML5 specification.
The term "void elements" in HTML5 refers to those types of elements whose instances can never have content such as br and img, and not instances that have no content. Thus, instances of the elements of P never constitute a โvoid elementโ.
The HTML5 specification does not make such comments about excluding "empty" or "void" P elements:
- http://www.w3.org/TR/html5/grouping-content.html#the-p-element
- http://www.w3.org/TR/html-markup/p.html
... I really would not worry too much about it.
From a technical point of view, spaces are a thing. It takes up memory and has its own character code. This is a character that contains only a space.
However, the space and the newline are the same for most browsers. From this point of view, the tag "p" with any of them will be considered as "something."
HOWEVER, what the W3C says about this and what individual browser vendors say can often be completely different. As far as I know, most browsers respect this, but some can still handle the "p" tag as if it contained content, but not.
Best practice is to use CSS to provide spacing and breaks. Use the br tags over paragraphs if you only want line breaks. The p tag is a semantic tag for use with the paragraph language construct, and therefore, its use as a layout element is not recommended.
To my knowledge, an empty <p> will include <p></p> , <p/> , <p/> and <p> </p> .
<p><br/></p> and <p> </p> (and if you want to get really technical information, <p> </p> since all the Unicode HTML blank space technically uses 1-4 byte) will not be technically empty, but it is certainly not recommended to use it as such, and if you are, then you should consider why, because this is probably not the best choice.
The <p> must contain a paragraph of text.