35

</p> is only required in XHTML but not in HTML. Some times you have to close it anyway , e.g. when you align the paragraph left/right/center.

Would mixing the usage of </p> as in the following be a bad idea? Note that there is no ending </p> tag in the 1st and 3rd paragraph.

<h1>My article</h1>
<p>This is my 1st paragraph.
<p class="align=right">This is my 2nd paragraph</p>
<p>This is my 3rd paragraph.

Reasons why I don't want to close the P-tags:

  • Easy to work with in my CMS (more demanding code to make it XHTML)
  • 1kb lighter files
Hakan
  • 3,835
  • 14
  • 45
  • 66
  • 3
    Does it hurt anything to close it? – Daenyth Dec 11 '11 at 00:23
  • 34
    **Always write well-formed HTML.** What have you got against closing `

    ` (or any other) tag? It's a very bad practice and leads to confusion, bugs and dead kittens.

    – Bojangles Dec 11 '11 at 00:23
  • 2
    possible duplicate of [What are the actual problems of not closing tags and attributes in HTML](http://stackoverflow.com/questions/7125354/what-are-the-actual-problems-of-not-closing-tags-and-attributes-in-html) – robertc Dec 11 '11 at 00:27
  • 3
    I concur. You should always close your HTML tags. Just because some standards do not require them doesn't mean that is acceptable. You avoid inter-browser incompatibilities if you need a more convincing reason. – Xcalibur37 Dec 11 '11 at 00:27
  • 1
    It is also not syntactically required to use whitespace in your code most of the time. Readability is another thing. – kapa Dec 11 '11 at 00:28
  • "1kb lighter files" - This is actually overstated. Your web server/CMS should be configured to use HTTP compression. If it is, a 1kb difference in the size of an HTML/text file isn't going to amount to anything more than a handful of bytes when the page is actually transmitted. And if it isn't, then you have more significant inefficiencies than closing tags taking up too much space. – aroth Sep 27 '15 at 00:45
  • @Daenyth It hurts when you use it in a Angular 2 template. I just got the error `Unexpected closing tag "p"` – Luke T O'Brien Aug 15 '17 at 14:52
  • @Bojangles I'm not sure if it's normatively a bad practice. See Google's public HTML style guide. https://google.github.io/styleguide/htmlcssguide.html – user823447 Oct 20 '19 at 22:40
  • oops, just came across https://www.w3.org/Style/Examples/007/center.en.html that is full of unclosed p tags – George Birbilis Jan 15 '20 at 17:03
  • @Bojangles While I personally agree with your opinion that it is better to close tags, including the `

    ` tag, my understanding is that unclosed `

    ` tags are in fact well-formed HTML 5 (https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element).

    – M. Justin Aug 08 '22 at 20:23

4 Answers4

49

P-end-tag is only required in XHTML, not in HTML.

Correct

But some times you have to close it any way eg. when you align the paragraph left/right/center.

Incorrect. The only time you need an explicit end tag is when you want to end the paragraph and immediately follow it by something that is allowed inside a paragraph (such as text or an inline element). This is usually a bad idea.

Would it for any reason be a bad idea to mix the usage of P-end-tag

Only that consistency is a virtue which aids in code maintenance.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • And that make it possible to be consistency only using P-start-tag :D What about IMG-tags is it OK to have them inside P-tags (with "style="display:block")? What do you mean with "This is usually a bad idea." – Hakan Dec 11 '11 at 00:49
  • 3
    It very rarely makes semantic sense to have inline elements and block elements as siblings. – Quentin Dec 11 '11 at 10:15
19

HTML5 quote that makes it clear when p can be omitted

Since this is why most Googlers must be coming here: 4.4.1 "The p element" https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element

Tag omission in text/html:

A p element's end tag can be omitted if the p element is immediately followed by an address, article, aside, blockquote, details, div, dl, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6, header, hgroup, hr, main, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is an HTML element that is not an a, audio, del, ins, map, noscript, or video element, or an autonomous custom element.

Worth noting that a few other closing tags can also be omitted, e.g. li: https://html.spec.whatwg.org/multipage/grouping-content.html#the-li-element

The full list can be found at 12.1.2.4 "Optional tags" https://html.spec.whatwg.org/multipage/syntax.html#optional-tags

I don't recommend doing it however as others mentioned.

This HTML "feature" is one of the reasons why HTML5 is not a subset of XML.

History lesson

I was reading the history of HTML on Wikipedia, and when I reached the page about IBM Generalized Markup Language, which is a ~1970 predecessor to SGML, which is a predecessor to XML, which is a predecessor to HTML, when I saw this gem of a document sample:

   :h1.Chapter 1:  Introduction
   :p.GML supported hierarchical containers, such as
   :ol.
   :li.Ordered lists (like this one),
   :li.Unordered lists, and
   :li.Definition lists
   :eol.
   as well as simple structures.
   :p.Markup minimization (later generalized and formalized in SGML),
   allowed the end-tags to be omitted for the "h1" and "p" elements.

The example is copied verbatim from the home page of one of the creators of GML.

So it is a bit funny how the world goes around. In the 70's people already wanted autoclose. Then it evolved into XML which is something saner that forbids autoclose. But then we got autoclose back in HTML. HAML template syntax also comes to mind. Also not how the tag names are identical as those in HTML (h1, p, ol, li).

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
4

I can't think of any reasons why you couldn't do this, but would strongly encourage just using both the beginning and end tags everywhere for consistency. It'll also make it easier for when you do decide to start following the XHTML standards.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
1

You only need to finish the last <p> tag. Example:

<p align="left">Left
<p align="center">Center
<p align="right">Right</p> <!-If you don't put an ending tag, the text will continue in the same line
<p>End</p>

Plus it's faster and reduces the memory of the HTML file.

Boda
  • 27
  • 3