Skip to content

Instantly share code, notes, and snippets.

@rockwood
Created April 14, 2016 19:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rockwood/5a473bb04f2575aff7bcf0ca344f3fae to your computer and use it in GitHub Desktop.
Save rockwood/5a473bb04f2575aff7bcf0ca344f3fae to your computer and use it in GitHub Desktop.
BEM Conventions

Dos:

Avoid page specific stylesheets

It's easy to organize styles by page when the app is small, but a year from now we'll probably have 50+ pages and having a separate stylesheet for each page will result in tons of duplication.

Use the grid as much as possible

The grid has a ton of feature and it's really customizable from variables.scss. Anywhere you're doing multi-column layouts, try to use the grid.

Prefer smaller, isolated components at first

It's easy to want to reuse your components for things that might actually be different things. It's ok to duplicate styles at first and unify things later on. It's easier to combine two components into one than it is to pull apart an overly complex component.

Use utility classes

Much of the basic styling can be accomplished with utility classes. Things like padding, centering text, highlighting can be done with utility classes. It's totally ok to add utility classes yo your component elements.

Structuring CSS on the filesystem

See our blog post

Each component has it's own file

A component with the class .search-box should live in a file called components/search-box.scss. All subelements (ex: search-box__input) should live in that file as well. By keeping a one-to-one mapping of components to files, it's easy for a developer to inspect the DOM and know exactly where to find the file for a given component.

BEM conventions

See the CSS Tricks BEM tutorial

For an element with the class .search-box--highlighed. search-box is the block and highlighed is the modifier.

An element cannot have a modifier class without also having it's corresponding block class.

Invalid:

<div class="search-box--highlighed"></div>

Valid:

<div class="search-box search-box--highlighed"></div>

An element that uses subelement notation (__) must be within an element of the corresponding component. .search-box__input must descend from an element with the class .search-box

Invalid:

<div class="search-box">
  <input class="form__input"></input>
</div>`

This is invalid because the form__input must live within a component with the class form

Valid:

<div class="search-box">
  <input class="search-box__input"></input>
</div>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment