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

3 Answers 3

3

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.

0
1

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.

1
  • For full compatibility, make sure to add a space before the slash like <link />
    – tidwall
    Sep 20, 2010 at 22:21
1

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.

5
  • Doesn't this actually mean that they are valid in HTML 4.01?
    – Ameer
    Sep 21, 2010 at 0:32
  • 1
    @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. Sep 21, 2010 at 1:18
  • @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
    Sep 21, 2010 at 8:02
  • 1
    @Alohci: HTML is sing Fun, Fun, Fun, 'til her daddy takes the <tbody> away! Sep 21, 2010 at 11:06
  • 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
    Sep 22, 2010 at 15:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.