Don't forget these tags to make HTML work like you expect

blog.jim-nielsen.com

132 points by FromTheArchives 4 hours ago


aragonite - an hour ago

Fun fact: both HN and (no doubt not coincidentally) paulgraham.com ship no DOCTYPE and are rendered in Quirks Mode. You can see this in devtools by evaluating `document.compatMode`.

I ran into this because I have a little userscript I inject everywhere that helps me copy text in hovered elements (not just links). It does:

[...document.querySelectorAll(":hover")].at(-1)

to grab the innermost hovered element. It works fine on standards-mode pages, but it's flaky on quirks-mode pages.

Question: is there any straightforward & clean way as a user to force a quirks-mode page to render in standards mode? I know you can do something like:

document.write("<!DOCTYPE html>" + document.documentElement.innerHTML);

but that blows away the entire document & introduces a ton of problems. Is there a cleaner trick?

dugmartin - an hour ago

I know this was a joke:

   <div id="root"></div>
   <script src="bundle.js"></script>
but I feel there is a last tag missing:

   <main>...</main>
that will ensure screenreaders skip all your page "chrome" and make life much easier for a lot of folks. As a bonus mark any navigation elements inside main using <nav> (or role="navigation").
ilaksh - 2 hours ago

Anyone else prefer to use web components without bundling?

I probably should not admit this, but I have been using Lit Elements with raw JavaScript code. Because I stopped using autocomplete awhile ago.

I guess not using TypeScript at this point is basically the equivalent for many people these days of saying that I use punch cards.

theandrewbailey - 4 hours ago

I often reach for the HTML5 boilerplate for things like this:

https://github.com/h5bp/html5-boilerplate/blob/main/dist/ind...

chrismorgan - 2 hours ago

> <html lange="en">

s/lange/lang/

> <meta name="viewport" content="width=device-width,initial-scale=1.0">

Don’t need the “.0”. In fact, the atrocious incomplete spec of this stuff <https://www.w3.org/TR/css-viewport-1/> specifies using strtod to parse the number, which is locale dependent, so in theory on a locale that uses a different decimal separator (e.g. French), the “.0” will be ignored.

I have yet to test whether <meta name="viewport" content="width=device-width,initial-scale=1.5"> misbehaves (parsing as 1 instead of 1½) with LC_NUMERIC=fr_FR.UTF-8 on any user agents.

Aransentin - 2 hours ago

Note that <html> and <body> auto-close and don't need to be terminated.

Also, wrapping the <head> tags in an actual <head></head> is optional.

You also don't need the quotes as long the attribute doesn't have spaces or the like; <html lang=en> is OK.

(kind of pointless as the average website fetches a bazillion bytes of javascript for every page load nowadays, but sometimes slimming things down as much as possible can be fun and satisfying)

jraph - an hour ago

> <!doctype html> is what you want for consistent rendering. Or <!DOCTYPE HTML> if you prefer writing markup like it’s 1998. Or even <!doCTypE HTml> if you eschew all societal norms. It’s case-insensitive so they’ll all work.

And <!DOCTYPE html> if you want polyglot (X)HTML.

lapcat - an hour ago

  <meta name="viewport" content="width=device-width,initial-scale=1.0">
width=device-width is actually redundant and cargo culting. All you need is initial-scale. I explain in a bit more detail here: https://news.ycombinator.com/item?id=36112889
isolay - 2 hours ago

The "without meta utf-8" part of course depends on your browser's default encoding.

irarelycomment - an hour ago

[dead]

hlava - 2 hours ago

It's 2025, the end of it. Is this really necessary to share?