Skip to content

Commit 643ad50

Browse files
committed
Update html.md
Add to the markup coding standard
1 parent 92551ab commit 643ad50

File tree

1 file changed

+261
-5
lines changed
  • manual/en-US/coding-standards/chapters

1 file changed

+261
-5
lines changed

manual/en-US/coding-standards/chapters/html.md

Lines changed: 261 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,65 @@
33
These guidelines have been assembled following an examination of emerging practices, ideas and existing styleguides, and include items from:
44

55
1. [Google's html styleguide](http://google-styleguide.googlecode.com/svn/trunk/htmlcssguide.xml)
6+
2. [JQuery's HTML Styleguide](http://contribute.jquery.org/style-guide/html/)
7+
3. [Nicolas Ghallager's "Principles of writing consistent, idiomatic HTML"](https://github.com/necolas/idiomatic-html)
8+
4. [Harry Robert's "My HTML/CSS coding style"](http://csswizardry.com/2012/04/my-html-css-coding-style/)
9+
4. [The BBC's Media Standards and Guidelines](http://www.bbc.co.uk/guidelines/futuremedia/technical/semantic_markup.shtml)
10+
11+
### General principles
12+
13+
All code should look like a single person typed it, no matter how many people contributed. All comments and class names should be written using British English spelling.
14+
15+
### Doctype
16+
17+
Always use the minimal, versionless doctype.
18+
19+
```html
20+
<!doctype html>
21+
```
22+
23+
### Language
24+
25+
Always define which language the page is written in.
26+
27+
```html
28+
<html lang="en">
29+
```
30+
31+
### Encoding
32+
Always define the character encoding. The encoding should be defined as early as possible.
33+
Make sure your editor uses UTF-8 as character encoding, without a byte order mark (UTF-8, no BOM).
34+
Do not specify the encoding of style sheets as these assume UTF-8.
35+
36+
```html
37+
<meta charset="utf-8">
38+
```
39+
40+
[More on encodings and when and how to specify them can be found in Handling character encodings in HTML and CSS](http://www.w3.org/International/tutorials/tutorial-char-enc/)
41+
42+
43+
### Capitalisation
44+
All html should be lowercase; element names, attributes, attribute values (unless text/CDATA), CSS selectors, properties, and property values (with the exception of strings). Additionally, there is no need to use CDATA to escape inline JavaScript, formerly a requirement to meet XML strictness in XHTML.
45+
46+
```html
47+
<!-- Good -->
48+
<img src="joomla.png" alt="Joomla">
49+
50+
<!-- Bad -->
51+
<A HREF="/">Home</A>
52+
```
53+
54+
```html
55+
<!-- Good -->
56+
a {
57+
color: #a3a3a3;
58+
}
59+
60+
<!-- Bad -->
61+
a {
62+
color: #A3A3A3;
63+
}
64+
```
665

766
### Protocol
867

@@ -18,8 +77,12 @@ This prevents mixed content issues and results in minor file size savings.
1877
<link rel="stylesheet" href="http://joomla.org/css/main.css">
1978
```
2079

80+
### Elements and Attributes
81+
82+
Always include html, head, and body tags.
83+
2184
### Type attributes
22-
Do not use type attributes for style sheets (unless not using CSS) and scripts (unless not using JavaScript).
85+
Do not use type or attributes for style sheets (unless not using CSS) and scripts (unless not using JavaScript).
2386
```html
2487
<!-- Good -->
2588
<link rel="stylesheet" href="//joomla.org/css/main.css">
@@ -28,17 +91,76 @@ Do not use type attributes for style sheets (unless not using CSS) and scripts (
2891
<link rel="stylesheet" href="//joomla.org/css/main.css" type="text/css">
2992
```
3093

31-
### Capitalisation
32-
All html should be lowercase; element names, attributes, attribute values (unless text/CDATA), CSS selectors, properties, and property values (with the exception of strings).
94+
### Language attributes
95+
Do not use language attributes on script tags.
96+
```html
97+
<!-- Good -->
98+
<script href="//code.jquery.com/jquery-latest.js">
99+
100+
<!-- Bad -->
101+
<script href="//code.jquery.com/jquery-latest.js" language="javascript">
102+
```
103+
104+
105+
### Attributes
33106
107+
Attribute values should be quoted using double ("") quotes. Optional attributes should be omitted.
34108
```html
35109
<!-- Good -->
36-
<img src="joomla.png" alt="Joomla">
110+
<script src="//code.jquery.com/jquery-latest.js"></script>
111+
<!-- Bad -->
112+
<script src='//code.jquery.com/jquery-latest.js'></script>
113+
```
37114

115+
Use attribute/value pairs for boolean attributes
116+
```html
117+
<!-- Good -->
118+
<input type="checkbox" value="on" checked="checked">
38119
<!-- Bad -->
39-
<A HREF="/">Home</A>
120+
<input type="checkbox" value="on" checked>
121+
```
122+
123+
HTML attributes should be listed in an order that reflects the fact that class names are the primary interface through which CSS and JavaScript select elements.
124+
1. class
125+
2. id
126+
3. data-*
127+
4. Everything else
128+
```html
129+
<!-- Good -->
130+
<a class="[some-value]" id="[some-value]" data-name="[some-value]" href="[some-value]">Text</a>
131+
<!-- Bad -->
132+
<a href="[some-value]" class="[some-value]" id="[some-value]" data-name="[some-value]">Text</a>
133+
```
134+
135+
Elements with multiple attributes can have attributes arranged across multiple lines in an effort to improve readability and produce more useful diffs:
136+
137+
```html
138+
<a class="[some-value]"
139+
data-action="[some-value]"
140+
data-id="[some-value]"
141+
href="[some-value]">
142+
<span>Text</span>
143+
</a>
144+
```
145+
146+
### Elements
147+
Optional closing tags may not be omitted.
148+
```html
149+
<!-- Good -->
150+
<p>The quick brown fox jumps over the lazy dog.</p>
151+
<!-- Bad -->
152+
<p>The quick brown fox jumps over the lazy dog.
153+
```
154+
155+
Self-closing (void) elements should not be closed. Trailing forward slashes and spaces should be omitted.
156+
```html
157+
<!-- Good -->
158+
<img src="//images/logo.png" alt="">
159+
<!-- Bad -->
160+
<img src="//images/logo.png" alt="" />
40161
```
41162

163+
42164
### Formatting
43165
Use a new line for every block, list, or table element, and indent every such child element.
44166

@@ -58,6 +180,113 @@ Use a new line for every block, list, or table element, and indent every such ch
58180
</ul></div>
59181
```
60182

183+
We prefer readability over file-size savings when it comes to maintaining existing files. Plenty of whitespace is encouraged. Use whitespace to visually separate groups of related markup and to improve the readability and maintainability of your HTML. Use two empty lines between larger blocks, and use a single empty line between child blocks of larger blocks. Be consistent. (If you are worried about your document's size, spaces (as well as repeated use of the same strings - for instance class names) are excellent candidates for compression. Also, you may use a markup minifier to decrease your document's file size.)
184+
185+
Keep line-length to a sensible maximum, e.g., 80 columns.
186+
187+
Tip: configure your editor to "show invisibles". This will allow you to eliminate end of line whitespace, eliminate unintended blank line whitespace, and avoid polluting commits.
188+
189+
```html
190+
<blockquote>
191+
<p><em>Space</em>, the final frontier.</p>
192+
</blockquote>
193+
194+
195+
<ul>
196+
<li>Moe</li>
197+
<li>Larry</li>
198+
<li>Curly</li>
199+
</ul>
200+
201+
202+
<table>
203+
<thead>
204+
<tr>
205+
<th scope="col">Income</th>
206+
<th scope="col">Taxes</th>
207+
</tr>
208+
<tbody>
209+
<tr>
210+
<td>$ 5.00</td>
211+
<td>$ 4.50</td>
212+
</tr>
213+
</table>
214+
```
215+
216+
### Indentation
217+
Don't indent inside html, body, script, or style. Indent inside head and all other elements.
218+
Indent by four spaces at a time. Don’t use tabs or mix tabs and spaces for indentation.
219+
220+
221+
```html
222+
<!-- Good -->
223+
<!doctype html>
224+
<html lang="en">
225+
<head>
226+
<meta charset="utf-8">
227+
<title>Sample Page</title>
228+
229+
<link rel="stylesheet" href="/style.css">
230+
<style>
231+
body {
232+
font-size: 100em;
233+
}
234+
</style>
235+
236+
<script src="/jquery.js"></script>
237+
<script>
238+
$(function() {
239+
$( "p" ).text( $.fn.jquery );
240+
});
241+
</script>
242+
</head>
243+
<body>
244+
245+
<p>Joomla! is awesome!<p>
246+
247+
</body>
248+
</html>
249+
```
250+
251+
### Trailing Whitespace
252+
Remove trailing white spaces. Trailing white spaces are unnecessary and can complicate diffs.
253+
254+
```html
255+
<!-- Good -->
256+
<p>Yes please.</p>
257+
258+
<!-- Bad -->
259+
<p>No, thank you. </p>
260+
```
261+
262+
263+
### Entity References
264+
Do not use entity references. There is no need to use entity references like &mdash;, &rdquo;, or &#x263a;, assuming the same encoding (UTF-8) is used for files and editors as well as among teams.
265+
266+
The only exceptions apply to characters with special meaning in HTML (like < and &) as well as control or “invisible” characters (like no-break spaces).
267+
```html
268+
<!-- Good -->
269+
<p>The currency symbol for the Euro is “€”.</p>
270+
271+
<!-- Bad -->
272+
<p>The currency symbol for the Euro is &ldquo;&eur;&rdquo;.</p>
273+
```
274+
275+
### Inline CSS
276+
277+
Inline CSS must be avoided. When altering states using JavaScript, use CSS to define your states, and only use onobtrusive JavaScript to alter class names whenever possible.
278+
```html
279+
<!-- Good -->
280+
<a class="is-link-disabled" href="//index.php">Home</a>
281+
282+
<!-- Bad -->
283+
<a href="//index.php" style="text-decoration: line-through;">Home</a>
284+
```
285+
286+
### Style Attributes
287+
You should not use border, align, valign, or clear attributes. Avoid use of style attributes, except where using syndicated content or internal syndicating systems.
288+
289+
61290
### Semantics
62291
Use HTML according to its purpose. For example, use heading elements for headings, p elements for paragraphs, a elements for anchors, etc.
63292

@@ -70,6 +299,26 @@ Using HTML according to its purpose is important for accessibility, reuse, and c
70299
<div onclick="goToSubscriptions();">View subscriptions</div>
71300
```
72301

302+
303+
304+
### Markup
305+
306+
#### img
307+
308+
### Comments
309+
@todo: comment styles in JS, CSS, HTML
310+
For more complex blocks of HTML, it may be useful to add a comment to the closing tag:
311+
```html
312+
<div class="parent">
313+
314+
<div class="child">
315+
</div><!-- /child -->
316+
317+
</div><!-- /parent -->
318+
```
319+
320+
321+
73322
### Mark todos
74323
Highlight todos by using the keyword TODO, eg:
75324

@@ -80,3 +329,10 @@ Highlight todos by using the keyword TODO, eg:
80329
<li>Blog</li>
81330
</ul>
82331
```
332+
333+
334+
335+
### Markup validation tools
336+
@todo: list various testing tools:
337+
* http://validator.w3.org/nu/
338+
* http://csslint.net/

0 commit comments

Comments
 (0)