📝 The case against self-closing tags in HTML
At best, they're a relic from the past, and at worst, they're actively misleading.
https://jakearchibald.com/2023/against-self-closing-tags-in-html/
📝 The case against self-closing tags in HTML
At best, they're a relic from the past, and at worst, they're actively misleading.
https://jakearchibald.com/2023/against-self-closing-tags-in-html/
@jaffathecake As I did mention “parse” in my comment, I think I need to clarify that I meant only human readability, not the actual HTML parser :)
That said, I think the best way forward would be for tools like prettier to fix the `<div />` cases (and browsers to complain about these in one way or another), just so we would handle the actual problems and won't have to argue about the personal preferences.
@chrismardell @kizu sure, but only because you've internalised that /> means "closed", even though it doesn't.
You could just as easily internalise that > at the end of an img tag means "closed".
@joshhunt @keithamus I think people care about it because it was very important for strict XHTML.
With HTML 5, the closing tags are no longer necessary, and there’s no longer any XML serialization of HTML, but old habits die hard.
@jaffathecake <br> is just too convenient and easy example.
Let's better talk about more dangerous HTML "loose mode" mess:
https://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#optional-tags
"An li element's end tag may be omitted if the li element is immediately followed by another li element or if there is no more content in the parent element."
Such tricky conditions is a nightmare from SAX parsers perspective. HTML parser becomes a complex look-ahead-go-back-just-in-case SAX deviation with vaguely implied logic.
@jaffathecake I have never liked self-closing behaviour and a "self closing tag" is a neat way for me to know "I opened the thing, and the thing is now closed".
The problem is there shouldn't BE self closing tags.
Open a tag, close a tag. Even if it has no content.
Consistency matters to my brain, and a list of separate tags you don't need to close hurts it.
@jaffathecake I suspect that's why this persists.
"I open a thing, I close the thing".
It's just a good pattern. And HTML's design sure doesn't need that - but that's the problem. We want symmetry. Conceptually.
@jaffathecake I'm in "to me self-closing looks pretty" clan 😄
Also, you missed one more case.
I'm working with Vue a lot. In Vue component can have a slot and don't have it.
I.e. `<error />` could just print a default error message and `<error>Error message</error>` would display a custom error message.
I'm for consistency and for Vue templates self-closing HTML tags is good for consistency.
Perfectly valid case IMO, since you bring up JSX.
@jaffathecake 🙋 I was absolutely one of those "always explicitly close empty tags" people because it meant you didn't need to memorize which tags were self closing.
But learning now that the `/>` doesn't actually do anything and still relies on self-closing behavior has made me repent my sinful ways. 🙏
@develwithoutacause @jaffathecake yeah, it's purely aesthetic. Like you note, it's only for humans.
I am inconsistent, I admit. I'll use them when working with Java devs, but not on pure Frontend code or PHP code. And otherwise just trust Prettier to clean up after me.
@fnordius @develwithoutacause they're only for humans… except in the cases where they aren't 😀
That's why I'd prefer to only use them when they effect the tree.
@jaffathecake you make some very pragmatic points there.
But as someone who started web development in the 2000s, I know web tech can be summarised as:
* 1990s - old fashioned & inconsistent
* 2000s - new & exciting
* 2010s - overly complicated bloatware
* 2020s - scary & unnatural
Therefore, I propose we only support 2 formats on the web: XHTML and RDF.
> I wouldn't try and write JSON so it can be parsed by a YAML parser, so I don't see why I'd do the same with HTML and XML.
Joke's on you here! Because it doesn't matter how you format a valid json document, a YAML parser will parse it regardless 😉
@jaffathecake completely disagree. HTML treating some tags as special cases that don’t need to be closed is atrocious, and it ignoring /> and requiring an opening and closing tag to create an empty element is doubly so. HTML should treat all tags the same and tags with no content should have an option to self-close instead of pointlessly doubling the tag and creating noise.
Additionally, supporting /> in plain HTML comes with no downside as everywhere it appears it’s intended to self-close the tag, and even improves resilience for cases where people used to XML and XHTML didn’t know it gets ignored and used it.
Your arguments rely on the status quo of the decisions made by the W3C standard body. That body, at least when it comes to HTML, seems to really like proving itself incompetent since its inception up to today. They’re like the Jim Cramer of standards, whatever they decided, the exact opposite is most likely a more sensible choice.
@jaffathecake true, sorry, in that context I agree even if I hate that it’s true. There’s just something about front-end web standards always managing to choose the worst possible design that really grinds my gears.
And yeah, I forgot it’s not W3C which is really nice since I generally respect W3C and HTML was a stain on them for me.
@jaffathecake okay you really convinced me here
… that I hate web development xD
@jaffathecake Thanks for the write up! I liked self-closing tags aesthetically (maybe a psychological about there not being a tag open without it being explicitly closed [1]) but after reading through this post I think I'll be avoiding them in the future.
@jaffathecake I actually like self-closing tags, it keep things neat.
I did read the article and aside that it can be misleading for people that would assume a consistency of JSX, I don't agree for the cases against, even in responses of this post.
1. I don't care about JSX and what assumptions people could possibly make based on their stack.
This is the web, content is king, HTML first as foundation, everything else later.
2. Prettier and other compilation tools are not my concern.
3. Only tags with content have closing tags, which make sense.
4. I reckon all tags should close.
5. Therefore self-closing tags for elements that do not have content but only value through attributes make sense.
It's clean, strict, consistent, helping juniors to be familiarised with tags and their difference and IT IS PRETTY !!! 😂
@jaffathecake Uh... any JSON is a valid YAML, so "I wouldn't try and write JSON so it can be parsed by a YAML parser" doesn't make sense ☺️.
Great post! Learned a lot about how HTML parses self-closing tags, ie it ignores them. I did not know that, and I suspect most people don't either. Given that, I agree with what you said about tools not outputting them.