KISSmetrics Style Guides

 › CSS Styleguide

Table of Contents

Coding-style

CSS

Properties

# Bad
a {
  color:#F00;
}

# Good
a {
  color: #F00;
}
# Bad
a {
  color: whitesmoke;
}

# Good
a {
  color: #F5F5F5;
}

Tags that affect document flow include: position, display, top, margin

These tags affect more than just presentational content inside of their respective containers, and moving them to the top of their selector helps with readability.

# Bad
h1 {
  background-color: #333;
  left: 0;
  position: fixed;
  top: 0;
}

# Good
h1 {
  position: fixed;
  top: 0;
  left: 0;
  background-color: #333;
}

Selectors

# Bad
div.container {}

# Good
.container {}
# Bad
ul.nav li a {}

# Good
.nav a {}
# Bad
.selector { color: #444; }

# Good
.selector {
  color: #444;
}
# Bad
.selector-1, .selector-2, .selector-3 {
  background: #fff;
  color: #000;
}

# Good
.selector-1,
.selector-2,
.selector-3 {
  background: #fff;
  color: #000;
}

Shorthand

Shorthand should be used within reason and caution.

# Bad
div {
  margin-top: 10px;
  margin-right: 25px;
  margin-bottom: 10px;
  margin-left: 25px;
}

# Good
div {
  margin: 10px 25px;
}

Classes

When creating a class name, be semantic and label the function of the element you are naming. Using classes that aren't semantic only serves as a shortcut to writing inline CSS.

Bad class names: pink float-left align-center one-and-a-half xy

Good class names: sidebar post author cta description

A good rule of thumb to figure out if your class name makes sense is to look at the class names that lead up to it.

<article class='blog-post'>
  <header>
    <h1 class='title'>On the Shortness of Life</h1>
    <div class='author'>
      <h3 class='name'>Seneca</h3>
      <img src='...'>
    </div>
  </header>
  <div class='align-left'>
    The majority of mortals, Paulinus, complain bitterly...
  </div>
</article>

Here, it's easy to understand layout, even if you don't know what the text is. .blog-post .author .name reads very coherently illustrates the content inside. .blog-post .align-left doesn't give any indication of content. Don't use multiple classes as a way to skirt this principle.

SCSS

Nesting

# Bad
.post {

  header {
    background-color: #FFF;
  }

  width: 600px;
}

# Good
.post {
  width: 600px;

  header {
    background-color: #FFF;
  }
}
# Bad
.post {
  width: 600px;
  header {}
}

# Good
.post {
  width: 600px;

  header {}
}
# Bad
.post {

  header {}

  &:hover {}
}

# Good
.post {

  &:hover {}

  header {}
}
# Bad
.post {

  header {}

  @media screen and (max-width: 640px) {}
}

# Good
.post {

  @media screen and (max-width: 640px) {}

  header {}
}
.post {

  header {

    nav {
      // no more!
    }
  }
}

Variables

Variables were created for a few reasons:

Variables fall into two categories: globals and helpers.

Global variables are there to enforce the overarching themes present in a site. Background colors, font colors, typefaces, etc. They should be used with a consistent prefix such as base or primary to illustrate their relevance in a broad context.

Helper variables serve a different role. They are variables whose purpose fits into a niche context. Things like error styling, promotional styling, and CTAs can all use helper variables.

# Bad
$baseBackgroundColor

# Good
$baseWhite
# Bad
$primaryWhite
$baseBlue

# Good
$baseWhite
$baseBlue
# Bad
$comicsans:       Comic Sans MS, sans-serif;
$papyrus:         Papyrus, fantasy;

# Good
$baseSansSerif:   'Open Sans', sans-serif;
$baseSerif:       'Open Serif', serif;
# Bad
$secondRed
$limeGreen
$ctaBackground

# Good
$errorRed
$successGreen
$ctaYellow

Variable files should not contain actionable css, only property assignments. As such, use a vertical column layout to make the page more readable.

# bad
$baseSansSerif: 'Open Sans', sans-serif;
$baseSerif: 'Open Serif', serif;

# Good
$baseSansSerif:   'Open Sans', sans-serif;
$baseSerif:       'Open Serif', serif;

Organization

File structure should marry the concept of atomic CSS with the best practices of the platform serving the application. Assuming a rails applicatons, consider a file structure like the following:

stylesheets
├── globals
│   ├── mixins.scss
│   └── variables.scss
├── atoms
│   ├── buttons.scss
│   ├── fields.scss
│   ├── headings.scss
│   ├── links.scss
│   └── paragraphs.scss
├── molecules
│   ├── articles.scss
│   ├── forms.scss
│   └── headers.scss
├── layouts
│   ├── fullwidth.scss
│   └── signupsidebar.scss
├── application.scss
└── home.scss

Comments

# Bad
/* Let's simplify this line before release */

# Good
// Causing an overflow bug in Firefox 23.0.1. Discussion at github.com

Vendor Prefixes

# Bad
-webkit-box-shadow: inset 0 0 1px 1px #eee;
-moz-box-shadow: inset 0 0 1px 1px #eee;
box-shadow: inset 0 0 1px 1px #eee;

# Good
-webkit-box-shadow: inset 0 0 1px 1px #eee;
   -moz-box-shadow: inset 0 0 1px 1px #eee;
        box-shadow: inset 0 0 1px 1px #eee;