This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Viktor
about 9 years

Should the HTML link tag be self-closing or not?

Over at W3schools they say that it should be self-closed like this:

<link type="text/css" ; rel="stylesheet" ; href="stylesheet.css">

But over here at Codeacademy the editor warns me about it and want it to be closed like this:

<link type="text/css" ; rel="stylesheet" ; href="stylesheet.css"/>

Which one is really the official way to do it?

Answer 54e3e31a93767694650015c3

2 votes

Permalink

It would depend how the server is responding. Is it serving HTML or XML? HTML5 may be either HTML or XML conforming, depending how we write it.

<!DOCTYPE html>
<html>
</html>

The Document Type Declaration may be written in any case. Declaring the root element as HTML is all the browser needs to know it is dealing with HTML5 (in newer browsers).

At this point the browser expects the MIME Type, text/html, and if we don’t otherwise declare it, this is what the server will be sending. None of the markup needs to conform to XML in this case, so no self-closing tag syntax is needed, either.

<link rel="" href="">

is enough (filling in the attributes). The type is taken from the MIME type (in the header) of the resource, in this case, .css implies, text/css. For HTML5 this is a given so the type attribute is not required. Same goes for script. .js is, text/javascript, and needn’t be attributed. It’s when we wish to deviate from the HTML5 norms that we need to include type attributes.

Just to recap:

HTML 4 => <link>
XHTML => <link /> => XML compliant
HTML5 => <link/> => XML compliant

We’re most likely serving text until we start getting into serious App development. If one is heading in that direction, then establishing good habits now will ensure they are burned in by the time one reaches that level, and it can’t hurt anything since HTML 4 parsing ignores it. text/html is parsed as HTML 4, just so we keep in mind that most HTML 4 is valid HTML5 (taking away obsolete and deprecated elements/attributes).

Personally, I work strictly in HTML 4 syntax, not the XML compliant syntax I was so hardened into using for XHTML. The choice is one’s own, as it were, under the given terms above.

Aside:

There are no semi-colons in HTML elements. The attributes are separated by a single space.

points
Submitted by Roy
about 9 years