3

I've been looking through a ton of sites recently to get a better understanding of how large websites structure their HTML5 pages.

I noticed that they tend to do

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

Instead of

<script src="test.js" />

But do

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

Instead of

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

Even in this technique blog they do so, why is this a preferred design style?

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

Peter O.
  • 32,158
  • 14
  • 82
  • 96
user1431282
  • 6,535
  • 13
  • 51
  • 68

3 Answers3

3

That's because the web is normalized and some things are possible according to the norm and some other aren't. With a logic behind being that some elements are naturally void and some others aren't.

From the w3.org on the script element :

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

So you can't have

<script src="test.js" />

On the link element :

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

A link element, which can't have a content, can be written

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

or even (much better if you don't try to write XHTML)

<link rel="stylesheet" href="test.css">
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

According to the HTML standard, the ending tag is required for the script tag, it can't be a self-closing tag.

Self-closing tags are used in XHTML for tags that doesn't require a closing tag in HTML, like the link and input tags.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

In HTML, there are tags which are always self-closed. For example,

<hr>Some content here</hr>

does not make any sense. In the same way, there are tags which cannot be self-closed. tag is one of them.

I am not sure about the reason of no self-closed tags, but the reason might come from the fact that the tag was intended to always contain code inside.

Wazy
  • 8,822
  • 10
  • 53
  • 98