4

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

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • 5
    This is from the HTML4 spec; just so you're aware, you should probably take care to reference [the HTML5 spec](http://www.w3.org/TR/html5/) for current information. – Matt Ball Mar 05 '13 at 05:08
  • 2
    If I have a blank paragraph, I put `

     

    ` to be safe.
    – Casey Dwayne Mar 05 '13 at 05:11

3 Answers3

7

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:

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:

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

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 2
    Browsers do not actually honor the HTML 4.01 recommendation, as one can easily check e.g. with `


    `, which should not differ from `

    `, but actually does. So the HTML5 CR reflects the reality. The question is theoretic only, and stops being even a theoretic question when HTML 4.01 will be officially superseded by HTML5.
    – Jukka K. Korpela Mar 05 '13 at 05:50
  • 2
    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". – Alohci Mar 05 '13 at 09:20
  • @Alohci I was going to ask for a citation, [but found it easily enough](http://www.w3.org/TR/html-markup/syntax.html#syntax-elements). Thanks for clarifying. – Matt Ball Mar 05 '13 at 14:00
  • Note that the /> XML syntax is meaningless in HTML. The only reason
    , and

    all validate in HTML 4 is because the validator sees them as just
    , and

    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 reason
    does not: because the mandatory end tag is missing.
    – BoltClock Mar 16 '17 at 12:54
1

From 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joe Mills
  • 1,619
  • 11
  • 12
  • Don't use `br` for *any* line breaks, only for meaningful ones (like in addresses or poems). – unor Mar 05 '13 at 13:05
1

An empty <p> tag as far as I know would include <p></p>, <p/>, <p />, and <p> </p>.

<p><br /></p> and <p>&nbsp;</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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dustin Perolio
  • 193
  • 1
  • 4
  • 16