The 3 Ground Rules for Writing HTML

Published on October 27, 2009 (ā†» February 5, 2024), filed under (RSS feed for allĀ categories).

This and many other posts are also available as a pretty, well-behaved ebook: On Web Development.

1. Respect Syntax and Semantics

In other words: Validate your code and use markup according to its purpose. For validation there are validators, and both syntax and semantics are explained in specifications. Many advantages spring out of respecting syntax and semantics. Most notably, avoiding a few accessibility pitfalls and being professional.

2. Donā€™t Use Presentational or Behavioral Markup

In other words: Avoid presentational and behavioral elements and attributes, avoid presentational and behavorial ID and class names, avoid linking off to too many style sheets and scripts, avoid screwing up file names, &c. pp. Markup changes continue to be most expensive. Separation of concerns is the only option you have to write markup that is reusable and maintainable.

3. Leave Everything Out That Is Not Absolutely Necessary

In other words: Leave everything out that is not absolutely necessary. Question everything. Just using the markup you need sounds easy but, unfortunately, itā€™s not. It requires experience and expertise. When questioning everything and omitting even more, look into syntactical opportunities, too (protocol-less URLs, optional tags, unquoted attribute values, &c.). Once comfortable with the techniques, welcome not only performance but also maintainability benefits.

Bonus

At the end of the day, however, web development is about finding the right balance. Implementation issues, special accessibility requirements, client pressure, more substantial understandability needs, temporary experiments, and many more things can make us choose a different approach that affects the rules above. For a professional web developer itā€™s crucial not to go overboard with exceptions, and to keep the rules in mind all the time.

Was this useful or interesting? Share (toot) this post, or maybe treat me to a coffee.Ā Thanks!

About Me

Jens Oliver Meiert, on September 30, 2021.

Iā€™m Jens, and Iā€™m an engineering lead and author. Iā€™ve worked as a technical lead for companies like Google and as an engineering manager for companies like Miro, Iā€™m close to W3C and WHATWG, and I write and review books for Oā€™Reilly and FrontendĀ Dogma.

With my current move to Spain, Iā€™m open to a new remote frontend leadership position. Feel free to review and refer my CV or LinkedInĀ profile.

I love trying things, not only in web development, but also in other areas like philosophy. Here on meiert.com I share some of my views andĀ experiences.

Comments (Closed)

  1. On October 27, 2009, 23:16 CET, Neovov said:

    Mmmā€¦ For me, specifications are not for a learning purpose, but for implementors. Something is missing for teaching how to use the semantics.

    Your second point can be discussed, if someone try to learn HTML 4 and you tell him ā€œdonā€™t use because it is a presentational elementā€ then he try to learn HTML5 and ba-zing! Heā€™s lost with which is now semantic šŸ˜Š.

    You may know, but Iā€™m not quite right with the third point.

    If Iā€™d have to summarised your points with words : 1) be coherent, 2) think future, 3) KISS

  2. On October 27, 2009, 23:38 CET, Christian Leon Christensen said:

    What do you mean by ‘avoid presentational and behavorial ID and class namesā€™?

    No [div id=menu]?

  3. On October 28, 2009, 0:05 CET, Craig Reville said:

    Spot on,

    All three points valid in there own right.

    First one is a must for me, I must have an as close to validating code as possible (Not always possible when using blogging streams)

    Second one, I agree, I have seen some people adding fancy coding and element descriptions, that are not required, they make the file size larger.

    Third one, ALWAYS keep the code simple and not over bulk it. By keeping elements simple, you are making it easier to upgrade in the future so does not require additional future coding now!!

    Well presented, nice and simpleā€¦.thank you

    Regards

    Craig

  4. On October 28, 2009, 0:07 CET, Five Minute Argument said:

    Maybe itā€™s just me, but ‘menuā€™ seems more structural than either presentational or behavioural.

  5. On October 28, 2009, 1:01 CET, DefunktOne said:

    thank you for sharing this!

  6. On October 28, 2009, 1:11 CET, Mike said:

    Great summary Jens! I completely agree. I think you should expand a bit on the importance of landing on the logical structure and then building upon it with semantic, consistent class names. I think too many developers start with bad markup and try to correct it with unnecessarily complicated CSS and JavaScript.

  7. On October 28, 2009, 3:47 CET, Ruben S said:

    In a perfect world these would all be great, but until all contemporary browsers see the same code and render the same output as a result, there will be a need to bend these rules. Iā€™m looking specifically at a company in Redmond!

    HTML5 with its more semantic tags seems to be another step in the right direction, but it seems to me weā€™ve still got a LONG way to go yet.

  8. On October 28, 2009, 6:39 CET, anon said:

    ā€œIn other words: Leave everything out that is not absolutely necessary. ā€
    - Can someone please teach that to the people who wrote the Microsoft Word ā€œSave as HTMLā€ bit !!!!

  9. On October 28, 2009, 7:41 CET, Neovov said:

    Well, of course the ā€œsmallā€ tag was removed from my exampleā€¦

  10. On October 29, 2009, 10:06 CET, Jens Oliver Meiert said:

    Nicolas, yet specs usually explain meaning and purpose, along with examples. And every site between specs and you is a potential error source. (Short story. šŸ˜Š)

    Christian, with presentational ID and class names I mean stuff like red, w1024, clear, &c.

    Craig, thanks!

    Five Minute Argument (and Christian again), I agree.

    Ruben, things will definitely continue to be exciting, yes.

    Anon, šŸ˜Š

    Nicolas again, small is actually a case for which you can argue itā€™s not presentational. Think ā€œsmall print,ā€ which usually refers to certain legal contents, and this is how HTML 5 puts it at least.

  11. On November 4, 2009, 9:16 CET, Niels Matthijs said:

    Smart and concise write-up, though Iā€™m not quite sure about point 3.

    As you already stated yourself in point 2 mark-up changes are the most expensive ones, so providing a perfectly tailored html document for a certain site will obviously hinder maintainability (design reworks etc etc), as it will almost always result in costly mark-up changes over time (you know, call the company who handled the Drupal implementation + go through a whole lot of error checking cycles). You might not actually be hinting at removing all structural elements you donā€™t need, but Iā€™m quite sure many people will read it like that.

    The reason why I donā€™t actually disagree with point 3 is based on the fact that I believe that ā€œstructural elementsā€ like div class=ā€headerā€ (in html replaced by the header element) are not ā€œunnecessaryā€, even if they are not used for styling or behavior. They have a clear structural function enriching your document. I know that Iā€™m one of the few who think like this, so point 3 could use some extra explaining if you ask me šŸ˜Š

  12. On November 5, 2009, 1:03 CET, David S said:

    Best distillation of ā€œbest practicesā€ Iā€™ve seen on HTML. I agree completely, and I think that youā€™ve covered most objections people may have by including the Bonus at the end.

    It is difficult to completely follow these basic rules (and thereā€™s only 3!), but certainly youā€™ll make a better site if you at least keep these fundamentals in mind as you work.

    Many thanks for your ā€œvalidatorsā€ linkā€“great lists you have there!

  13. On November 9, 2009, 11:38 CET, Richard said:

    As you say balance is important, and being a pragmatist I am not going to stop using CSS to control what happens when a mouse hovers over a link (which is clearly behavioural). Yes, I could do it all in JavaScript instead but so long as it works in CSS I will continue to do it that way. Itā€™s an anomaly but to my mind a welcome one.

    Itā€™s interesting too that WCAG 2.0 doesnā€™t require markup to be validated (unlike WCAG 1.0 which does), it only requires the code to be well-formed, which is better from an accessibility point of view because in testing I no longer have to fail a checkpoint because of an unencoded ampersand. Of course it makes testing easier if the code does validate.

  14. On May 10, 2010, 15:28 CEST, Webstandard-Blog said:

    Most of the Webdevelopers or persons who ā€œmake HTMLā€ have problems with think about semantic, because they donā€™t think about the meaning of the content. Thatā€™s the reason why I can advise your 3 basic rules to everybody before he or she starts writing HTML!

  15. On May 13, 2010, 16:34 CEST, Justin said:

    After reading the comments I have gained confidence in your ā€œ3 Basic Rulesā€ I have recently found myself having to research various information on writing HTML. I am not great with it but it has been increasingly necessary since I own multiple sites. Owning a site and being incompetent with html can be quite stressful at times.

    To be honest I am not 100% clear on this even though they are basic rules but I will be sure to use it as reference in the near future.

  16. On June 30, 2010, 9:25 CEST, Bernhard said:

    The theses are correct! I look forward to html5 and css3!

  17. On April 7, 2011, 11:01 CEST, kokogo.de said:

    I think most of the Webmasters have problems with think about semantic, because they donā€™t think about the meaning of the content. Thatā€™s the reason why I can advise your 3 basic rules to everybody before he or she starts writing HTML!

  18. On March 12, 2013, 17:49 CET, vente bois said:

    Hi,
    Itā€™s interesting too that WCAG 2.0 doesnā€™t require markup to be validated (unlike WCAG 1.0 which does), it only requires the code to be well-formed, which is better from an accessibility point of view because in testing I no longer have to fail a checkpoint because of an unencoded ampersand. Of course it makes testing easier if the code does validate.