• Stars
    star
    525
  • Rank 81,152 (Top 2 %)
  • Language
  • Created almost 12 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Principles of writing consistent, idiomatic HTML.

Principles of writing consistent, idiomatic HTML

unmaintained

The following document outlines a reasonable style guide for HTML development. These guidelines strongly encourage the use of existing, common, sensible patterns. They should be adapted as needed to create your own style guide.

This is a living document and new ideas are always welcome. Please contribute.

Table of contents

  1. General principles
  2. Whitespace
  3. Format
  4. Attribute order
  5. Naming
  6. Practical example

License

1. General principles

  • All code in any code-base should look like a single person typed it, no matter how many people contributed.
  • Strictly enforce the agreed upon style.
  • If in doubt when deciding upon a style, use existing, common patterns.

2. Whitespace

Only one style should exist across the entire source of your code-base. Always be consistent in your use of whitespace. Use whitespace to improve readability.

  • Never mix spaces and tabs for indentation.
  • Choose between soft indents (spaces) or real tabs. Stick to your choice without fail. (Preference: spaces)
  • If using spaces, choose the number of characters used per indentation level. (Preference: 4 spaces)

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.

3. Format

  • Always use lowercase tag and attribute names.
  • Write one discrete element per line.
  • Use one additional level of indentation for each nested element.
  • Use valueless boolean attributes (e.g. checked rather than checked="checked").
  • Always use double quotes to quote attribute values.
  • Omit the type attributes from link stylesheet, style and script elements.
  • Always include closing tags.
  • Don't include a trailing slash in self-closing elements.

(Keep line-length to a sensible maximum, e.g., 80 columns.)

Example:

<div class="tweet">
    <a href="path/to/somewhere">
        <img src="path/to/image.png" alt="">
    </a>
    <p>[tweet text]</p>
    <button disabled>Reply</button>
</div>

Exceptions and slight deviations

Elements with multiple attributes can have attributes arranged across multiple lines in an effort to improve readability and produce more useful diffs.

Example:

<a class="[value]"
 data-action="[value]"
 data-id="[value]"
 href="[url]">
    <span>[text]</span>
</a>

4. Attribute order

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.

  1. class
  2. id
  3. data-*
  4. Everything else

Example:

<a class="[value]" id="[value]" data-name="[value]" href="[url]">[text]</a>

5. Naming

Naming is hard, but very important. It's a crucial part of the process of developing a maintainable code base, and ensuring that you have a relatively scalable interface between your HTML and CSS/JS.

  • Use clear, thoughtful, and appropriate names for HTML classes. The names should be informative both within HTML and CSS files.
  • Avoid systematic use of abbreviated class names. Don't make things difficult to understand.

Example with bad names:

<div class="cb s-scr"></div>
.s-scr {
  overflow: auto;
}

.cb {
  background: #000;
}

Example with better names:

<div class="column-body is-scrollable"></div>
.is-scrollable {
    overflow: auto;
}

.column-body {
    background: #000;
}

6. Practical example

An example of various conventions.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Document</title>
        <link rel="stylesheet" href="main.css">
        <script src="main.js"></script>
    </head>
    <body>
        <article class="post" id="1234">
            <time class="timestamp">March 15, 2012</time>
            <a data-id="1234"
             data-analytics-category="[value]"
             data-analytics-action="[value]"
             href="[url]">[text]</a>
            <ul>
                <li>
                    <a href="[url]">[text]</a>
                    <img src="[url]" alt="[text]">
                </li>
                <li>
                    <a href="[url]">[text]</a>
                </li>
            </ul>

            <a class="link-complex" href="[url]">
                <span class="link-complex__target">[text]</span>
                [text]
            </a>

            <input value="text" readonly>
        </article>
    </body>
</html>

License

Principles of writing consistent, idiomatic HTML by Nicolas Gallagher is licensed under the Creative Commons Attribution 3.0 Unported License. This applies to all documents and translations in this repository.

Based on a work at github.com/necolas/idiomatic-html.

More Repositories

1

normalize.css

A modern alternative to CSS resets
CSS
51,663
star
2

react-native-web

Cross-platform React UI packages
JavaScript
21,280
star
3

idiomatic-css

Principles of writing consistent, idiomatic CSS.
6,603
star
4

dotfiles

OS X dotfiles: bash, git, vim, etc.
Shell
781
star
5

css3-facebook-buttons

Simple CSS to recreate the appearance of Facebook's buttons and toolbars.
CSS
576
star
6

css3-social-signin-buttons

CSS3 Social Sign-in Buttons with icons. Small and large sizes.
CSS
474
star
7

issue-guidelines

A set of guidelines for submitting issues and pull requests on projects
425
star
8

griddle

A CSS grid constructor
CSS
366
star
9

styleq

styleQ is a quick, small JavaScript runtime for merging the HTML class names produced by CSS compilers.
JavaScript
183
star
10

icon-builder-example

JavaScript
147
star
11

xdm.js

JSON-RPC 2.0 cross-domain communication over postMessage
JavaScript
89
star
12

emitter.js

A simple JavaScript event-emitter
JavaScript
84
star
13

jekyll-boilerplate

A jekyll starting point.
HTML
73
star
14

generator-jsmodule

A Yeoman generator to scaffold out a simple JS module for Node.js or the browser
JavaScript
68
star
15

chrome-tab-limit

Chrome browser extension that limits you to 20 open tabs
JavaScript
54
star
16

suit-grid-layouts

DEPRECATED
HTML
46
star
17

project-euler

JavaScript solutions to the Project Euler problems.
JavaScript
45
star
18

delegate.js

DOM event delegation component
JavaScript
44
star
19

custom-tweet-button-for-wordpress

A fully customisable HTML and CSS Tweet Button for Wordpress. Built using PHP; no JavaScript requirement. Uses the bit.ly and Twitter APIs.
PHP
36
star
20

dom-shims

Complete and well-tested shims / polyfills for various DOM API's missing in not-so-modern browsers
JavaScript
34
star
21

eraser.css

CSS `!important`-fueled bulldozer
CSS
29
star
22

newsriver-ui

User interface for displaying a river of news generated by Dave Winer's River2 software.
JavaScript
27
star
23

dom-closest

For a given DOM element, find the first ancestor that matches a given CSS selector.
JavaScript
22
star
24

event-shim.js

A W3C DOM Event API shim for IE 8
JavaScript
19
star
25

dom-matches

Check if a DOM element matches a given selector.
JavaScript
19
star
26

matches.js

DEPRECATED
JavaScript
12
star
27

dom-insert-html

HTML-string DOM insertion methods using 'insertAdjacentHTML'
JavaScript
12
star
28

dom-siblings

Find a DOM element's siblings, optionally filtered by a CSS selector.
JavaScript
10
star
29

dom-insert

DOM insertion methods.
JavaScript
9
star
30

dom-responder-events

4
star
31

flight-mustache

A Flight component for rendering Mustache templates with mustache.js
JavaScript
2
star
32

webpack-bugs

JavaScript
1
star