HTML Style Guide HTML Style Guide
HTML Style Guide HTML Style Guide
The keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
document are to be interpreted as described in RFC 2119.
Icon Legend:
Table of Contents
1. Files
1. File Format
2. Filename
2. Document
1. Doctype
2. Language
3. Character Encoding
4. Viewport
5. Head and Body
3. Comments
1. Single-line Comments
2. Multi-line Comments
3. Closing Tag Comments
4. Sensitive Information
4. Formatting
1. Line Indentation
2. Direct Child Elements
3. Block, List and Table Elements
4. Trailing Whitespace
5. Elements and Attributes
5. Elements and Attributes
1. Self-closing Elements
2. Optional Open/Close Tags
3. Attribute Values
4. Attribute Booleans
5. Type and Language Attribute
6. Type Attribute
7. Protocol
6. Best Practices
1. Structures, Styles and Scripts
2. Valid HTML
3. Semantic HTML
4. Form Field Labels
5. Form Field Names
6. Entity References
1. Files
This section describes the format and naming convention of HTML files.
File Format
Filename
▲ Table of Contents
2. Document
This section showcases the main elements required in a complete HTML file.
▲ Table of Contents
Example
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
</head>
<body>
<h1>Welcome</h1>
<p>This is a skeleton document.</p>
</body>
</html>
▲ Document
3. Comments
This section describes how comments should be formatted and used.
1. Single-line comments MUST be on one line and text inside MUST be surrounded by spaces
e.g. <!-- This is a comment -->
2. Multi-line comments MUST start and end on their own line and text MUST NOT be indented
i.e. <!-- ↵ Line number one ↵ Line number two ↵ -->
3. Closing tag comments SHOULD include the class or ID symbol and name
e.g. </div><!-- #main-wrapper -->
4. Sensitive information MUST NOT be placed in a comment
e.g. <!-- Sends email to [email protected] -->
▲ Table of Contents
1. Single-line Comments
Single-line comments MUST be on one line and text inside MUST be surrounded by spaces.
✖ Incorrect
<!--
This is a comment
-->
↳ Incorrect because <!-- , This is a comment and --> should be one line.
<!--This is a comment-->
✔ Correct
▲ Comments
2. Multi-line Comments
Multi-line comments MUST start and end on their own line and text MUST NOT be indented.
✖ Incorrect
<!-- This is a comment
that spans multiple lines
-->
<!--
This is a comment
that spans multiple lines
-->
↳ Incorrect because This is a comment and that spans multiple lines are indented.
✔ Correct
<!--
This is a comment
that spans multiple lines
-->
▲ Comments
Closing tag comments SHOULD include the class or ID symbol and name.
✖ Incorrect
<div id="main-wrapper">
...
</div><!-- main-wrapper -->
<div id="main-wrapper">
...
</div><!-- .main-wrapper -->
✔ Correct
<div id="main-wrapper">
...
</div><!-- #main-wrapper -->
▲ Comments
4. Sensitive Information
✖ Incorrect
▲ Comments
4. Formatting
This section outline various, general formatting rules related to whitespace and text.
1. Line indentation MUST be accomplished using tabs
i.e. <ul> ↵ ⇥ <li>
2. Direct child elements within html , body , style or script element MUST NOT be indented
i.e. <body> ↵ <h1></h1> ↵ </body> , <head> ↵ ⇥ ... </head>
3. Block, list and table elements MUST start on a new line and their children MUST be indented
i.e. <div> ↵ ⇥ <h1> , <table> ↵ ⇥ <th>
4. Trailing whitespace MUST NOT be present
i.e. no <p></p> · · ↵
5. Elements and attributes MUST be all lowercase
e.g. <a href="" title=""> , <link rel="stylesheet" href="">
▲ Table of Contents
1. Line Indentation
✖ Incorrect
<ul>
<li>Item number one</li>
<li>Item number two</li>
</ul>
✔ Correct
<ul>
<li>Item number one</li>
<li>Item number two</li>
</ul>
▲ Formatting
Direct child elements within html , body , style or script element MUST NOT be indented.
✖ Incorrect
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
<meta charset="utf-8">
<style>
#main-wrapper {
width: 960px;
}
</style>
<script>
function show_alert() {
}
</script>
</head>
<body>
<div id="main-wrapper">
<h1>Welcome</h1>
<p>This is a skeleton document.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
<meta charset="utf-8">
<style>
#main-wrapper {
width: 960px;
}
</style>
<script>
function show_alert() {
}
</script>
</head>
<body>
<div id="main-wrapper">
<h1>Welcome</h1>
<p>This is a skeleton document.</p>
</div>
</body>
</html>
▲ Formatting
Block, list and table elements MUST start on a new line and their children MUST be indented.
✖ Incorrect
<div><p>This is a paragraph.</p></div>
<table><tr><th>Header</th></tr><tr><td>Content</td></tr></table>
↳ Incorrect because <div> , <ul> , <table> and <tr> are not on their own line.
↳ Incorrect because <p> , <li> , <tr> and <td> are not indented.
✔ Correct
<div>
<p>This is a paragraph.</p>
</div>
<ul>
<li>Item one</li>
<li>Item two</li>
</ul>
<table>
<tr>
<th>Header</th>
</tr>
<tr>
<td>Content</td>
</tr>
</table>
▲ Formatting
4. Trailing Whitespace
✖ Incorrect
<p>This is a paragraph.</p>
✔ Correct
<p>This is a paragraph.</p>
▲ Formatting
✖ Incorrect
✔ Correct
▲ Formatting
▲ Table of Contents
1. Self-closing Elements
✖ Incorrect
<br />
<br>
✖ Incorrect
<!DOCTYPE html>
<html lang="en">
<div id="main-wrapper">
<h1>Welcome</h1>
<p>This is a skeleton document.
</div>
</html>
✔ Correct
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<div id="main-wrapper">
<h1>Welcome</h1>
<p>This is a skeleton document.</p>
</div>
</body>
</html>
3. Attribute Values
✖ Incorrect
↳ Incorrect because processor.php and application are not wrapped in double quotes.
✔ Correct
4. Attribute Booleans
✔ Correct
✖ Incorrect
✔ Correct
<script src="//script.js"></script>
6. Type Attribute
✖ Incorrect
✔ Correct
7. Protocol
~ Acceptable
✔ Correct
6. Best Practices
▲ Table of Contents
✖ Incorrect
<div class="internships-module">
...
</div>
✔ Correct
▲ Best Practices
2. Valid HTML
✖ Incorrect
<ul>
<li>Item one</li>
<li>Item two</li>
<ul>
<li>Sub-item one</li>
<li>Sub-item two</li>
</ul>
</ul>
✔ Correct
<ul>
<li>Item one</li>
<li>Item two</li>
<li>
<ul>
<li>Sub-item one</li>
<li>Sub-item two</li>
</ul>
</li>
</ul>
▲ Best Practices
3. Semantic HTML
Semantic HTML MUST be written.
✖ Incorrect
↳ Incorrect because Lorem ipsum is to be bold, not strongly spoken by a screen reader.
✔ Correct
▲ Best Practices
✖ Incorrect
✔ Correct
▲ Best Practices
✖ Incorrect
↳ Incorrect because emailaddress and email-address are not using underscores between words.
✔ Correct
▲ Best Practices
6. Entity References
✖ Incorrect
✔ Correct
▲ Best Practices
▲ Table of Contents
Inspired in part by style guides from: CKAN, Google, jQuery, and WordPress.