Can self closing <link> tags be problematic?
Asked Answered
O

2

27

I read that self closing tags were problematic for some browsers such as IE7 and Firefox 3 here: Why don't self-closing script tags work?

I am curious if this issue can also hold true for linking stylesheets.

For example using

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

Instead of

<link href="/css/style.css" rel="stylesheet" type="text/css"></link>
Othella answered 30/3, 2012 at 13:31 Comment(0)
P
38

<link href="/css/style.css" rel="stylesheet" type="text/css"></link> is not a good idea.

If you use html4 use this:
<link href="/css/style.css" rel="stylesheet" type="text/css">

If you use xhtml use this:
<link href="/css/style.css" rel="stylesheet" type="text/css" />

In html5 both versions are fine.

Participate answered 30/3, 2012 at 13:38 Comment(9)
@nailer: Modern browsers do accept lots of crab. So <link></link> might work. But since it is neither valid html4 nor valid html5 nor valid xhtml the behavior of the browser is theoretically unexpected. w3schools.com/tags/tag_link.aspImpatiens
Acknowledged, and agreed, but w3schools is not a good reference.Microcosm
I must disagree with "In html5 both versions are fine." -- Maybe both work, but the HTML5 spec says no closing tag.Settler
@Flimzy: You can see 3 version in my answer. Only the version in line 1 has a closing tag. I wrote, that the versions in line 2 (html4-style) and line 3 (xhtml-style) both are fine for html5. But non of them has a closing tag. The version in line 3 is a self-closing tag. This is an allowed version in html5. For void elements like <br>or <img src=""> the self-closing version (i.e. <br />or <img src="" />) has exactly the same meaning as the non-closing version. It is just syntactic sugar that will be ignored. It is not an error. So both <link href=""> and <link href="" /> are fine.Impatiens
I read your entire answer. You don't need to repeat it. Only the last sentence is technically incorrect.Settler
@Flimzy: All sentences are correct, including the last one. Both, the html4-version and the xhtml-version are perfectly correct, in fully accordance with the html5-specification and therefore fine in html5. And, in contrast to your 1st comment, non of them has a closing tag </...> (which would be wrong). Just one of them is self-closing <... /> which in the html5-specification is allowed for void elements like link.Impatiens
Your over-verbose explanations won't convince me. If you have some authoritative source to convince me otherwise, that would go a lot farther. You were asked for a reference 6 years ago for another one of your claims (one I agree with, though--although it is vague, and could use a reference), and only provided W3schools, which everyone knows is not authoritative.Settler
go to html.spec.whatwg.org/multipage/parsing.html and search for the string "self-closing" The 2nd finding is in a note within a section about non-void-elements. This note says: "The trailing U+002F (/) in a start tag name can be used only in foreign content to specify self-closing tags. (Self-closing tags don't exist in HTML.) It is also allowed for void elements, but doesn't have any effect in this case."Impatiens
html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead also makes very clear, that for void elements (like LINK) self-closing tags are allowed. They do not cause an error. There is just a flag that has been set when the character "/" was found, and if the element is a LINK element then everything is fine. The parser just needs to acknowledge this flag. If you read more on this page, you will find, that self-closing tags throw errors in many other situations, but void elements are explicitly not on this list. Self-closing void elements are 100% correct in html5.Impatiens
C
27

HTML 4

http://www.w3.org/TR/html401/struct/links.html#edef-LINK
Start tag: required, End tag: forbidden

HTML 5

http://www.w3.org/TR/html5/document-metadata.html#the-link-element
Tag omission in text/html: No end tag.

http://www.w3.org/TR/html5/syntax.html#elements-0
"Void elements: ... link ..."
"Void elements only have a start tag; end tags must not be specified for void elements."

Criminology answered 17/10, 2014 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.