Closing a Knowledge Gap: CSS Naming Conventions - BEM
Published: 2023-02-18 19:24:17


When I was learning CSS I was always just naming at random I might have been `.navBar` or `.nav-bar` or `.navarea`. I was just all over the place, it lead to many frustrating days of hunting down why my CSS wasn't working and the answer was usually in my HTML I had  `class="navBar"` and my CSS is was trying `.nav-bar{}`.


## BEM:

BEM stands for "Block Element Modifier".

BEM is one of the most widely used CSS naming conventions, and it is often used in conjunction with JavaScript frameworks like React. BEM's approach of using descriptive class names that describe the purpose of each element can make it easier to work with CSS in a component-based architecture.

## Core components of BEM:

1. Blocks: Blocks are standalone components that can be reused throughout a website. They represent larger, meaningful sections of a page, such as a header, footer, or navigation menu.

2. Elements: Elements are parts of a block and cannot exist outside of their parent block. They represent smaller, functional parts of a block, such as a button or a list item.

3. Modifiers: Modifiers are used to modify the appearance or behavior of a block or element. They can be used to create variations of a block or element, such as a different color, size, or state.

## Syntax

BEM class names are typically structured using the following syntax:

```
.block {}
.block__element {}
.block--modifier {}
```
For example, if you had a block representing a navigation menu, you might use the following class names:

```
.nav {}
.nav__item {}
.nav__item--active {}
```

This structure helps to create clear and consistent class names that are easy to read and understand. BEM is often used in conjunction with JavaScript frameworks like React, as it can help to create more organized and maintainable components.
<br>
## Code Example

```html
<nav class="nav">
  <ul class="nav__list">
    <li class="nav__item">
      <a href="#" class="nav__link--active">Home</a>
    </li>
    <li class="nav__item">
      <a href="#" class="nav__link">About</a>
    </li>
    <li class="nav__item">
      <a href="#" class="nav__link nav__link">Services</a>
    </li>
    <li class="nav__item">
      <a href="#" class="nav__link">Contact</a>
    </li>
  </ul>
</nav>
```


```CSS
.nav {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: lightblue;
}

.nav__list {
  display: flex;
  justify-content: space-between;
  list-style: none;
  width: 80%;
  margin: 0;
  padding: 0;
}

.nav__item {
  margin: 0;
  padding: 0;
}

.nav__link {
  text-decoration: none;
  color: black;
  font-weight: bold;
}

.nav__link--active {
  color: green;
  font-weight: bold;
}

```



![Nav with CSS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l77nl35ccxtmz5nypavg.png)

---

###Further Reading:
[getbem.com/](https://getbem.com/)

---


❤️❤️❤️


Follow me on [LinkedIn](https://www.linkedin.com/in/davidchedrick/) for all the updates and future blog posts

###Other blogs in this series:

[**Closing a Knowledge Gap: Best Practices for Writing Git Commit Messages**](https://dev.to/davidchedrick/closing-a-knowledge-gap-best-practices-for-writing-git-commit-messages-5c8l)