CSS Style Guide

General Guidelines

General Naming Rules

  • Use dashes rather than camelCase or PascalCase
  • When possible, block level class names should reflect the name of the element given during the roadmapping phase.
  • Element names should be succinct
  • Class names should be written as [ITCSS_PREFIX]-[BLOCK_NAME]__[ELEMENT_NAME]--[MODIFIER_NAME].

Styling using IDs

NEVER STYLE USING ID SELECTORS. Just don’t do it thank you :)

Syntax

  • Use the .scss syntax, never the original .sass syntax
  • Order your regular CSS and @include declarations logically (see below)

Ordering of property declarations

  1. Property declarations

    List all standard property declarations, anything that isn’t an @include or a nested selector.

    .btn-green {
      background: green;
      font-weight: bold;
      // ...
    }
  2. Nested selectors

    Nested selectors, if necessary, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors.

    .btn {
      background: green;
      font-weight: bold;
      @include transition(background 0.5s ease);
    
      .icon {
        margin-right: 10px;
      }
    }
  3. BEM

    When ordering BEM classes, group modifiers with their root element:

    .c-teaser-card { ... }
    .c-teaser-card--black { ... }
    
    .c-teaser-card__header { ... }
    .c-teaser-card__header--left { ... }
    
    .c-teaser-card__title { ... }
    .c-teaser-card__title--large { ... }

Variables

Prefer dash-cased variable names (e.g. $my-variable) over camelCased or snake_cased variable names. It is acceptable to prefix variable names that are intended to be used only within the same file with an underscore (e.g. $_my-variable).

Color variables should be written with similar name-spacing when appropriate:

Bad

$blue: #4049FF;
$light-blue: #AFBBFF;
$transparent-banana: transparentize(#FFEA16, 0.5);

Good

$blue: #4049FF;
$blue-light: #AFBBFF;
$banana-transparent: transparentize(#FFEA16, 0.5);

Mixins

Mixins should be used to DRY up your code, add clarity, or abstract complexity–in much the same way as well-named functions. Mixins that accept no arguments can be useful for this, but note that if you are not compressing your payload (e.g. gzip), this may contribute to unnecessary code duplication in the resulting styles.

Font Mixins

Basic font styles should be written and included as mixins. Doing this allows repeat usage of font styles across the project. Base font classes should also be used for headings - Never style H1 - h6 - reset heading styles and style them with classes. Font mixins should not declare margins or padding - just font-based styles.

@mixin title-serif-lg() {
  font-size: 7.2rem;
  font-weight: 900;
}

.f-title-serif-lg() {
  @include title-serif-lg();
  margin-bottom: 2rem;
}

Extend directive

Do not use @extend directives. Gzipping should handle most of the savings you would have gained by using @extend, and you can DRY up your stylesheets nicely with mixins.

Nested selectors

Do not nest selectors more than three levels deep!

.page-container {
  .content {
    .profile {
      // STOP!
    }
  }
}

When selectors become this long, you’re likely writing CSS that is:

  • Strongly coupled to the HTML (fragile) —OR—
  • Overly specific (powerful) —OR—
  • Not reusable

Again: never nest ID selectors!

If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you are writing well formed HTML and CSS, you should never need to do this.

Nesting BEM selectors

When writing sass for BEM elements, do not nest element or modifier suffixes. This helps improve searching in an IDE:

Bad

.c-teaser-card {

  &--black {
    // Stuff
  }

  &__header {
    // Stuff
  }

  &__title {
    // Stuff
  }
}

Good

.c-teaser-card {
  // Stuff
}

.c-teaser-card--black { ... }

.c-teaser-card__header { ... }

.c-teaser-card__title { ... }

⬆ back to top

SCSS File Location

Scss files should be saved in the same directory as the Fractal component, rather than in the assets/scss directory. Fractal component scss files can then be imported with a glob statement in a base scss file.