Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why some HTML5 tags must have a starting and ending tag instead of self closing them with />?

Tags:

html

In HTML5 there are tags that must have a starting and an ending tag even when their content is empty:

<script></script>   <!-- Right way. -->
<div></div>         <!-- Right way. -->
<content></content> <!-- Right way. -->

<script/>  <!-- Wrong way. -->
<div/>     <!-- Wrong way. -->
<content/> <!-- Wrong way. -->

In XML this difference doesn't exist: <node/> and <node></node> are the same entity.

Why tags like script, div and content can't be defined simply as: <script/>, <div/> and <content/>?

like image 548
Daniele Orlando Avatar asked Oct 22 '15 14:10

Daniele Orlando


People also ask

Why do some HTML tags not have closing tags?

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.

Why are opening and closing tags important in HTML?

An opening tag begins a section of page content, and a closing tag ends it. For example, to markup a section of text as a paragraph, you would open the paragraph with an opening paragraph tag <p> and close it with a closing paragraph tag </p> (closing tags always proceed the element with a /).

Do all HTML tags require a beginning and end tag?

No, all HTML tags don't have end tag. Example- <br> tag is used to break the line. It doesn't have an end tag. However most of the tags have end tags.

Does HTML5 have self-closing tags?

↑ The full list of valid self-closing tags in HTML5 is: area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track, and wbr.


2 Answers

From the w3c:

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag. In XHTML, it is possible for any element to use this syntax. But note that it is only conforming for elements with content models that permit them to be empty.

The examples you listed would generally contain content, JavaScript, or other elements, so having a proper start and end tag would delimit the scope of those elements/tags.

like image 194
apaul Avatar answered Oct 02 '22 20:10

apaul


Simply because they are basically some "container" for other elements.

There are elements which are didn't used as parent elements for others, like img or base for example, this one can be closed without an closing tag with a trailing />, but it is not necessary.

like image 20
mstruebing Avatar answered Oct 02 '22 19:10

mstruebing