BEM

In frontend-land we use BEM notation to name our classes.

Don't forget to use overall REM instead of PX (maybe for "box-shadows" and "borders" properties if you don't want them to scale.)

Short for Blocks, Elements and Modifiers.

  • A Block — represents a standalone entity.
  • A Element — represents a part of a block semantically tied to its block.
  • A Modifier —represents a flag on a block or element which can change its behavior.

First off, wait… Are your telling me the symbols -- and __ have a different meaning in BEM?

  1. .block
  2. block__element
  3. block--modifier

Example Card:

  • In this example, we have a block called “card,” which contains a content element and a button element. The button element also has a “primary” variation, which acts as a modifier. The HTML code defines the structure of the card component, using a div with a class of “card” as the main container.
  • The card represents a block. Inside the card container, there are various other elements, such as a content area and a button. Each of these elements has a class name that starts with “card__,” which indicates that they are children of the “card” block. The class “card__content” is applied to the content area. The class “card” is used to style the main container of the card component and all its children elements.
  • The modifier is added as a class to the button element, following the BEM naming convention. The class name “card__button–primary” indicates that it is a variation of the “card__button” element. The CSS code uses this class to style the primary button differently from the default button.
html
<div class="card"> 
  <div class="card__content"> 
    <h3 class="card__title">Card Title</h3> 
    <p class="card__text">Card text</p> 
  </div> 
  <button class="card__button">Button</button> 
  <button class="card__button card__button--primary"> 
    Primary Button 
  </button> 
</div>
css
.card { 
  background-color: rgb(45, 97, 66); 
  border-radius: 0.8rem; 
  width: 50%; 
  display: flex; 
  flex-direction: column; 
  align-items: center; 
  justify-content: center; 
  margin: 10rem auto; 
  box-shadow: 0 0.2rem 0.5rem 0.2rem #aaaaaa; 
  padding: 1rem; 
} 


/* The Elements follow the naming convention  
mentioned as: .block__element */ 

.card__content { 
  padding: 1.6rem; 
} 

.card__title { 
  font-size: 3rem; 
  font-weight: bold; 
  margin-bottom: 0.8rem; 
} 

.card__text { 
  font-size: 2rem; 
  color: gray; 
  text-align: center; 
} 

.card__button { 
  background-color: white; 
  color: black; 
  border-radius: 0.4rem; 
  padding: 1rem; 
  text-transform: uppercase; 
  margin: 1rem; 
  cursor: pointer; 
  box-shadow: 0 0.2rem 0.5rem 0.2rem #aaaaaa; 
  font-weight: bold; 
} 


/* The Modifiers follow the naming convention  
    mentioned as: .block__element--modifier */ 

.card__button--primary { 
  background-color: blue; 
  margin: 1rem; 
  cursor: pointer; 
}
sass
  card { 
    background-color: rgb(45, 97, 66); 
    border-radius: 0.8rem; 
    width: 50%; 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    justify-content: center; 
    margin: 10rem auto; 
    box-shadow: 0 0.2rem 0.5rem 0.2rem #aaaaaa; 
    padding: 1rem;

  /* The Elements follow the naming convention  
  mentioned as: .block__element */ 

    &__content { 
      padding: 1.6rem; 
    }

    &_title { 
      font-size: 3rem; 
      font-weight: bold; 
      margin-bottom: 0.8rem; 
    }

    &_text { 
      font-size: 2rem; 
      color: gray; 
      text-align: center; 
    } 

    &__button { 
      background-color: white; 
      color: black; 
      border-radius: 0.4rem; 
      padding: 1rem; 
      text-transform: uppercase; 
      margin: 1rem; 
      cursor: pointer; 
      box-shadow: 0 0.2rem 0.5rem 0.2rem #aaaaaa; 
      font-weight: bold;

      /* The Modifiers follow the naming convention  
      mentioned as: .block__element--modifier */ 

      &--primary { 
        background-color: blue; 
        margin: 1rem; 
        cursor: pointer; 
      } 
    }
}

Example Form:

  • The form block is represented by the class “form“. This is the main container for the form and it is used to apply styles that affect the entire form.
  • The labels and inputs are elements of the form block, they are represented by the classes “form__label” and “form__input” respectively. These elements are used to apply styles that affect only the labels and inputs of the form.
  • The submit button is also an element of the form block and is represented by the class “form__submit“. It is used to apply styles that affect only the submit button of the form.
  • The submit button also has a modifier called “disabled” which is represented by the class “form__submit–disabled“. This modifier is used to apply styles that change the appearance of the submit button when it is disabled.
html
<form class="form">
  <!
  <label class="form__label" for="name"> 
      Name: 
  </label> 
  <input class="form__input" id="name" type="text" placeholder="Enter your name">

  <label class="form__label" for="email"> 
      Email: 
  </label> 
  <input class="form__input" id="email" type="email" placeholder="Enter your email">

  <label class="form__label" for="password"> 
      Password: 
  </label>
  <input class="form__input" id="password" type="password" placeholder="Enter your password"> 

  <button class="form__submit form__submit--disabled" type="submit" disabled>Sign Up</button> 
</form>
css
 /* Block */ 
  .form { 
    display: flex; 
    flex-wrap: wrap; 
    width: 40rem; 
  } 
    
  /* Element */ 
  .form__label { 
    width: 100%; 
    margin-bottom: 1rem; 
    font-size: 1.8rem; 
  } 
    
  /* Element */ 
  .form__input { 
    width: 100%; 
    padding: 1rem; 
    font-size: 1.6remx; 
    margin-bottom: 2rem; 
  } 
    
  /* Element */ 
  .form__submit { 
    background-color: #007bff; 
    color: #fff; 
    padding: 1rem 2rem; 
    border: none; 
    font-size: 1.8rem; 
  } 
    
  /* Modifier */ 
  .form__submit--disabled { 
    background-color: #ccc; 
    cursor: not-allowed; 
  }
sass
 /* Block */ 
  .form { 
    display: flex; 
    flex-wrap: wrap; 
    width: 40rem;

    /* Element */ 
    &__label { 
      width: 100%; 
      margin-bottom: 1rem; 
      font-size: 1.8rem; 
    } 
      
    /* Element */ 
    &__input { 
      width: 100%; 
      padding: 1rem; 
      font-size: 1.6rem; 
      margin-bottom: 2rem; 
    } 
      
    /* Element */ 
    &__submit { 
      background-color: #007bff; 
      color: #fff; 
      padding: 1rem 2rem; 
      border: none; 
      font-size: 1.8rem;

    /* Modifier */ 
     & --disabled { 
        background-color: #ccc; 
        cursor: not-allowed; 
      } 
    } 
  }
✨ Well done! You now know all about BEM notation If you have any questions don't hesitate asking your colleagues!