does the link element have a closing back slash or not
i.e is it
(1) <link href="css/style.css" rel="stylesheet" type="text/css" media="all" />
or
(2) <link href="css/style.css" rel="stylesheet" type="text/css" media="all" >

Dani AI

Generated

Short answer: the <link> element is a void element in HTML — it only has a start tag and must not have an end tag. In everyday HTML as served with the normal text/html serialization (what almost every website uses today), a trailing / before > is unnecessary and ignored by the parser, which is why most sites (including DaniWeb) omit it. (developer.mozilla.org)

Clarification about XHTML: if you deliberately use the XML serialization of HTML (true XHTML served with Content-Type: application/xhtml+xml), you must follow XML syntax rules and use the XML-empty form for empty elements. That is an XML requirement, not an HTML one — so the slash matters only when you are actually serving the page as XHTML/XML. (developer.mozilla.org)

A few practical points that address things raised above: was right to point at XHTML, and correctly noted “forward slash.” ’s warning about “self-closing” is worth refining: HTML has void elements (no end tag) but not XML-style self-closing semantics — the slash in HTML is decorative and can be risky (it can be parsed as part of an unquoted attribute value), so it’s safer to omit it in HTML. (developer.mozilla.org)

Quick checklist for and others: if you serve pages normally (text/html + HTML5 doctype) drop the trailing slash; if you intentionally serve application/xhtml+xml, follow XML rules and self-close empty elements; run a validator when in doubt, and avoid leaving attributes unquoted (trailing slashes can change parsing when attributes are unquoted). (github.com)

Recommended Answers

All 5 Replies

If you use a (X)HTML strict doctype then you will have to use a closing backslash, but with HTML5 doctype for example you can use the second as well.

*forward slash

coz most sites including daniweb dont include the back slash, so was a bit confused on what is which. so with backlash it is. wish they had documented it in the html specification

Indeed a forward slash (or trailing slash), of course! it has more to do with valid XML then HTML. XHTML (X stands for XML) disappeard slowly in favor of HTML5, so we don't need to be so strict anymore with the way we markup our pages.

HTML NEVER had a self closing element

XHTML required all elements to close, singleton elements were required to self close,
<img alt='' src='' /> or
<link href="css/style.css" rel="stylesheet" type="text/css" media="all" /> or
<a href=''>link text</a>
XHTML is happily dead in favour of HTML5 the elements now look like
<link rel="stylesheet" href="theme.css"> type not required
<script src="scriptfile.js"></script> type not required
<img alt='' src=''>
<a href=''>link text</a>

It is properly documented in the HTML documentation
It is not mentioned:
That is entirely proper.
it is not; never was; never will be; part of the HTML specification.
It would be somewhat strange to reference everything(anything) that is not part of the specification

'Hey look, the service manual for my car, is in the HTML spec, as "not part of HTML" ' <<attempted humour<<

commented: exactly +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.