DEV Community

Awais Abbas
Awais Abbas

Posted on

Choosing the Right CSS Naming Convention for Your Project

When it comes to naming CSS classes, there are several different conventions that developers use. In this post, I will discuss some of the conventions I use and others that are commonly used in the CSS community.

1- BEM (Block Element Modifier): BEM is a naming convention that helps developers write structured and organized CSS code. It divides UI elements into separate blocks, elements, and modifiers to create clear and maintainable class names. BEM is a popular convention that many developers prefer for its clear and structured naming scheme.

<div class="block">
  <div class="block__element">
    <div class="block__element--modifier"></div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
.block { /* block styles */ }
.block__element { /* element styles */ }
.block__element--modifier { /* modifier styles */ }
Enter fullscreen mode Exit fullscreen mode

In this example, the "block" class represents a standalone component, while the "block_element" class represents a part of that component. The "block_element--modifier" class is used to modify the appearance or behavior of the element.

2- SMACSS (Scalable and Modular Architecture for CSS): SMACSS is another naming convention that helps create scalable and maintainable CSS code. It categorizes CSS into five categories: base, layout, module, state, and theme. Each category has its own naming conventions, which can help to create clear and organized CSS code.

<div class="layout">
  <div class="module">
    <div class="state"></div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
.layout { /* layout styles */ }
.module { /* module styles */ }
.state { /* state styles */ }
Enter fullscreen mode Exit fullscreen mode

Here, the "layout" class represents the overall layout of the page, while the "module" class represents a specific UI component. The "state" class is used to represent different states that the component can be in, such as "active" or "disabled".

3- Atomic CSS: Atomic CSS is a naming convention that breaks CSS styles down into smaller, atomic classes that can be combined to create more complex styles. Each class represents a single property or attribute, such as "text-center" for center-aligning text or "bg-blue" for setting a blue background color. Atomic CSS can make it easier to create consistent and reusable styles, but it can also lead to a large number of classes in your HTML code.

<div class="text-center bg-blue p-2">
  <p class="text-white">Hello World!</p>
</div>
Enter fullscreen mode Exit fullscreen mode
.text-center { text-align: center; }
.bg-blue { background-color: blue; }
.p-2 { padding: 2px; }
.text-white { color: white; }
Enter fullscreen mode Exit fullscreen mode

In this one, each class represents a single property or attribute that can be combined to create more complex styles. The "text-center" class is used to center-align text, while the "bg-blue" class is used to set a blue background color. The "p-2" class sets a padding of 2 pixels, and the "text-white" class sets the text color to white.

4- OOCSS (Object-Oriented CSS): OOCSS is a naming convention that aims to create reusable, object-oriented CSS code. It's based on the idea of separating UI elements into separate objects, which can then be reused across different parts of the website. OOCSS encourages the use of abstract, reusable classes and avoids styling specific elements directly.

<div class="button">
  <span class="button__label">Click me</span>
</div>
Enter fullscreen mode Exit fullscreen mode
.button { /* button styles */ }
.button__label { /* label styles */ }
Enter fullscreen mode Exit fullscreen mode

Here, the "button" class represents a reusable UI component that can be used across different parts of the website. The "button__label" class represents a specific part of the component and can be reused across different instances of the component.

5- Utility Classes: Utility classes are classes that are designed to perform a specific task, such as setting the font size or changing the color of text. They are often used in conjunction with other naming conventions, such as BEM or Atomic CSS, to create reusable and consistent styles.

<div class="bg-blue text-white p-2">
  <p class="text-center">Hello World!</p>
</div>
Enter fullscreen mode Exit fullscreen mode
.bg-blue { background-color: blue; }
.text-white { color: white; }
.p-2 { padding: 2px; }
.text-center { text-align: center; }
Enter fullscreen mode Exit fullscreen mode

In this example, each class represents a specific utility, such as setting a blue background color or center-aligning text. These classes can be combined to create more complex styles, and can be reused across different parts of the website.

While each convention has its own advantages and disadvantages, the most important thing is to choose a convention that works best for you and your team's needs.

Top comments (1)

Collapse
 
fruntend profile image
fruntend

Сongratulations 🥳! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up 👍