-
Notifications
You must be signed in to change notification settings - Fork 74
New HTML Style Guide #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New HTML Style Guide #117
Changes from all commits
266cf44
072a1eb
adcc070
e0cc9c5
a34b5ac
71a0680
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,175 @@ | ||
<script>{ | ||
"title": "HTML Style Guide" | ||
"title": "HTML Style Guide", | ||
"toc": true | ||
}</script> | ||
|
||
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. | ||
|
||
## Doctype | ||
Always use the minimal, versionless doctype. | ||
## Linting | ||
|
||
``` | ||
<!doctype html> | ||
``` | ||
Use [grunt-html](https://www.npmjs.com/package/grunt-html) to detect errors and potential problems. Most jQuery projects have a Grunt task for linting all HTML files: `grunt htmllint`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think Chassis has this (although I could be missing it). Might be useful to add that validation, especially with the documentation/style guides in Chassis. |
||
|
||
## Language | ||
## Spacing | ||
|
||
Always define which language the page is written in. | ||
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`, or `style`. Indent everything else. | ||
- No whitespace at the end of line or on blank lines. | ||
- Separate attributes with a space. | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en"> | ||
``` | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Sample Page</title> | ||
|
||
## Encoding | ||
<link rel="stylesheet" href="/style.css"> | ||
<style> | ||
body { | ||
font-size: 100em; | ||
} | ||
</style> | ||
|
||
<script src="/jquery.js"></script> | ||
<script> | ||
$( function() { | ||
$( "p" ).text( $.fn.jquery ); | ||
} ); | ||
</script> | ||
</head> | ||
<body> | ||
|
||
Always define the character encoding. The encoding should be defined as early as possible. | ||
<section> | ||
<p>jQuery is awesome!<p> | ||
</section> | ||
|
||
</body> | ||
</html> | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need a large code example followed by another large code example so high up in the style guide. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spacing and Formatting can't really do without large code examples. And, if I push them below then the html style guide would have a different format from the other style guides. |
||
<meta charset="utf-8"> | ||
``` | ||
|
||
## Elements and Attributes | ||
|
||
All element and attribute names should be lowercase. Attribute values should be quoted. Optional closing tags should be included. Self-closing elements should not be closed. Optional attributes should be omitted. Always include `html`, `head`, and `body` tags. | ||
## Formatting | ||
|
||
- Always use the minimal, versionless and lowercase doctype. | ||
- Always define which language the page is written in. | ||
- Always include `html`, `head`, and `body` 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](https://html.spec.whatwg.org/multipage/semantics.html#charset). | ||
- 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` and `class` attributes must be written before any other attribute. | ||
- Always include an `alt` attribute for images. | ||
|
||
```html | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Good Example</title> | ||
<link rel="stylesheet" href="style.css"> | ||
<script src="/jquery.js"></script> | ||
</head> | ||
<body> | ||
|
||
* No `type` or `language` attributes on `script` tags. | ||
* No `type` attribute on `link` or `style` tags. | ||
<img class="sample-image" src="assets/img/img.png" alt="Sample Image"> | ||
<p>This is a sample image</p> | ||
|
||
</body> | ||
</html> | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this example is providing value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about you explain the value that you think this example provides. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what you mean by value here. To me, the example covers every point mentioned in formatting. |
||
<script src="..."></script> | ||
<script></script> | ||
<link rel="stylesheet" href="..."> | ||
<style></style> | ||
``` | ||
|
||
## Indentation | ||
## HTML Semantics | ||
|
||
Always use HTML elements for what they are meant to be used for. | ||
|
||
Don't indent inside `html`, `body`, `script`, or `style`. Indent inside `head` and all other elements. | ||
## Reducing Markup | ||
|
||
### Example | ||
Always try to avoid superfluous parent elements. | ||
|
||
```html | ||
<!-- Bad HTML --> | ||
<span class="avatar"> | ||
<img src="assets/img/img.png" alt="Jane Doe"> | ||
</span> | ||
|
||
<!-- Good HTML --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
<img class="avatar" src="assets/img/img.png" alt="Jane Doe"> | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a meaningless example. It's not clear that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
## Separation of Concerns | ||
|
||
Always keep markup, styling, and scripting separate. | ||
|
||
```html | ||
<!-- Bad Example --> | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Sample Page</title> | ||
<script src="script.js"></script> | ||
</head> | ||
<body> | ||
|
||
<link rel="stylesheet" href="/style.css"> | ||
<style> | ||
body { | ||
font-size: 100em; | ||
} | ||
</style> | ||
<h1 style="font-size:1em;line-height:2em;">This is a heading.</h1> | ||
<p style="text-decoration:underline;font-size:.5em;line-height:1em;">This is an underlined paragraph.</p> | ||
</body> | ||
</html> | ||
|
||
<script src="/jquery.js"></script> | ||
<script> | ||
$(function() { | ||
$( "p" ).text( $.fn.jquery ); | ||
}); | ||
</script> | ||
<!-- Good Example --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implies that |
||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Sample Page</title> | ||
<link rel="stylesheet" href="style.css"> | ||
<script src="script.js"></script> | ||
</head> | ||
<body> | ||
|
||
<p>jQuery is awesome!<p> | ||
|
||
<h1>This is a heading.</h1> | ||
<p>This is an underlined paragraph.</p> | ||
</body> | ||
</html> | ||
``` | ||
|
||
## Forms | ||
|
||
Always include a `for` attribute for `label` elements. This is more robust than implicit labeling across browsers and assistive technologies. | ||
|
||
```html | ||
<!-- Bad Example --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing. |
||
<label>First<input type="radio" name="input" value="first"></label> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the label text after the input so the two examples are consistent. |
||
|
||
<!-- Good Example --> | ||
<input type="radio" name="input" id="input-1" value="first"> | ||
<label for="input-1">First</label> | ||
``` | ||
Don't use the `placeholder` attribute for labeling; always use a `label` element. | ||
|
||
```html | ||
<!-- Bad Example --> | ||
<input type="text" id="name" placeholder="Enter your name"> | ||
|
||
<!-- Good Example --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates the style guide itself:
|
||
<label for="name">Name</label> | ||
<input id="name" type="text" placeholder="Jane Doe"> | ||
``` | ||
|
||
## 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 `-->`. | ||
|
||
```html | ||
<!-- Good single line comment --> | ||
|
||
<!-- | ||
Good example of a multi-line comment. | ||
If your comment takes up multiple lines, please do this. | ||
--> | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While extending this page it might be better to add a TOC similar to the JavaScript style guide? (possible by adding
"toc": true
).Would it be a good idea here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't get that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a Table of Contents, Basically add this to the top of your page:
<script>{ "title": "HTML Style Guide", "toc": true }</script>
You can see an example here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thank you!