This forum is now read-only. Please use our new forums! Go to forums
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
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency