Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a <P> tag considered empty?

W3.org states:

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>?

like image 204
Pacerier Avatar asked Jan 15 '23 01:01

Pacerier


1 Answers

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.

@Alohci clarifies:

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:

  • http://www.w3.org/TR/html5/grouping-content.html#the-p-element
  • http://www.w3.org/TR/html-markup/p.html

...I really wouldn't worry too much about it.

like image 92
Matt Ball Avatar answered Jan 19 '23 12:01

Matt Ball