A Style Guide For HTML & CSS
Last updated
Last updated
Back in the mid 2010s I was an Instructor for the , a bootcamp program that offered several different tracts to help individuals get entry-level jobs at startups. I wrote this style guide to help my students learn HTML and CSS:
This style guide is intended for students enrolled in Startup Institute's RampUp Web Design class, but is useful for anyone learning HTML & CSS. This document outlines guidelines, best practices, and common approaches to writing clean maintainable HTML & CSS files.
Names for your project, files, and folders should be short, lowercase, and use dashes instead of spaces. This improves readability both in and outside of your code.
Keep your files organized by their type. Files should be kept in the following folders:
my-project/
HTML Documents
.html
my-project/css
Stylesheets
.css
my-project/fonts
Web Fonts
.wotff
my-project/img
Images
.jpg
.gif
.png
.svg
my-project/js
Scripts
.js
Nested (child) elements should start on a new line and be indented within the parent element.
Always use double quotes, never single quotes, on attributes.
Include a trailing slash in self-closing elements
Don’t omit optional closing tags
Every html document should start with the DOCTYPE tag.
Quickly and easily ensure proper rendering of your content by declaring an explicit character encoding. When doing so, you may avoid using character entities in your HTML, provided their encoding matches that of the document (UTF-8).
Stylesheets are nested in the <head>
section of your HTML document.
For unique attributes like id
& name
should follow the camelCasing
convention.
Class names should be formatted using kabab-case.
HTML attributes should come in this particular order for easier reading of code: - `id`, `name` - `class` - `data-*` - `src`, `for`, `type`, `href`, `value` - `title`, `alt` - `role`, `aria-*`
Attributes which uniquely identify an element (id
, name
) should come first for ease of readability, followed by classes, which are typically the most common attribute found across all elements in an HTML document.
Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML.
Include one space before the opening brace of declaration blocks for readability. ```css // Incorrect .foo{display: block;}
// Correct .foo {display: block;}
Declarations with multiple property values should include a space after each comma
Avoid specifying units for zero values
Keep selectors short and strive to limit the number of elements in each selector to three.
Selectors with a single declaration can be kept on a single line.
For selectors with multiple declarations:
Each declaration should appear on its own line
Declarations are nested within the selector block
Place closing braces of declaration blocks on a new line
Declarations should be arranged alphabetically within selectors since in-browser developer tools will list declarations this way. ```css // Incorrect .foo { position: relative; top: 0; left: 12px; display: block; }
// Correct .foo { display: block; left: 0; position: relative; top: 0; }
Heavily inspired by . Made with <3 in Chicago. Open Source under MIT. Copyright 2015 Joe Mainwaring.