HTML Styling Standards · HTML-02
Use HTML5 · HTML-02.1 · MUST
HTML5 (HTML syntax) is preferred for all HTML documents: .
(It’s recommended to use HTML, as text/html. Do not use XHTML. XHTML, as application/xhtml+xml, lacks both browser and infrastructure support and offers less room for optimization than HTML.)
Although fine with HTML, do not close void elements, i.e. write , not .
Use valid HTML where possible · HTML-02.2 · MUST
Use valid HTML code unless that is not possible due to otherwise unattainable performance goals regarding file size.
Use tools such as the W3C HTML validator to test.
Using valid HTML is a measurable baseline quality attribute that contributes to learning about technical requirements and constraints, and that ensures proper HTML usage.
<!-- Not recommended -->
<title>Test</title>
<article>This is only a test.</article><!-- Recommended -->
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Test</title>
<article>This is only a test.</article>Use HTML according to its purpose · HTML-02.3 · MUST
Use elements (sometimes incorrectly called “tags”) for what they have been created for. For example, use heading elements for headings, p elements for paragraphs, a elements for anchors, etc.
Using HTML according to its purpose is important for accessibility, reuse, and code efficiency reasons.
<!-- Not recommended -->
<div onclick="goToRecommendations();">All recommendations</div><!-- Recommended -->
<a href="recommendations/">All recommendations</a>Use HTTPS for embedded resources where possible · HTML-02.4 · MUST
Always use HTTPS (https:) for images and other media files, style sheets, and scripts, unless the respective files are not available over HTTPS.
<!-- Not recommended: omits the protocol -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!-- Not recommended: uses HTTP -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script><!-- Recommended -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>/* Not recommended: omits the protocol */
@import "//fonts.googleapis.com/css?family=Open+Sans";
/* Not recommended: uses HTTP */
@import "http://fonts.googleapis.com/css?family=Open+Sans";/* Recommended */
@import "https://fonts.googleapis.com/css?family=Open+Sans";Separate structure from presentation from behavior · HTML-02.5 · MUST
Strictly keep structure (markup), presentation (styling), and behavior (scripting) apart, and try to keep the interaction between the three to an absolute minimum.
That is, make sure documents and templates contain only HTML and HTML that is solely serving structural purposes. Move everything presentational into style sheets, and everything behavioral into scripts.
In addition, keep the contact area as small as possible by linking as few style sheets and scripts as possible from documents and templates.
Separating structure from presentation from behavior is important for maintenance reasons. It is always more expensive to change HTML documents and templates than it is to update style sheets and scripts.
<!-- Not recommended -->
<!DOCTYPE html>
<title>HTML sucks</title>
<link rel="stylesheet" href="base.css" media="screen" />
<link rel="stylesheet" href="grid.css" media="screen" />
<link rel="stylesheet" href="print.css" media="print" />
<h1 style="font-size: 1em;">HTML sucks</h1>
<p>
I’ve read about this on a few sites but now I’m sure:
<u>HTML is stupid!!1</u>
</p>
<center>
I can’t believe there’s no way to control the styling of my website without
doing everything all over again!
</center><!-- Recommended -->
<!DOCTYPE html>
<title>My first CSS-only redesign</title>
<link rel="stylesheet" href="default.css" />
<h1>My first CSS-only redesign</h1>
<p>
I’ve read about this on a few sites but today I’m actually doing it:
separating concerns and avoiding anything in the HTML of my website that is
presentational.
</p>
<p>It’s awesome!</p>Do not use entity references · HTML-02.6 · MUST
There is no need to use entity references like —, ”, or ☺, assuming the same encoding (UTF-8) is used for files and editors as well as among teams.
The only exceptions apply to characters with special meaning in HTML (like < and &) as well as control or “invisible” characters (like no-break spaces - ).
<!-- Not recommended -->
The currency symbol for the Euro is “&eur;”.<!-- Recommended -->
The currency symbol for the Euro is "€".Omit type attributes for style sheets and scripts · HTML-02.7 · MUST
type attributes for style sheets and scripts · HTML-02.7 · MUSTDo not use type attributes for style sheets (unless not using CSS) and scripts (unless not using JavaScript).
Specifying type attributes in these contexts is not necessary as HTML5 implies text/css and text/javascript as defaults. This can be safely done even for older browsers.
<!-- Not recommended -->
<link
rel="stylesheet"
href="https://www.google.com/css/maia.css"
type="text/css"
/><!-- Recommended -->
<link rel="stylesheet" href="https://www.google.com/css/maia.css" /><!-- Not recommended -->
<script
src="https://www.google.com/js/gweb/analytics/autotrack.js"
type="text/javascript"
></script><!-- Recommended -->
<script src="https://www.google.com/js/gweb/analytics/autotrack.js"></script>Avoid unnecessary id attributes · HTML-02.8 · MUST
id attributes · HTML-02.8 · MUSTPrefer class attributes for styling and data- attributes for scripting.
Where id attributes are strictly required, always include a hyphen in the value to ensure it does not match the JavaScript identifier syntax, e.g. use user-profile rather than just profile or userProfile.
When an element has an id attribute, browsers will make that available as a named property on the global window prototype, which may cause unexpected behavior. While id attribute values containing a hyphen are still available as property names, these cannot be referenced as global JavaScript variables.
<!-- Not recommended: <code>window.userProfile</code> will resolve to reference the <div> node -->
<div aria-describedby="userProfile">
…
<div id="userProfile"></div>
…
</div><!-- Recommended: <code>id</code> attribute is required and its value includes a hyphen -->
<div aria-describedby="user-profile">
…
<div id="user-profile"></div>
…
</div>Use only lowercase · HTML-02.9 · MUST
All code has to be lowercase: This applies to HTML element names, attributes, attribute values (unless text/CDATA), CSS selectors, properties, and property values (with the exception of strings).
<!-- Not recommended -->
<a href="/">Home </a><!-- Recommended -->
<a href="/">Home</a>Remove trailing white spaces · HTML-02.10 · MUST
Trailing white spaces are unnecessary and can complicate diffs.
<!-- Not recommended -->
<p>What? </p><!-- Recommended -->
<p>Yes please.</p>Provide alternative contents for multimedia · HTML-02.11 · SHOULD
For multimedia, such as images, videos, animated objects via canvas, make sure to offer alternative access. For images that means use of meaningful alternative text (alt) and for video and audio transcripts and captions, if available.
Providing alternative contents is important for accessibility reasons: A blind user has few cues to tell what an image is about without the alt attribute, and other users may have no way of understanding what video or audio contents are about either.
(For images whose alt attributes would introduce redundancy, and for images whose purpose is purely decorative which you cannot immediately use CSS for, use no alternative text, as in alt="".)
<!-- Not recommended -->
<img src="spreadsheet.png" /><!-- Recommended -->
<img src="spreadsheet.png" alt="Spreadsheet screenshot." />Use HTTPS for embedded resources where possible · HTML-02.12 · MUST
Always use HTTPS (https:) for images and other media files, style sheets, and scripts, unless the respective files are not available over HTTPS.
<!-- Not recommended: omits the protocol -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<!-- Not recommended: uses HTTP -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script><!-- Recommended -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>