Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

P-end-tag (</p>) is not needed in HTML [closed]

Tags:

html

paragraph

</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
like image 847
Hakan Avatar asked Dec 11 '11 00:12

Hakan


People also ask

Does P tag need to be closed?

The <p> element is used to identify blocks of paragraph text. The closing <p> tag is optional and is implied by the opening tag of the next HTML element encountered in an HTML document after an opening <p> tag.

Which HTML tags don't need to be closed?

The void elements or singleton tags in HTML don't require a closing tag to be valid. These elements are usually ones that either stand alone on the page ​or where the end of their contents is obvious from the context of the page itself.

Is P tag necessary in HTML?

For any practical purpose, you don't need to add the </p> into your markup. But if there is a string XHTML adheration requirement, then you would probably need to close all your markup tags, including <p> . Some XHTML analyzer would report this as an error. You need to use tick marks to escape HTML tags in posts.


2 Answers

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.

like image 105
Quentin Avatar answered Sep 28 '22 16:09

Quentin


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