Why is this HTML convention recommended?

I have been browsing tons of sites recently to get a better understanding of how large websites structure their HTML5 pages.

I've noticed what they tend to do

<script src="test.js"></script> 

      

Instead

<script src="test.js" />

      

But do

<link rel="stylesheet" href="test.css"/> 

      

Instead

<link rel="stylesheet" href="test.css"></link>

      

Even on this tech blog they do it, why is this the preferred design style?

http://net.tutsplus.com/tutorials/html-css-techniques/25-html5-features-tips-and-techniques-you-must-know/

+3


source to share


3 answers


This is because the network is normalized and some things are possible according to the norm and some are not. Logically, some elements are naturally invalid and some others are not.

From w3.org in the script element :

The script element must have both a start tag and an end tag.

So you cannot

<script src="test.js" />

      

In the link element :



The link element is a void element. The link element must have a start but must not have an end tag.

Element

A link

that cannot have content can be written

<link rel="stylesheet" href="test.css"/>

      

or even (much better if you are not trying to write XHTML)

<link rel="stylesheet" href="test.css">

      

+3


source


According to the HTML standard, an end tag is required for a tag script

, it cannot be a self-closing tag.



Self-closing tags are used in XHTML for tags that do not require an end tag in HTML, such as link

and tags input

.

+1


source


HTML has tags that are always self-closing. For example,

<hr>Some content here</hr>

doesn't make any sense. Likewise, there are tags that cannot be self-closed. tag is one of them.

I'm not sure about the reason for the lack of self-closing tags, but the reason may be that the tag should always contain the code inside.

+1


source







All Articles