Giter VIP home page Giter VIP logo

bem-cheat-sheet's Introduction

BEM Cheat Sheet

BEM (Block, Element, Modifier) is a component-based approach to web development. The idea behind it is to divide the user interface into independent blocks. This makes interface development easy and fast even with a complex UI, and it allows reuse of existing code without copying and pasting.

Block

A functionally independent page component that can be reused. It is meaningful on its own.

  • Block name describes its purpose (menu, button), not its state (red or big)
  • Never use CSS tag (a, span) or ID selectors (#post)
  • A block should not influence its environment, e.g. by setting margins or absolute positioning. This should be done by the parent.
<!-- CORRECT -->
<div class="error"></div>

<!-- INCORRECT -->
<div class="red-text"></div>

Element

A part of a block that cannot be used separately from it.

  • The element name describes its purpose and is separated from the block name with a double underscore (__)
<form class="search-form">
  <input class="search-form__input" />
  <input class="search-form__input" />
  <button class="search-form__button"></button>
</form>

Modifier

An entity that defines the appearance, state, or behavior of a block or element.

  • The modifier name is separated from the block or element by a double dash (--)
  • Can define appearance (button--red) or state (button--disabled)
  • There are two types of modifiers. Boolean (button--large) and key-value pairs (button--text-red)
  • For easier manipulation in JavaScript, we can also use helper modifier classes such as is-active, is-focused etc.
.button {

  &--large {}

  &--text-red {}
}

.menu {

  &__item {

    &.is-active {}
  }
}

Block Nesting

  • Blocks can be nested in each other
  • You can have as many nesting levels as you want
  • An element is always part of a block (post__header), not another element (post__header__author)
  • While the CSS structure is flat, the DOM structure does not need to be. (See post__content and post__image in the following example.)
<article class="post">
  <div class="post__header">
    <div class="post-header">
      <div class="post-header__author"></div>
      <div class="post-header__date"></div>
    </div>
  </div>
  <div class="post__content">
    <img class="post__image" />
  </div>
</article>

While block nesting is allowed in BEM (and absolutely necessary for most websites), blocks should never visually influence one another.

// INCORRECT
.page {

  &--home {

    .post {}
  }
}

Additionally, we should avoid targeting HTML tags and IDs whenever possible.

// INCORRECT
.post {

  p {}

  img {}
}

In some cases however, we might have no other choice. One such example is the base styling of links or other elements throughout the website. The same applies for styling content received from WYSIWYG editors.

// OKAY
a {
  color: currentColor;
}

.content-from-admin {
  p {
    margin-bottom: 1em;
  }
}

SCSS Structure

To keep your SCSS file organized, you should have one block per file, with elements and modifiers nested.

.post {
  border: 1px solid $black;

  &--featured {
    border-width: 2px;
  }

  &__image {
    width: 50%;

    &--large {
      width: 100%;
    }
  }
}

The same applies for mixin includes.

.post {
  font-size: 14px;

  @include mq($md) {
    font-size: 16px;
  }

  &--featured {
    font-size: 16px;

    @include mq($md) {
      font-size: 18px;
    }
  }
}

Breaking BEM Rules

Sometimes, you don't need to strictly stick to the BEM methodology. One such example are block modifiers which should also modify the appearance of child elements.

Consider an unordered list where some of the items should be highlighted. These should include a different colored text and larger images. In an ideal BEM world, this is how we would achieve this:

<ul class="list">
  <li class="list__item">
    <div class="list__text">Lorem ipsum</div>
    <img src="img.jpg" class="list__image" />
  </li>
  <li class="list__item">
    <div class="list__text list__text--featured">Lorem ipsum</div>
    <img src="img.jpg" class="list__image list__image--large" />
  </li>
</ul>
.list {

  &__item {}

  &__text {
    color: $black;

    &--highlighted {
      color: $red;
    }
  }

  &__image {
    width: 100px;
    height: 100px;

    &--large {
      width: 200px;
      height: 200px;
    }
  }
}

However, this means we need to assign modifiers to two different elements. In this case, we might choose to break BEM rules and use a modifier for the whole block instead. (We need to save the block as a variable, in this case $s for selector.)

<ul class="list">
  <li class="list__item">
    <div class="list__text">Lorem ipsum</div>
    <img src="img.jpg" class="list__image" />
  </li>
  <li class="list__item list__item--highlighted">
    <div class="list__text">Lorem ipsum</div>
    <img src="img.jpg" class="list__image" />
  </li>
</ul>
.list {
  $s: &;

  &__item {}

  &__text {
    color: $black;
  }

  &__image {
    width: 100px;
    height: 100px;
  }

  &--highlighted {

    #{$s}__text {
      color: $red;
    }

    #{$s}__image {
      width: 200px;
      height: 200px;
    }
  }
}

bem-cheat-sheet's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vuongvgc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.