Hacker News new | ask | show | jobs
by sureaboutthis 2646 days ago
Just because I can...

I find it interesting that he is inconsistent in his usage of <link>. Some of his markup has a closing slash and some of it doesn't. However, my main gripe, is that the HTML spec has never specified or required a closing slash. While it is allowed, putting that slash there has no meaning, it does nothing, and the spec tells browsers they are to ignore for those reasons.

When I ask why some people insist on putting it there, I get wildly different, unreasonable answers.

"It's just a bad habit I picked up."

How do you pick up a bad habit for something that does not exist?

"I do it so it's XHTML or XML compatible."

Well, you aren't serving it as either XHTML or XML and are very unlikely to do that. In addition, the rest of your HTML is probably not XHTML or XML compatible on top of that.

"The spec does allow it."

It allows it, as I always tell them, due to backwards compatibility with (X)HTML uses in the past, but it also says you're wasting your time and effort. See my comment at the top.

To end this, no HTML specification in the history of the universe has ever stated you need to or should put a closing slash on <link> or <img> or <input> or any other HTML tag and there is no example of such usage anywhere in said documentation either.

5 comments

HTML doesn't require a closing slash, but without it needs to be context-aware and maintain a whitelist of tags that are self closing (one effect of this is that custom elements can never be self closing).

It's not really surprising that a developer would gravitate towards an encoding that is not dependent on maintaining a whitelist of tags, because that is the solution that intuitively feels more "correct".

Completely agree. If you want a generic parser for HTML-like tags, templates or anything where the context isn't a full HTML page, you definitely want syntactically correct/valid markup.
Too bad the closing slash doesn't do anything. <script src="..." /> will break horribly if you try to use it. Everything still depends on the whitelist.
If one needs a closing slash to be reminded which of the five or so tags are self closing, one has far more issues than needing a closing slash.
My point is that not that remembering things is hard, it's that it feels like a hack. Developers gravitate to solutions that feel elegant.
Putting the slash in, as I said, is there for backwards compatibility only, not elegance, and it could be removed at any time since this backwards compatibility issue no longer exists, or is of minor consequence nowadays.

It is far, far better to read and follow the standard specification and not follow a whim that makes you feel good. These are the things that will burn you first.

Issues like being a parser?
prettier-js puts them there. I don’t know why, and I don’t care, so I leave them there.
I do it because it feels right and logical. If something has a start, it should have an end. If you’re making a <div> that is only going to be used as a spacer with styling, you wouldn’t just do a <div> and leave it hanging.
You are talking about closing tags. I'm talking about self closing tags.

One should never use an element as a spacer tag. That's 1990s styling at best.

You are talking about closing tags. I'm talking about self closing tags.

I am, too. My point is that in my mind it feels right to close all tags, either with a closing tag or self-closing it with a /.

One should never use an element as a spacer tag.

One should never use “never” in programming. You are incapable of knowing every possible circumstance someone else is working in.

That's 1990s styling at best.

I built and sold several web sites in the 90’s. That’s not how it was done. It was worse.

You are putting the slash in because it feels good but the specification does not do that. You are playing with fire.

Despite your circumstance, the reason for spacer divs, and other such things, are relics of the past and such reasons no longer exist. Using them now is beyond silly.

personally, I don't care one way or the other, so I always leave them whichever was my most recently used linter/IDE's default preference & basically ignore it beyond that

the spec says to ignore it either way & I think my way aligns w/ that (while being nearly optimal in terms of effort efficiency)

Some template engines require well-formed XML (like Thymeleaf, in certain configurations)
And that is XML, not HTML, and never the twain shall meet.

The hilarity of your statement is all the disdain people had for XML and the W3C's push for its usage over HTML. Now everyone praises WHATWG for breaking away from the W3C over this and, here we are, with XML lookalikes and XML usage everywhere.