tags:

views:

79

answers:

3

Are XHTML style close tags valid in HTML? What I mean is, in XHTML we use <link href="style.css" type="text/css" rel="stylesheet" />. Is this valid in HTML? or should I be using <link href="style.css" type="text/css" rel="stylesheet"></link>?

+1  A: 

You should use <link> in HTML. <link/> in XHTML. If you are feeding XHTML as text/html then it really doesn't matter, because technically it's invalid HTML, unreal XHTML, HTML pretending to be XHTML but the Content-Type is text/html so not a fully fledged XHTML document.

If you feed xhtml style to HTML, it won't be valid but the browser will parse it properly.

You should remember which elements are self closing, script is not self closing and link is self closing. There's a list out there in the w3 spec somewhere.

meder
For full compatibility, make sure to add a space before the slash like <link />
jojaba
+1  A: 

They are invalid in HTML4 for the LINK element.

Start tag: required, End tag: forbidden

But XML-style tags as in your question are valid in HTML5:

Authors may optionally choose to use this same syntax for void elements in the HTML syntax as well.

Basically, HTML5 allows XML/XHTML style markup. That's the nice thing of HTML5. You aren't forced to serve XHTML as application/xml+html which would only cause IE to havoc. Just serve it as text/html with a <!DOCTYPE html> in top of the page and you're safe.

BalusC
+1  A: 

To quote the W3 validator regarding self-closing tags:

The sequence can be interpreted in at least two different ways, depending on the DOCTYPE of the document. For HTML 4.01 Strict, the '/' terminates the tag '). However, since many browsers don't interpret it this way, even in the presence of an HTML 4.01 Strict DOCTYPE, it is best to avoid it completely in pure HTML documents and reserve its use solely for those written in XHTML.

Moses
Doesn't this actually mean that they are valid in HTML 4.01?
Ameer
@Ameer: It means they are valid, *but they don't mean what you think they mean*. "Terminates the tag" means that it *ends* the tag. Which means that the following `>` is *not* part of the tag, it is part of the *content* of the element. IOW, instead of `<em>some emphasized text</em>`, you can also write `<em/some emphasized text/`. It means the same thing. As for the `link` element specifically: it's an empty element, which means that in HTML, *only* `<link>` is allowed, in XHTML, *only* `<link/>` is allowed and in HTML5 (but not XHTML5), both are allowed. `<link></link>` is never allowed.
Jörg W Mittag
@Jörg - That tells only half the story. As link is a void element, `<link/` is both valid and complete. The following `>` is character data. Since link is only allowed in 'head', the `>` will end the head element, start 'body' and the `>` will begin the first child of body. Since character data is not allowed to be a direct child of body in HTML 4.01 Strict, this is a validation failure, but since that rule doesn't apply in HTML 4.01 Transitional, `<link/>Hello` passes validation there. However no other head element can follow `<link/>` so `<link/><link/>Hello` fails. Don'tcha just love HTML!
Alohci
@Alohci: HTML is *sing* Fun, Fun, Fun, 'til her daddy takes the `<tbody>` away!
Jörg W Mittag
So, what's the final say?
Ameer
The final say is DONT use / in HTML. Using "/" in place of ">" or in combination "/>" should be avoided in HTML markup. And while you can create perfectly validated code using "/" as Jorg and Alohci mentioned, it should be considered "tricky" code and avoided as it is hard to maintain, non-standard, and quickly becomes unclear what is happening in the code.
Moses