Skip to content

Instantly share code, notes, and snippets.

@incompl
Last active December 28, 2015 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save incompl/7570916 to your computer and use it in GitHub Desktop.
Save incompl/7570916 to your computer and use it in GitHub Desktop.
A Starting Point for CSS Styleguides

A Starting Point for CSS Styleguides

This is a CSS styleguide that you could fork to use for your project. Some of the rules give you multiple acceptable options. If you want to adopt this styleguide for your project, you should modify it for your project.

Here is an example styleguide made from this starting point

This styleguide follows my styleguide for styleguides. That means it follows the following rules:

  • Focus on readability.
  • Focus on consistency.
  • Follow common practice unless you have good reason.

Terminology

/* this whole thing is a rule */
selector {
  property: value; /* this line is a declaration */
}

Basic stuff

  • Write valid CSS
  • Use UTF-8
  • Put a space after :
  • Put { on same line as selector
  • Put a space before {
  • Put each rule on its own line

Example:

/* comment */
.egg {
  color: white;
  background: black;
}

Aligning style values

Some projects choose to align style values, like this:

/* comment */
.egg {
  color:      white;
  background: black;
}

If your project does this, do it consistently.

Comments

Only /* */ comments are valid in plain CSS

Some projects require that comments begin on a new line, but this is generally not a strict rule.

Units

Prefer these units:

  • %
  • em
  • px

Avoid thse units:

  • in
  • cm
  • mm

Use consistent indenting

It should be the same as the indenting used across your project, such as in JavaScript. Common options are:

  • two space
  • four space
  • tab character

Use consistent ID / class name formatting

Use one of the following:

  • camelCase
  • dashes-are-nice
  • underscores_are_ok

If you use dashes or underscores, use all lowercase.

ProperCase is not a common convention in the CSS community so you should probably not use it.

Use consistent quotes

Either ' or "

Preferably the same as in your JavaScript.

Declaration Order

I suggest omitting declaration order from your styleguide. Effectively you're deciding between spending time maintaining the order and spending time finding declarations. Since most rules contain a very small number of declarations it doesn't seem worthwhile to make this optimization.

If you feel that declarations must be in a particular order, here are some options:

You can group rules that start with the same prefix. For example, group font-family and font-size together.

You can group rules according to their function. source

You can put rules in alphabetical order. source

Do not use Duplicate Properties

Do not put the same property twice in the same rule.

/* bad */
.egg {
  color: red;
  color: white;
}

This has no special semantics and is probably a simple mistake.

Do not use the star or underscore hack

The star hack looks like this:

.egg {
  width: 100px;
  *width: 200px;
}

In this example, 200px width is used in IE7 and below. This is invalid CSS. A separate IE7 stylesheet might be better.

The underscore hack is the same thing but with _ instead of *.

Prefer Shorthand

Prefer shorthand unless there is a specific reason not to. For example:

.bad {
  margin-top: 1em;
  margin-right: 2em;
  margin-bottom: 3em;
  margin-left: 4em;
}

.good {
  margin: 1em 2em 3em 4em;
}

This is not a strict rule because there are cases where longhand might be considered more readable. For example:

/* easy to miss the extra 2 properties */
.shorthand {
  background: url("http://example.com/doglife/eggs/cool/backgrounds/background.png") #fff repeat-y;
}

/* more redundant but less likely to misread */
.longhand {
  background-image: url("http://example.com/doglife/eggs/cool/backgrounds/background.png");
  background-color: #fff;
  background-repeat: repeat-y;
}

Avoid !important.

It might be appropriate to use !important in some situations, such as paving over flaws in 3rd party CSS that you don't have control over. In most situations, use of !important can be replaced with better inheritence.

Here are some CSSLint options options for your SublimeLinter.sublime-settings
These enforce the non-optional rules of this styleguide and nothing else.
"csslint_options":
{
"adjoining-classes": false,
"box-model": false,
"box-sizing": false,
"compatible-vendor-prefixes": false,
"display-property-grouping": false,
"duplicate-background-images": false,
"duplicate-properties": true,
"empty-rules": false,
"errors": true,
"fallback-colors": false,
"floats": false,
"font-faces": false,
"font-sizes": false,
"gradients": false,
"import": false,
"important": false,
"known-properties": true,
"outline-none": false,
"overqualified-elements": false,
"qualified-headings": false,
"regex-selectors": false,
"rules-count": false,
"shorthand": false,
"star-property-hack": true,
"text-indent": false,
"underscore-property-hack": true,
"unique-headings": false,
"universal-selector": false,
"vendor-prefix": false,
"zero-units": false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment