TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Write HTML Right

263 pointsby aparks517almost 3 years ago

38 comments

shaknaalmost 3 years ago
Whilst the spec certainly allows you to ignore closing of a whole range of elements, it&#x27;s not necessarily the wisest of choices to make. The parser does actually get slower when you fail to close your tags in my experience.<p>Unscientific stats from a recent project where I noticed it:<p>+ Document is about 50,000 words in size. About 150 words to a paragraph element, on average.<p>+ Converting the entire thing to self-closing p elements added an overhead of about 120ms in Firefox on Linux, before initial render.<p>+ Converting the entire thing to self-closing p elements added an overhead of about 480ms in Chrome on Linux, before initial render.<p>+ Converting the entire thing to self-closing p elements added an overhead of about 400ms in Firefox on Android, before initial render.<p>+ Converting the entire thing to self-closing p elements added an overhead of about 560ms in Chrome on Android, before initial render.<p>+ The time differences appeared to be linearly increasing, as the document grew from 20,000 to 50,000 words.<p>+ Curiously, Quirks Mode also increased the load times by about 250ms on Firefox and 150ms on Chrome. (Tried it just because I was surprised at the massive overhead of removing&#x2F;adding the tag endings.)<p>The most common place this was going to be opened was Chrome on Android, and a whopping half-second slower to first render is going to be noticeable to the end user. For some prettier mark up.<p>Whilst you can debate whether that increased latency actually affects the user, a decreased latency will always make people smile more. So including the end tags is a no-brainer. Feel free to write it without them - but you _might_ consider whether your target is appropriate for you to generate them before you serve up the content.
评论 #31698313 未加载
评论 #31697790 未加载
评论 #31696699 未加载
评论 #31697346 未加载
评论 #31697458 未加载
评论 #31700565 未加载
myfonjalmost 3 years ago
Although, as the article correctly points out, omitting the HTML tag is technically fine, there is one rather important argument for its inclusion: it can and should have a LANG attribute:<p><pre><code> &lt;html lang=en-GB&gt; </code></pre> It&#x27;s not verbose after all, and IIUC may be omitted if and only if the document is served with corresponding information in `Content-Language:` HTTP header, but nasty (or rather annoying) things may happen if that fails [1], so when it comes to &quot;right HTML&quot;, following this advice sounds reasonable.<p>[1] <a href="https:&#x2F;&#x2F;adrianroselli.com&#x2F;2015&#x2F;01&#x2F;on-use-of-lang-attribute.html" rel="nofollow">https:&#x2F;&#x2F;adrianroselli.com&#x2F;2015&#x2F;01&#x2F;on-use-of-lang-attribute.h...</a>
timw4mailalmost 3 years ago
No thanks. With the full markup you can see where things end, not just where they start.<p>I think this is similar to semicolons in Javascript: with semicolons at the end of each statement there is no ambiguity, but if you do not have semicolons, you have to know about edge cases, like if a line starts with a square bracket or paren.
评论 #31700179 未加载
评论 #31697613 未加载
评论 #31700020 未加载
评论 #31699927 未加载
robgibbonsalmost 3 years ago
This works for blog posts, where the body of the document is one long block of paragraphs, but I suspect this style would quickly become untenable for complex apps. Indentation _is_ information, which is lost here.
评论 #31696532 未加载
评论 #31695500 未加载
评论 #31699662 未加载
评论 #31695440 未加载
评论 #31695496 未加载
评论 #31695766 未加载
btrettelalmost 3 years ago
Regarding writing &quot;one-sentence-per-line&quot;, I&#x27;ve noticed that style before in LaTeX. While I don&#x27;t use that style, one advantage that I like is the ability to include comments on the sentence level in LaTeX.<p>So instead of this:<p><pre><code> First sentence. Second sentence. % Comment on first sentence. </code></pre> I can write:<p><pre><code> First sentence. % Comment on first sentence. Second sentence. </code></pre> (Of course, one could define a new TeX macro that doesn&#x27;t display anything to add comments anywhere in-line. That&#x27;s not as readable, though.)<p>I&#x27;ve also read that one-sentence-per-line works better with diff programs, but I haven&#x27;t had any problems with the program meld, so this isn&#x27;t convincing to me. The advantage the linked article mentions in terms of rearranging sentences also is worth considering, though I haven&#x27;t found the normal way to be that bad so I&#x27;m not convinced by that either.<p>Some other links on this coding&#x2F;writing style:<p><a href="https:&#x2F;&#x2F;rhodesmill.org&#x2F;brandon&#x2F;2012&#x2F;one-sentence-per-line&#x2F;" rel="nofollow">https:&#x2F;&#x2F;rhodesmill.org&#x2F;brandon&#x2F;2012&#x2F;one-sentence-per-line&#x2F;</a><p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=4642395" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=4642395</a><p><a href="http:&#x2F;&#x2F;www.uvm.edu&#x2F;pdodds&#x2F;writings&#x2F;2015-05-13better-writing-editing-thinking-with-line-breaks&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.uvm.edu&#x2F;pdodds&#x2F;writings&#x2F;2015-05-13better-writing-...</a>
评论 #31697381 未加载
buzzy_hackeralmost 3 years ago
&gt; A few years ago, I found out I&#x27;d been tying my shoes wrong for my entire life. I thought laces came undone easily and didn&#x27;t usually look very good. At least that&#x27;s how mine were, and I never paid much attention to anyone else&#x27;s. It took a couple of weeks to re-train my hands but now I have bows in my laces that look good and rarely come undone.<p>I’m equally interested in this as the HTML. Any clue what the author is referring to?
评论 #31696737 未加载
评论 #31696789 未加载
评论 #31696786 未加载
评论 #31696807 未加载
评论 #31696968 未加载
评论 #31700210 未加载
kuschkualmost 3 years ago
I appreciate that this blog post itself is written in the exact same style! I really miss being able to read the view-source: version of websites easily, but this blog post does it well :)
评论 #31696451 未加载
siversalmost 3 years ago
Thanks to Aaron for posting this. Such a great reminder.<p>Anyone interested in this subject, check out a series of three very tiny books called “UPGRADE YOUR HTML” by Jens Oliver Meiert.<p>They give great step-by-step examples for eliminating optional tags and attributes, reducing HTML to its cleanest simplest valid form. The author is a super-expert in this specific subject, working with Google and W3C on this. His bio here: <a href="https:&#x2F;&#x2F;meiert.com&#x2F;en&#x2F;biography&#x2F;" rel="nofollow">https:&#x2F;&#x2F;meiert.com&#x2F;en&#x2F;biography&#x2F;</a><p>From LeanPub: <a href="https:&#x2F;&#x2F;leanpub.com&#x2F;b&#x2F;upgrade-your-html-123" rel="nofollow">https:&#x2F;&#x2F;leanpub.com&#x2F;b&#x2F;upgrade-your-html-123</a><p>From Amazon: <a href="https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;B08NP4GXY2&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;B08NP4GXY2&#x2F;</a>
评论 #31698613 未加载
gildasalmost 3 years ago
This is how SingleFile writes HTML by default :). However, it is also the most duplicated issue in the tracker.
评论 #31696166 未加载
评论 #31695761 未加载
iostream24almost 3 years ago
At this point you are better off making a DSL that compiles to html.<p>- it will be possible to be consistent with closing tags or not<p>- you can do other arbitrary things to improve your working experience with it<p>Ever tried Slang styled templates?
zeven7almost 3 years ago
I like this idea. As someone who argued vehemently for XHTML a couple decades ago (even wrote a fair amount of XSLT in those XML-crazed days), who&#x27;s been wandering between different levels of &quot;how strict should I be?&quot; since that time, this article marks the step of my journey where I feel like I can really embrace the goodness that SGML has to offer for the first time. So thank you. This article has changed me.
sylwarealmost 3 years ago
Regarding tables, there is one trick: size of borders are actually weighted semantic separators, and should be in HTML, not in CSS.
评论 #31695360 未加载
dasil003almost 3 years ago
I like the aesthetic though I&#x27;m not sure how sustainable it is beyond basic content documents. On a side note though, I clicked around and big props to Aaron on the lofi.limo project, this is very cool.
评论 #31696533 未加载
meksteralmost 3 years ago
HTML can&#x27;t be fixed with a small trick like that.<p>Just use templating engine like Pug and get away with most of the annoyances.<p>It&#x27;s concise about what part of the text is covered by a certain tag due to forced indentation, not to mention you&#x27;ll never need to close any tag and you never write &quot;class=&quot; but are all turned into CSS selector notation among many other tricks.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;pugjs&#x2F;pug#syntax" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pugjs&#x2F;pug#syntax</a><p>Unless the HTML I&#x27;m composing will be touched by people like designers who would get scared of new syntax, in which case I&#x27;ll use Twig or Nunjucks, I&#x27;ll never write plain HTML for myself.<p>There&#x27;s also a very solid implementation in PHP as well.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;pug-php&#x2F;pug" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pug-php&#x2F;pug</a><p>You can either let server side (node.js or PHP) compile that on demand or let your editors compile them as you edit if you&#x27;re working on a static file.<p>I really think the language humans write should deviate from the language the runtimes understand to get all the convenience while never breaking how runtimes&#x2F;crawlers interpret your output. Same goes for Stylus against CSS.
account42almost 3 years ago
&gt; However, any content which cannot go in a p element (most other block-display elements, for example) implies the end of its content, so we can usually leave off the end tag.<p>Note however that this means that the whitespace between paragraphs will be part of the paragraph which can be annoying if someone tries to copy the text on your website and gets an additional space after each paragraph which wouldn&#x27;t have happened if you explicitly closed the &lt;&#x2F;p&gt; directly after the text.<p>Also, you should keep the opening &lt;html&gt; and specify the language of your document even for english since e.g. automatic hyphenation does not work if you don&#x27;t specify a language.<p>Otherwise really like this condensed HTML style and have recently converted my personal website to it.
andrew_almost 3 years ago
The lack of closing tags is giving me severe anxiety. I know it&#x27;s valid non-xml syntax but all the hairs on my neck are at attention.
评论 #31696936 未加载
评论 #31698003 未加载
jacobsenscottalmost 3 years ago
If you must write html by hand this seems nice. But I would never actually write html by hand anymore. For most web apps you write more tags than text. I love slim because it was designed with that in mind. There is no overhead to writing tags, and just a little for writing text. Which is the right way to go for web apps.
spread_lovealmost 3 years ago
omitting &lt;html&gt; works fine in browsers but breaks a lot of other developer tooling in my experience. It&#x27;s nice to save 6B I guess, but compared to the behemoth webapp it&#x27;s wrapping it&#x27;s not much of an optimization.
JasonFruitalmost 3 years ago
Why does it matter? A good HTML editor ought to be able to take in HTML, display it and edit it according to the user&#x27;s preferences, and save it in a size-minimizing way. Why should we have to choose only one way?
movedxalmost 3 years ago
Author: write HTML right Me: this green on black background is terrible to read, I&#x27;ll use reader mode Chrome: this author did not write their HTML correctly, so there is no reader mode available<p>How ironic.
评论 #31700229 未加载
评论 #31700062 未加载
评论 #31705329 未加载
moreatialmost 3 years ago
Is there a tool to convert an existing HTML document into this style? E.g. strip out optional closing tags, without doing full minimisation&#x2F;whitespace stripping.
DustinBrettalmost 3 years ago
I&#x27;ve been using <a href="https:&#x2F;&#x2F;github.com&#x2F;terser&#x2F;html-minifier-terser" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;terser&#x2F;html-minifier-terser</a> to get this kind of HTML for my personal site for a while. It passes W3C so I&#x27;m happy.<p>After reading the connected blog post <a href="http:&#x2F;&#x2F;perfectionkills.com&#x2F;experimenting-with-html-minifier&#x2F;" rel="nofollow">http:&#x2F;&#x2F;perfectionkills.com&#x2F;experimenting-with-html-minifier&#x2F;</a>
epolanskialmost 3 years ago
Slightly off topic but I&#x27;d like to point out that paragraphs in HTML are grouping not textual elements. They are like divs or headers, not like span or b.<p>They are mistakenly and traditionally associated with literature-type paragraphs but that is not correct. You generally use them in forms to split different groups or inputs, that has nothing with paragraphs of a written form and even less with textual paragraphs.<p>I think there is really a lot of confusion about them in this whole thread.
评论 #31701271 未加载
layer8almost 3 years ago
&gt; gator<p>What do they mean here?
评论 #31696403 未加载
nhooyralmost 3 years ago
&gt; It used to be the case that URL parsers would remove newlines and tabs, so we could split long URLs across lines and even format their query parameters nicely with tabs. Unfortunately, this was taken advantage of for data exfiltration via HTML injection and we no longer have this nice thing as URL parsers have been made more strict to prevent this kind of attack.<p>Does anyone have a source&#x2F;reference for this?
Kazkansalmost 3 years ago
Why dont just use groff&#x2F;troff and output to html?
Voeidalmost 3 years ago
Figure 2. showing the &quot;common style&quot; is something I&#x27;ve never used or seen before.<p>What is the &quot;right&quot; way? Perhaps it is to use style from both of these extreme examples and write code that is easy to read and edit for the person that is working with it.<p>Or perhaps the right way is to never imply the way you are doing things is the only correct way and then try to pass it on as facts?
JJMcJalmost 3 years ago
I too found out I&#x27;d been doing my shoelaces wrong. YouTube set me straight.<p>For HTML, these are good recommendations.<p>Sometimes, like for technical writing where there are<p><pre><code> various </code></pre> <i>distinct</i> and important formatting choices, it&#x27;s just hard work to get it the way you want it even with a WYSIWYG editor.
exodustalmost 3 years ago
Closing li tags is the right thing to do! I always close the kitchen drawer too after putting the scissors back. But I rarely write HTML as content anyway, it&#x27;s mostly templates for the CMS, where it&#x27;s best to close the tags.
评论 #31696058 未加载
martin_aalmost 3 years ago
While I&#x27;m no big fan of SEO and all that surrounds it: Will this open-tag-thing here influence how crawlers handle your site and index&#x2F;rank it?
评论 #31700267 未加载
评论 #31698654 未加载
tiffanyhalmost 3 years ago
One of the easiest ways to improve SEO is to just properly use existing HTML tags (instead of using a custom DIV for everything).
jokoonalmost 3 years ago
I&#x27;m curious if a more strict html parser would actually be faster.<p>Browsers are not really fast on my Android, and I wish they were fast.
评论 #31700280 未加载
评论 #31700025 未加载
_glassalmost 3 years ago
love the troff reference. I wrote my first CV in troff. mostly because it was available on my linux machine, and working.
iLoveOncallalmost 3 years ago
In 2022 how often do you actually write text by hand in your HTML files? I find that beside the few buttons here and there (and that&#x27;s if you don&#x27;t have i18n), text is always going to be served by a server.<p>In 2022 we also all use text editors or IDEs that can collapse entire blocks of tags, to improve readability.<p>I&#x27;m not sure I can see a clear benefit here outside of very few edge cases, and I am sure it comes with its lot of disadvantages.
评论 #31695915 未加载
EugeneOZalmost 3 years ago
XML is beautiful and clean, and I prefer to write full closing tags.
评论 #31698532 未加载
评论 #31696765 未加载
irrationalalmost 3 years ago
This post needs an OCD trigger warning.
评论 #31704208 未加载
eatsyourtacosalmost 3 years ago
Rite HTML Wright<p>(sorry)
math_dandyalmost 3 years ago
Keep calm and Prettier on.