We discourage authors from using empty P elements.
User agents should ignore empty P elements.
How do we define empty?
Does a space character between the <p> </p>
tags count?
What about a new-line character between <p> </p>
?
We discourage authors from using empty P elements.
User agents should ignore empty P elements.
How do we define empty?
Does a space character between the <p> </p>
tags count?
What about a new-line character between <p> </p>
?
The HTML4 spec defines an empty element as one that has no content. Self-closing tags definitely define empty elements, like <br/>
and <img/>
. I think it's very reasonable to expect that a start tag immediately followed by an end tag, such as <p></p>
also defines an empty element. Whether or not <p> </p>
qualifies as "empty" is likely up to the user agent, but let's look at the inline content model definition from the spec:
<!-- %inline; covers inline or "text-level" elements -->
<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">
"Empty" content is one that contains none of the following:
#PCDATA
%fontstyle;
%phrase;
%special;
%formctrl;
So is a space character any of the above? Yes, it's #PCDATA
, so I would deduce that <p> </p>
is not an empty P
element. And regardless of the HTML4 spec's discouragement, <p></p>
, <p> </p>
, and <p/>
are all a perfectly valid HTML fragments.
Such elements are now called "void" by the HTML5 spec.
The term "void elements" in HTML5 refers to those element types whose instances can never have content like br and img, not to instances that happen to have no content. So instances of P elements never constitute a "void element."
The HTML5 spec makes no such comment about avoiding "empty" or "void" P
elements:
...I really wouldn't worry too much about it.
respectively, all of which are valid. The validator emits a warning on the use of /> in each of these examples. Something like
for example would not validate for the same reasonFrom a technical perspective, whitespace is a thing. It takes up memory and has its own character code. It is a character that contains only whitespace.
That said, a space and a newline character are the same thing to most browsers. From that perspective, a "p" tag with either would be considered "something".
HOWEVER, what the W3C says about this and what particular browser vendors say can often be a very different thing. Most browsers honor this to my knowledge, but some may still treat a "p" tag as if it has content when it does not.
Best practice is to use CSS to provide spacing and breaks. Use "br" tags over paragraphs if just the line break is needed. The "p" tag is a semantic tag for use with the language construct "paragraph", and as such using it as a layout element is discouraged.
An empty <p>
tag as far as I know would include <p></p>
, <p/>
, <p />
, and <p> </p>
.
<p><br /></p>
and <p> </p>
(and if you want to get really technical, <p> </p>
since all Unicode HTML whitespace technically uses 1-4 bytes) would not be technically empty, but one certainly wouldn't be encouraged to use it as such, and if you are you should look at why, because it's likely not the best choice.
A <p>
tag is meant to contain a paragraph of text.
` to be safe. – Casey Dwayne Mar 05 '13 at 05:11