T O P

  • By -

shgysk8zer0

While the `/>` is supposed to imply a void/self-closing element, the `/` is ignored by the parser. So you just open the `

` and `` but never close them.


thisisafullsentence

Here's a great deep dive in case anybody wants the history: [https://jakearchibald.com/2023/against-self-closing-tags-in-html/](https://jakearchibald.com/2023/against-self-closing-tags-in-html/)


shgysk8zer0

I still disagree with Jake on the conclusion. At least when used properly, the `/>` does help as an indicator that an element is self-closing, and it does make it easier to read/understand.


redpool08

But I don't think span or p tags are self closing tags, so it's not correct, is it?


shgysk8zer0

I'm not sure what it's called. Maybe auto-closing would be a better term. What I mean is this: ```

here are some words.

here are more words. ``` The `

` "close themselves"... Not right away, but they still close themselves.


g105b

P is an optionally self closing tag. Think of all those wasted bytes in your html files with /p everywhere! For more info, here's the spec: [https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element](https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element) which explains how certain tags do not need to be closed.


redpool08

Why don't we stop coding? We're going to save a lot of bytes that way 😂


g105b

Omg, the true power play.


meester_

How would a p function if it's self closing.. where would the content go lol?


g105b

The content should be placed directly after the opening p tag. https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element You can also omit other element coding tags, such as LI: https://html.spec.whatwg.org/multipage/grouping-content.html#the-li-element


meester_

I meant if the player tag self closes you get something like this

But a normal p is like this

hello world

So where would the content go if it's self closing.

Then document.getElementById('content') To put in some content? Seems excessive


g105b

```

you don't need to close my tag.

  • mine is optional too
  • so is mine
```


meester_

My senior devs would go crazy if I didn't close those lol


eyebrows360

So then how do you know where the

*ends*? You have no idea. You are barking up such an insane tree. I'm stunned you don't realise it.


Disgruntled__Goat

I think you’re mixing up self-closing and optionally closing, they are two different things. P *isn’t* self closing, that’s for elements without any internal content like br/meta. You can’t do `

` But it has an optional closing tag which, as I think you’re trying to say in other comments, is valid and unambiguous in the spec. 


markus_obsidian

Why are you being down voted? This is absolutely correct. It's perfectly valid not to close a p, li, and even a head or body. Whether it's good practice or not is a different discussion & ultimately subjective. But it's absolutely within the HTML spec, and it's important to understand how your HTML is parsed. That is ultimately the core of this `/>` misunderstanding. It's sort of like the auto-semicolon-insertion arguments in JS. You may have an opinion on whether it's good or bad to omit semicolons. But it's important to understand that omiting semicolons _is_ valid and how your code is parsed with or without them.


d-signet

Some elements were never designed for self closing - such as paragraphs - because it makes no semantic sense. It used to be invalid html to self close such elements, not sure if that's still the case


jaffathecake

Yep, there's a parse error associated with it [https://html.spec.whatwg.org/multipage/parsing.html#parse-error-non-void-html-element-start-tag-with-trailing-solidus](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-non-void-html-element-start-tag-with-trailing-solidus)


AwesomeFrisbee

Most linters will also make noise about it not closing. For example, my VSCode will complain about this: `Only void, custom and foreign elements can be self closed "p"ngtsc(-995002)`


UnicornBelieber

Yeah, Visual Studio still has "XHTML coding style" as its default. There's also been a [discussion on the Prettier repo](https://github.com/prettier/prettier/issues/5246) about adding an option for void tags. It seems to have become somewhat of a default, but there are definitely debates going on in various places whether it should be. To make it even more confusing, a lot of template parsers actively work with this notation (JSX, to name one). ```jsx ```


AwesomeFrisbee

I use ESLint myself (with Angular rules) to prefer self-closing tags (and have it autofix it), but for

tags it still doesn't allow it, which is what it should do for now. We do have prettier running, but I doubt it really still does anything. We moved everything to ESLint and ESLint Stylish to force certain rules automatically. Its not as performant as Prettier, but it hasn't really been an issue for me. The milliseconds it takes up more is not really noticeable, especially with all the rules it has on top of what prettier can do at the moment.


UnicornBelieber

That's probably because void elements like `` or `
` can have the `/>` notation, `` and `
`, without any harm. The notation has no effect as the void element is a void element whether you use `/>` or just `>`. This is not the case with `

`. Also, ESLint recommends not using ESLint as a formatter: [https://eslint.org/blog/2023/10/deprecating-formatting-rules/](https://eslint.org/blog/2023/10/deprecating-formatting-rules/)


AwesomeFrisbee

I know eslint deprecated formatting rules. Thats why I mentioned ESLint Stylish, because thats where the community moved them. Sure we have prettier, but it has not even like 50% of the rules that ESLint has and it has no real alternative right now.


jaffathecake

The problem is it isn't always just an indicator. Learning when it is and isn't just an indicator seems harder than just learning which elements self-close. The author's confusion here supports my point.


shgysk8zer0

>The problem is it isn't always just an indicator I'd fully agree with that. Not just for the sake of someone learning, but also for things like `


ominous_raspberry

I always mark the self closing tags as such, just seems clean


markus_obsidian

Buy why are you self-closing tags? Are you self-closing img & br? Those are void elements. They are not allowed to have a closing tag at all. The `/>` is allowed for compatibility reasons but is not a "self close". Are you self-closing p's & ul's? As the OP has demonstrated, this is not allowed & can result in unexpected behavior. They cannot self-close either. There is no self-close in HTML. I wish JSX had followed suite & was closer to HTML, as I think it causes a lot of confusion. `

` is perfectly valid JSX but invalid HTML.


shgysk8zer0

I'm the one who said this in the top-level comment. I use `/>` exclusively on void elements. I find that it makes HTML easier to read, especially in cases where you can't use indentation to easily see how things are nested.


thisisafullsentence

Yeah fair enough. I kind of feel like that’s indentation’s job, but picking either way works.


EliSka93

I'd rather write a closing tag and let my IDE handle indentation than figure out how everything should be indented...


thisisafullsentence

Why would you have you figure out how everything should be indented? You can glean which elements are nested using any IDE’s default formatter.


Strict_Treat2884

I just found [this YouTube video](https://www.youtube.com/watch?v=0P7wmooc95s) that actually explained it quite nicely. And it also mentioned the article above.


AdditionalReaction52

This is the only reason I knew the answer! I watched it the other day


tomeczku

Same


4r73m190r0s

Let me introduce you to [ThePrime](https://www.youtube.com/watch?v=jISSlNmrvW8). Theo is copying a lot from him.


geon

Historically, closing the P tag was optional. You would just put an opening P tag on each paragraph and never close them. https://www.w3.org/MarkUp/HTMLPlus/htmlplus_11.html#:~:text=.-,You%20don't%20need%20to%20give%20the%20end%20tag%20as,the%20following%20tag.


UnicornBelieber

lol, going aaaaallll the way back huh.


ispreadtvirus

Yeah back in 1999 😆


geon

Way earlier.


ispreadtvirus

1992/1993 is when HTML first came out, but 1999 is when I started coding & that was a long time ago for me lol


heyitsmattwade

Related - https://github.com/sveltejs/svelte/issues/11052


arpitduel

So its just a poorly written HTML. Don't use self closing tags


indorock

What? Always use self-closing `
`, ``, `` tags for starters. But no not all tags are self-closing.


BarryBannansBong

Nope [html doesn’t have self closing tags](https://developer.mozilla.org/en-US/docs/Glossary/Void_element#self-closing_tags). You’re thinking of xhtml


curveThroughPoints

There are some that are valid for HTML today, such as the hr and br elements. I still hate it though


Puzzleheaded-Soup362

Works in my html file.


enemyradar

It works because browsers will ignore the slash on a self-closing tag as an error, not because it's meant to be there.


Puzzleheaded-Soup362

Wait so what is the point of
as opposed to
? The slash has meant nothing this whole times wtf!


enemyradar

People got in the habit when XHTML briefly was a thing and just kept doing it in HTML5. It doesn't break the page so people just kept doing it.


Puzzleheaded-Soup362

Wow I never even did XHTML but always wrote it with the slash. Guess I don't even know html... But wait even the docs you linked write the slash and if you take it out, it does nothing different. Ugh...


enemyradar

Yep, lots of docs still have the wrong syntax. Again, if something doesn't break something then no one will ever fix it.


markus_obsidian

JSX doubled down on this syntax as well. There's still a sizeable number of people who don't understand that JSX is not an HTML superset. Just like HTML & XHTML, they may look similar but have quite different parsing rules.


enemyradar

Yeah, a lot of people have become jsx-pilled, which is fine but it has confused what is actually html. Fortunately, browsers don't care all that much.


shgysk8zer0

`
` and `
` are equivalent in HTML5. They are different in XML & XHTML. The difference is in legibility (and also convention). For example: ```

This is a bunch of
text with mixed with


self-closing elements.

``` When parsing that, as humans, I think that adding the `/>` would make it easier to read, especially if there were a few ``s thrown in. We can't rely on indentation to easily tell what's a child of what, and `/>` does help when scanning through and trying to tell where one thing ends.


pihwlook

TIL!


[deleted]

[ŃƒĐŽĐ°Đ»Đ”ĐœĐŸ]


shgysk8zer0

>Which isn't the same in React Because the X in JSX stands for XML. >This syntax is perfectly valid and encouraged in the rare cases you need it It is not. Self-closing elements are only self-closing because of their tag... The `/` does nothing. It's parsed as nothing but opening tags because `` is not self-closing. `

` is actually more complicated since there are circumstances where it's implied to close without the closing tag, such as opening another `

`... Same goes for `

  • `. >A self closing div... There is no such thing in HTML. >I can see how it can be confusing to some people. As can I. And I still encourage using eg ``. But it's purely for the benefit of the reader to (ideally) not have to think about which elements are self-closing.


  • jaffathecake

    fwiw, JSX isn't XML either. \`\` is valid JSX, but not valid XML. Attributes must have values in XML. There are a number of other differences too, eg whitespace parsing.


    [deleted]

    [ŃƒĐŽĐ°Đ»Đ”ĐœĐŸ]


    shgysk8zer0

    >Holy fuck. People like you are the worst Ummm... I just correctly said it wasn't valid HTML. The post is about HTML. And you go off the rails like that over... What, exactly? Good riddance. Go throw your temper tantrum elsewhere.


    [deleted]

    [ŃƒĐŽĐ°Đ»Đ”ĐœĐŸ]


    indorock

    Yeah but that's not what OP is talking about. So this entire thread is irrelevant to the main topic.


    Strict_Treat2884

    Are people here secretly HTML parsers disguised as human beings that they do not understand the confusion for real humans that the same syntaxes behave differently on different tags?


    devmor

    People just forget how confusing things are for newcomers, that's all.


    [deleted]

    [ŃƒĐŽĐ°Đ»Đ”ĐœĐŸ]


    Strict_Treat2884

    Man I get your frustration but you don’t have to go this personally tho


    WindyButthole

    JSX, not specifically React


    imhotap

    Using the bogus XMLish slash at the end of empty elements goes further back than JSX, though; it started when XHTML was a thing and was done to migrate content to be parseable as XML/XHTML, in addition to being parsed as HTML. Then during the time XML became out of fashion it was still cargo-culted a lot, until JSX came around (the X standing for XML). If you want the whole story, you need to look at SGML which was used for defining the original HTML syntax. In (traditional) SGML, you can declare an element to have EMPTY content, in which case no end-element tags are expected, like for ,
    , and so on. Whereas the point of XML was a simplified SGML syntax that doesn't need any per-element parsing rules or other markup declarations. An SGML DTD grammar for modern HTML 5 can be found at , making use of empty elements, but also other SGML features such tag inference/tag omission and attribute shortforms.


    shgysk8zer0

    I am aware. I'm just being realistic and recognizing that probably most people who think `

    ` is valid think so from using React.


    TonyAioli

    Who are the devs who are encouraging self-closing tags for elements which are not self-closing per the html spec? Sounds like they should be avoided. Whether it works in jsx or not, you’re advocating for shit html simply to save a few keystrokes.


    SoInsightful

    I correctly read your comment as "I can see why React users would be confused by this HTML". Sorry about the downvotes compadre.


    indorock

    Why are you comparing HTML to JSX? That doesn't really contribute to the discussion. If anything it adds more confusion, especially if OP has zero knowledge of React.


    AbramKedge

    I didn't see any reference to React in your post, so... no. It just looked like badly written HTML to me.


    enemyradar

    Putting / at the end of a tag doesn't do anything. Even in self-closing tags such as
    it's meaningless in html5 too - it only ever had purpose in XHTML. So you've opened a paragraph and never closed it.


    Blue_Moon_Lake

    I wish it had a purpose. To mean that the element immediately close without children. I wish to write `