For writing maintainable and scalable HTML documents
- Avoid UNNECESSARY markup, such as surrounding a block element in a
<div>
DOCTYPE is required for activating standard mode.
DOCTYPE is not for DTD anymore, be simple.
Don’t use XML Declaration.
Bad:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
...
</html>Good:
<!DOCTYPE html>
<html>
...
</html>Tags and attributes should be in lowercase
Bad:
<DIV>
<UL>
<li>Item 1</li>
<li>Item 2</li>
</UL>
</DIV>
<INPUT TYPE="text">Good:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<input type="text">Add closing tags to elements that aren't self-closing
Bad:
<body>
<ul>
<li>Item 1
<li>Item 2
</ul>Good:
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</body>*Exception is unclosed <li> tags, when you use display:inline-block; to avoid unnecessary space between items
Don't add the forward slash for self-closing elements (img, br, hr, input)
Bad:
<ul>
<li>Item 1
<li>Item 2
</ul>Good:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>Bad:
<ul>
<li>Item 1</li><li>Item 2</li><li>Item 3</li>
</ul>
<tr>
<td>Cell 1</td><td>Cell 2</td><td>Cell 3</td>
</tr>Good:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>Put a single line break between blocks/components.
Bad:
<section class="component1">
...
</section>
<section class="component2">
...
</section>Good:
<section class="component1">
...
</section>
<section class="component2">
...
</section>alt attribute helps those who cannot process images or have image loading disabled. If img used as presentation element(you should try to avoid this), you can leave alt empty
Bad:
<img src="/img/logo.png">Good:
<img alt="Description of image" src="/img/logo.png">lang attribute will help translating an HTML document
Bad:
<html>Good:
<html lang="en-US">Specify document character encoding, UTF-8 is not default in all browsers yet
Good:
<head>
<meta charset="UTF-8">
</head>Spec requires the character encoding is specified within the first 1024 bytes of the document.
Bad:
<head>
<meta content="width=device-width" name="viewport">
...
<meta charset="UTF-8">
</head>Good:
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width" name="viewport">
...
</head>HTTP headers should be specified by a server, be simple.
Bad:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">Good:
<meta charset="UTF-8">Add all your links to stylesheets in <head>
Bad:
<html lang="ru">
<head>…</head>
<body>
<link rel="stylesheet" href="styles.css">
</body>
</html>Good:
<html lang="ru">
<head>
<link rel="stylesheet" href="styless.css">
</head>
<body>…</body>
</html>Sometimes body element is complemented in unexpected position by a browsers
Bad:
<html>
<head>
...
</head>
...
</html>Good:
<html>
<head>
...
</head>
<body>
...
</body>
</html>Add all your scripts before </body>
Bad:
<!DOCTYPE html>
<html lang="ru">
<head>
<script src="app.js"></script>
</head>
<body>…</body>
</html>Good:
<!DOCTYPE html>
<html lang="ru">
<head>…</head>
<body>
<!-- your page -->
<script src="app.js"></script>
</body>
</html>Surround values for attributes in double quotes. Even though quotes around attributes is optional, always put quotes around attributes for readability.
Bad:
<input type='radio' class='input'>Good:
<input type="radio" class="input">Boolean attributes(checked, disabled, required), do not need a value.
Bad:
<input type='radio' required='true'>Good:
<!DOCTYPE html>
<input type="radio" required>Just don't do this
Bad:
<section class=" component " ></h1>Good:
<section class="component"></h1>Follow this order for attributes:
- href, type, value
- class
- id
- name
- for, value, etc
- data-attributes
- checked, disabled, required
Bad:
<input required class="input" type="radio" >Good:
<input type="radio" class="input" required>
<a href="#" class="link"></a>Every form input that has text attached should utilize a <label> tag. Especially radio or checkbox elements.
Omit for attribute if possible(1st variant).
Bad:
<input type="radio" id="choose"> Radio buttonGood:
<label>
<input type="radio"> Radio button
</label>
<input type="radio" id="choose">
<label for="choose">Radio button</label>Form buttons should always include an explicit type. Use primary buttons for the type="submit" button and regular buttons for type="button"
Bad:
<form>
<input type="text">
<button>Say hi</button>
<button>Submit form</button>
</form>Good:
<form>
<input type="text">
<button type="button">Say hi</button>
<button type="submit">Submit form</button>
</form>- HTML is about structure and meaning, not style or behavior (don't use h(1,2,3) elements to make you font larger)
- Don't use styling tags such as
<bold>and<hr> - Paragraphs of text should always be placed in a
<p>tag. Never use multiple<br>tags - Items in list form should always be in
<ul>,<ol>, or<dl>. Never use a set of<div>or<p> - Omit type attribute for CSS. In HTML5, default
typeattribute’s value ofstyleelement istext/css - Omit type attribute for JavaScript. In HTML5, the default
typeattribute’s value ofscriptelement istext/javascript - Forget about hgroup element. W3C spec removes this element