6 ms·
This is a fantastic article with some facts I didn’t know. I hadn’t thought about how the self-closing tag is effectively nothing more than a stylistic choice.
by computomatic 3y ago
This is a fantastic article with some facts I didn’t know. I hadn’t thought about how the self-closing tag is effectively nothing more than a stylistic choice.
That said, it failed to persuade me. I’d still favour the self-closing style in any style guide. At least now it would be a more informed (and better scoped) preference.
Considering two style guides:
- <tag> is an opening tag
- </tag> is a closing tag
- <tag /> is a self-closing tag
Note: it remains the responsibility of the author to know which tags are self-closing.
Seems more useful than:
- <tag> is an opening tag or a self-closing tag
- </tag> is a closing tag
Admittedly, it’s subjective. That’s why any consistent guide is better than none.
- jaffathecake 3y agoBut <tag /> isn't a self-closing tag, it only works for particular tag names, so you'd have to add all of those to your ruleset. And then, when you've done that, you can just remove the "/>" bit, since it's a redundant rule.
- pornel 3y agoThe editor of the HTML5 spec calls it a talisman. It doesn't do anything, it's there to reinforce your belief.
- TheRealPomax 3y agoBut it doesn't even "work for particular names" because the `/>` part isn't what closes the element. By the time the parser has seen the sequence `<br` it already knows which element this is, because it can only be the BR element, and has already finalized the DOM node for it because that's the rule for the BR tag. The moment it sees the opening angled bracket and tag, that node is created in an already closed state. So those extra two `/>` do, in the most literal sense possible, nothing at all. They're allowed to be there for historical reason but they literally do nothing: they're treated as bytes to be ignored and the DOM parser skips over them while it looks for the next active token in order to continue building the DOM.
- deleted 3y ago[deleted]
- lolinder 3y agoIf this were true, I'd expect this HTML: <p>A<br B</p> <p>C</p> To render as this: A B C But instead it renders as this: A C The parser treats `B<` and `p` as attributes of `br`, and determines that the `<br` tag ends with the `>` at the end of line 1. If I end the `<br` with either `/>` or `>`, it parses correctly. Which means that `/>` and `>` are both considered valid endings for the `<br` tag, but you have to have one or the other and it will keep looking until it finds one.
- phatskat 3y agoRight, I think the person you’re replying to is partially correct in that the parser gets to “<br “ and recognizes that “this is be a br tag”, but it doesn’t close because tags can have any arbitrary attributes, and the W3C says br may also contain any of the global attributes. So, the parser indeed doesn’t just stop because it knows what tag it is and then doesn’t allow any attributes, it has to keep going until it reaches a valid closing “>”.
- lolinder 3y agoIt's only redundant if you expect people to memorize your style guide. If the style guide is instead enforced by a lint rule, then having the /> be required means that no one has to memorize the list of self-closing tags. The writer of the HTML will be told off if they try to use /> on a tag that won't actually self-close, and they will also be told off if they neglect /> on a tag that self-closes. Once written, the reader knows that a tag that ends in /> closes itself, and if it ends in > then there's definitely a closing tag somewhere.
- oneeyedpigeon 3y ago> Note: it remains the responsibility of the author to know which tags are self-closing. And they should be able to get almost all of the way there by remembering one very simple rule: self-closing tags to insert something at that point in the document, open/close tags to describe the content they surround.