How to write your HTML
Syntax
- Always use double quotes, never single quotes, on attributes.
- Don't include a trailing slash in self-closing elements—the HTML5 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>Hello, world!</h1>
</body>
</html>
Whitespace
- Use of whitespace can loosely reflect the separation of elements that you might see once rendered.
- Group and space elements with whitespace as you would expect them to be grouped visually in the rendered page.
- A mixture of no, single and double lines of whitespace help to separate elements, or show their visual relationship to one another.
<dl>
<dt>Lorem</dt>
<dd>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</dt>
<dt>Ipsum</dt>
<dd>Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.</dt>
<dt>Dolor</dt>
<dd>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi euismod in pharetra.</dt>
</dl>
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
Every HTML document should have a document language and culture set. 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-gb">
<!-- ... -->
</html>
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>
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">
CSS & JavaScript includes
Per HTML5 spec, typically there is no need to specify a type when including CSS and JavaScript files as text/css and text/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>
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.
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.
<input type="text" disabled>
<input type="checkbox" value="1" checked>
<select>
<option value="1" selected>1</option>
</select>
Semantics
By using a semantic HTML structure, assistive technologies can make better understanding of the content we are displaying to the user and how to interpret them.
An example would be headings. Anything which is a heading should be marked up using the appropriate HTML tags (<h1> through to <h6>) and as a result will form a document outline for the page.
HTML5 also introduces many new HTML elements which we can use to make our templates more semantic, and reduces the amount of <div> tags we need to use, which offer no semantic meaning and are just a generic container.
When using HTML5 Sectioning elements (e.g. <section>, <article>), please ensure to begin with a <h1> as this begins a new section within the document outline.
<article>
<header>
<h1>Blog post title</h1>
<time datetime="2001-01-01">Published on 01/01/2001</time>
</header>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam dignissim varius elit, ut dignissim risus pellentesque
venenatis. Morbi non dignissim tortor. Praesent
condimentum purus sagittis sapien porttitor congue sit
amet ac eros. Donec at diam eu mi mattis tristique.
Aliquam consectetur ornare commodo. Donec tristique
vestibulum tortor a sodales.</p>
<h2>Article sub-heading</h2>
<p>Pellentesque sodales ante semper diam rhoncus et aliquam
risus congue. Nunc id ornare purus. Vestibulum vel ante
et ipsum vestibulum iaculis. Proin et urna vitae purus
lobortis consequat. Morbi luctus dictum nunc, posuere
porta massa ornare et. Ut eget varius mauris. Mauris
at mi augue, a sodales nulla. Vivamus venenatis odio
eu lectus aliquet facilisis. Pellentesque habitant
morbi tristique senectus et netus et malesuada
fames ac turpis egestas. Quisque sollicitudin tortor
ac quam facilisis convallis.</p>
</article>
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.
- For instances where this is necessary, use of a JavaScript templating engine, such as Handlebars.js, is encouraged thus keeping markup in HTML.
General guidelines
- Use a definition list (
<dl>) to mark up forms where possible. - Items in list form should always be marked up using the appropriate
<ul>,<ol>, or<dl>tags. - Do not use the size attribute on input fields, instead specify the width within CSS.
- Tables should only be used for tabular data.
- Always use sentence casing for all text within HTML. Use CSS to apply lowercasing, or uppercasing to meet design requirements.