HTML Style Guide
This page outlines the style guide for HTML pages in all jQuery projects. These rules apply to web sites, demo pages, inline examples, test pages, etc. Exceptions are allowed for pages that must violate the rules by their very nature, e.g., a page that tests XHTML.
link Linting
Use grunt-html to detect errors and potential problems. Most jQuery projects have a Grunt task for linting all HTML files: grunt htmllint
.
link Spacing
In general, the jQuery style guide encourages liberal spacing for improved human readability.
- Indentation with tabs.
- Don't indent the direct children of
html
,body
,script
, orstyle
. Indent everything else. - No whitespace at the end of line or on blank lines.
- Separate attributes with a space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
|
link Formatting
- Always use the minimal, versionless and lowercase doctype.
- Always define which language the page is written in.
- Always include
html
,head
, andbody
tags. - Always define the character encoding. Most pages should use utf-8. The encoding should be defined as early as possible and must be in the first 1024 bytes of the document.
- Always use lowercase names for elements and attributes.
- Always quote attribute values. Always use double quotes instead of single quotes.
- Include the optional closing tags.
- Self-closing elements must not be closed.
- Optional attributes must be omitted.
rel
,type
,src
,href
andclass
attributes must be written before any other attribute.- Always include an
alt
attribute for images.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
link HTML Semantics
Always use HTML elements for what they are meant to be used for.
link Reducing Markup
Always try to avoid superfluous parent elements.
1
2
3
4
5
6
7
|
|
link Separation of Concerns
Always keep markup, styling, and scripting separate.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
|
link Forms
Always include a for
attribute for label
elements. This is more robust than implicit labeling across browsers and assistive technologies.
1
2
3
4
5
6
|
|
Don't use the placeholder
attribute for labeling; always use a label
element.
1
2
3
4
5
6
|
|
link Comments
Comments are always preceded by a blank line. Comments start with a capital first letter, but don't require a period at the end, unless you're writing full sentences. There must be a single space between the comment token and the comment text.
When comments take up multiple lines, break line after <!--
, write your comment in multiple lines and then close the comment in a next line with -->
.
1
2
3
4
5
6
|
|