logoalt Hacker News

jraphyesterday at 5:45 PM0 repliesview on HN

Reasons for not putting the slash in HTML:

- since the slash doesn't have any meaning in HTML, if you don't quote your attributes, you are at risk that your slash is attached to the value or your unquoted attribute: <br class=myclass/> ← uh oh, class = "myclass/"!

You can test this by visiting the following URL, and inspecting the content: data:text/html,<br class=myclass/>

Now guess what happens to the unquoted src attribute of the img tag followed by an unspaced stray slash… OTOH, you don't need to not quote the src attribute…

- it can give a false sense of correctness, one can reasonably consider that the closing effect of the slash is pure illusion and even potentially confusing.

For backward compatibility, a stray slash at the end of the start tag is ignored, not considered as an attribute that doesn't have a value, so there's argument to be made that it's still part of the syntax. You'll never have any issue if you always put a space before the slash (which most people who put the slashes do because of a silly bug in a browser that has not been relevant for a long time), or if you quote all your attributes.

I don't understand why they haven't decided to make the HTML5 parser parse <br class=myclass/> like <br class=myclass> but I guess it is what it is.