Standards for developing flexible, durable, and sustainable HTML

Introduction:

In this article i will explain standards for developing flexible, durable, and sustainable HTML.

Description:

In previous articles i explained C# Coding Standards and Naming Conventions. Now i will explain standards for developing flexible, durable, and sustainable HTML.

Table of contents

HTML

  • Syntax
  • HTML5 doctype
  • Language attribute
  • Character encoding
  • Internet Explorer compatibility mode
  • CSS and JavaScript includes
  • Practicality over purity
  • Attribute order
  • Boolean attributes
  • Reducing markup
  • Java Script generated markup

 

Now we will see one by one in detail

Syntax:

  • Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment.
  • Nested elements should be indented once (two spaces).
  • Always use double quotes, never single quotes, on attributes.
  • Don't include a trailing slash in self-closing elements—theHTML5 spec says they're optional.
  • Don’t omit optional closing tags (e.g. </li> or </body>).

<!DOCTYPE html> <html>  <head>    <title>Page title</title>  </head>  <body>    <img src="images/company-logo.png" alt="Company">    <h1 class="hello-world">Hello, world!</h1>  </body> </html>

HTML5 doctype

Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page.

<!DOCTYPE html> <html>  <head>  </head> </html>

Language attribute

From the HTML5 spec:

Authors are encouraged to specify a lang attribute on the root html element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth.

<html lang="en-us">  <!-- ... --> </html>

Read more about the lang attribute in the spec.

Head to Sitepoint for a list of language codes.

IE compatibility mode

Internet Explorer supports the use of a document compatibility<meta> tag to specify what version of IE the page should be rendered as. Unless circumstances require otherwise, it's most useful to instruct IE to use the latest supported mode with edge mode.

<meta http-equiv="X-UA-Compatible" content="IE=Edge">

For more information, read this awesome Stack Overflow article.

Character encoding

Quickly and easily ensure proper rendering of your content by declaring an explicit character encoding. When doing so, you may avoid using character entities in your HTML, provided their encoding matches that of the document (generally UTF-8).

<head>  <meta charset="UTF-8"> </head>

CSS and JavaScript includes

Per HTML5 spec, typically there is no need to specify a typewhen including CSS and JavaScript files as text/css andtext/javascript are their respective defaults.

<!-- External CSS --> <link rel="stylesheet" href="code-guide.css"> <!-- In-document CSS --> <style>  /* ... */ </style> <!-- JavaScript --> <script src="code-guide.js"></script>

HTML5 spec links

Practicality over purity

Strive to maintain HTML standards and semantics, but not at the expense of practicality. Use the least amount of markup with the fewest intricacies whenever possible.

Attribute order

HTML attributes should come in this particular order for easier reading of code.

  • class
  • idname
  • data-*
  • srcfortypehref
  • titlealt
  • aria-*role

Classes make for great reusable components, so they come first. Ids are more specific and should be used sparingly (e.g., for in-page bookmarks), so they come second.

<a class="..." id="..." data-modal="toggle" href="#">  Example link </a> <input class="form-control" type="text"> <img src="..." alt="...">

Boolean attributes

A boolean attribute is one that needs no declared value. XHTML required you to declare a value, but HTML5 has no such requirement.

<span style="color:rgb(47, 111, 159);"><input</span> <span style="color:rgb(79, 159, 207);">type=</span><span style="color:rgb(212, 73, 80);">"text"</span> <span style="color:rgb(79, 159, 207);">disabled</span><span style="color:rgb(47, 111, 159);">></span>

<span style="color:rgb(47, 111, 159);"><input</span> <span style="color:rgb(79, 159, 207);">type=</span><span style="color:rgb(212, 73, 80);">"checkbox"</span> <span style="color:rgb(79, 159, 207);">value=</span><span style="color:rgb(212, 73, 80);">"1"</span> <span style="color:rgb(79, 159, 207);">checked</span><span style="color:rgb(47, 111, 159);">></span>

<span style="color:rgb(47, 111, 159);"><select></span>
  <span style="color:rgb(47, 111, 159);"><option</span> <span style="color:rgb(79, 159, 207);">value=</span><span style="color:rgb(212, 73, 80);">"1"</span> <span style="color:rgb(79, 159, 207);">selected</span><span style="color:rgb(47, 111, 159);">></span>1<span style="color:rgb(47, 111, 159);"></option></span>
<span style="color:rgb(47, 111, 159);"></select></span>

For further reading, consult the WhatWG section on boolean attributes:

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If you must include the attribute's value, and you don't need to, follow this WhatWG guideline:

If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace.

In short, don't add a value.

Reducing markup

Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML. Take the following example:

<!-- Not so great --> <span class="avatar">  <img src="..."> </span> <!-- Better --> <img class="avatar" src="...">

JavaScript generated markup

Writing markup in a JavaScript file makes the content harder to find, harder to edit, and less performant. Avoid it whenever possible.

  • Created
    Jul 18, 2014
  • Updated
    Oct 03, 2020
  • Views
    2,313