Skip to content

Instantly share code, notes, and snippets.

@urre
Created May 27, 2014 10:15
Show Gist options
  • Save urre/cf970b8c4f7839076b92 to your computer and use it in GitHub Desktop.
Save urre/cf970b8c4f7839076b92 to your computer and use it in GitHub Desktop.
BEM and SCSS

BEM

Advantages of using BEM syntax for CSS are:

  1. Design of the website can change at any time, we must be ready for this HTML/CSS code is developing together with the design, ready to its changes
  2. Programmer and front-end developers are working together on the website codebase, contributing to each other's code
  3. Our CSS classes are easier for other team members to understand as they can easily identify the components in the HTML and also the purpose of the CSS classes.
  4. As classes are prefixed with the block it reduces the likelihood of accidentally having two classes with the same name, useful for larger projects.
  5. Using a naming convention gives your CSS and HTML more consistency making it easier to share code between projects.

BEM naming convention

.block {}
.block__element {}
.block--modifier {}

BEM in SCSS 3.3

Basic structure, example

.block {

  &__element {

  }
  &--modifier {

  }
}

HTML

<div class="module">
    <h2 class="module__heading module__heading--smaller">Lorem Ipsum</h2>
</div>

SCSS

.module {
  background: grey;
  color: white;

    &__heading {
      font-size: 1em;
    }

    &--heading--smaller {
      font-size: .8em;
    }
}

And this compiles to...

CSS

.module {
  background: grey;
  color: white;
}
.module__heading {
  font-size: 1em;
}
.module__heading--smaller {
  font-size: .8em;
  color: red;
}

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment