3 ms·
Please note that using slash at the end of the tag is not supported in HTML: <link rel="stylesheet" href="base.css" /> I often see such code and it seems
by codedokode 2y ago
Please note that using slash at the end of the tag is not supported in HTML:
<link rel="stylesheet" href="base.css" />
I often see such code and it seems that people simply don't know HTML and copy random garbage they found on the Internet. In the example above, the slash is simply ignored, but in this example the code will be parsed not as intended:
<div/>
This is why you should read proper documentation or specification instead of blindly copying garbage code from Internet.
MDN link: https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags https://developer.mozilla.org/en-US/docs/Glossary/Void_eleme...
- kiitos 2y ago> Please note that using slash at the end of the tag is not supported in HTML: `<link rel="stylesheet" href="base.css" />` It is supported in HTML5. https://html.spec.whatwg.org/multipage/syntax.html#start-tags https://html.spec.whatwg.org/multipage/syntax.html#start-tag... > ... if the element is one of the void elements ... then there may be a single U+002F SOLIDUS character (/) (at the end of the element, before the closing bracket) https://html.spec.whatwg.org/multipage/syntax.html#void-elements https://html.spec.whatwg.org/multipage/syntax.html#void-elem... > Void elements: area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr -- > This is why you should read proper documentation or specification instead of blindly copying garbage code from Internet. Physician, heal thyself :)
- codedokode 2y agoNo, try writing <div/> And it will be interpreted as <div>, not as a pair of <div></div>. HTML just ignores the slash character, it doesn't support XML-style self-closing tags. HTML is not XML.
- kiitos 2y agoWe're not talking about div, we're talking about link. Self-closing tags with the trailing slash are absolutely supported by HTML5, in some circumstances.
- codedokode 2y agoNo, spec authors simply added a rule to ignore the slash because Internet was full of garbage code and they realized that it is hopeless to expect low-quality frontend developers to fix their code. There is no such thing as "self-closing element" in HTML spec (only SVG and MathML have them because they are based on XML). Quoting the spec [1]: > Then, if the element is one of the void elements, or if the element is a foreign element, then there may be a single U+002F SOLIDUS character (/), which on foreign elements marks the start tag as self-closing. On void elements, it does not mark the start tag as self-closing but instead is unnecessary and has no effect of any kind. For such void elements, it should be used only with caution — especially since, if directly preceded by an unquoted attribute value, it becomes part of the attribute value rather than being discarded by the parser. [1] https://html.spec.whatwg.org/#start-tags https://html.spec.whatwg.org/#start-tags
- andrewfromx 2y agowow codedokode for the win!
- kiitos 2y agoYes, but that clearly indicates it's a supported syntax, which contradicts what you said up-thread. That's the only point I was making.