BEM and SASS? What Are They And How To Use Them (2024)

About the author

@catalinmpit is a software engineer, AWS community builder and technicalwriter based out of London. He’s currently an engineer at TypingDNA, working on applying keystroke dynamics as ameans ofbiometrics authentication.

Check out more of his work on catalins.tech

BEM and SASS? What Are They And How To Use Them (1)

Should you use BEM + SASS? It depends.

“It depends?" Well, yes! In many cases, it comes down to personal preference. It’s up to you to decide whether youchoose to use this combination in your projects or not. Throughout this article, we are going to investigate whatthey are, how they work, and the benefits of using them.

First of all, let’s dive into what BEM & SASS are.

BEM (Block Element Modifier) is a commonly used naming convention for writing classes in HTML.It helps you speed up the development process while keeping things organized and easy to understand for the whole team.

It is based on a simple idea where every big chunk of markup is called a Block. Everything inside a block iscalled an Element. And the Modifiers are variants of a specific block or element.

Let’s take a look over the following example for a better understanding:

<section class="bar"><img class="bar__logo" src="#"/><ul class="bar__list"><li class="bar__item"><a href="#" class="link link--highlight">Item 1</a></li><li class="bar__item"><a href="#" class="link">Item 2</a></li><li class="bar__item"><a href="#" class="link">Item 3</a></li></ul></section>

Here we have a block called "bar". It contains an image “bar__logo” and a list “bar__list” of links“bar__item” and “link” (which is another small block used throughout the project). For one of the links, we alsohave a modifier, "--highlight", which points out that our link will look different from the others. That’s it!

The same approach can be applied to any other type of block. For more details regarding BEM, check out thedocumentation.

SASS (Syntactically Awesome Style Sheets) on the other hand, is a CSS preprocessor that makes writing style syntaxmuch faster and easier to read.

“Prepro-what?" Think of CSS Preprocessors as programs that help you generate CSS using a unique syntax. Thingslike operators, nesting, mixins, inheritance (which are available in SASS) that are easier to write and maintain.

For examples in this article, we’ll use SCSS which is a variation of SASS, and more closely resembles CSS syntax. Youcan find the basics as well as the syntax difference between SASS & SCSS here.Here’s a simple example:

//variables$color-red: #FF0000;$color-gray: #CCCCCC;$color-black: #111111;$spacing: 5px;//nesting.bar {border: 1px solid $color-gray; // variable use&__list {padding: 0 3 * $spacing; //operation including variable}&__item {list-style: none;background-color: $color-black;margin-bottom: $spacing;padding: 2 * $spacing;}&__logo {display: none;}}.link {text-decoration: none; color: $color-red; &--highlight { font-weight: bold; }}

It’s pretty straight forward. Notice how we’re already combining it with BEM.

Now that we’ve covered BEM & SASS, let’s learn more by studying a common mistake in a real-world example. One might betempted to write:

<section class="profile-card"><p class="profile-card__name">Name<p><div class="profile-card__actions"><button class="profile-card__actions__save"><span class="profile-card__actions__save__icon" /><p class="profile__card__actions__save__text">Save</p></button><button class="profile-card__actions__delete"><span class="profile-card__actions__delete__icon" /><p class="profile__card__actions__delete__text">Delete</p></button></div><div class="profile-card__info">Lorem ipsum<button class="profile-card__info__button">Request info</button></div></section>

The SCSS will perfectly nest:

.profile-card {&__name {font-size: 14px;}&__actions {padding: 10px;&__save,&__delete {&__icon,&__text {color: white;@debug &;}&__text {font-size: 12px;}}&__save { background: black;}&__delete {background: red;}}&__info {&__button {color: white;background: black;}}}

However, there’s an issue with this approach; it’s not following the BEM naming methodology. Writing such nestedclasses for the elements within blocks should be avoided. Why? Because it will result in some long class names, such as:

.profile__card__actions__delete__text

You may think: “Then I’ll skip the class and target tags directly”. That’s doesn’t adhere to the BEM methodologyeither. The preferred way to go about this is:

<section class="profile-card"><p class="profile-card__name">Name</p><div class="profile-card__actions"><button class="btn btn--black"><span class="btn-icon btn-icon--check"/><p class="btn-text">Save</p></button><button class="btn btn--red"><span class="btn-icon btn-icon--trash"/><p class="btn-text">Delete</p></button></div><div class="profile-card__info">Lorem ipsum<button class="btn btn--black">Request info</button></div></section>

Where your styling will look like this:

.profile-card {&__name {font-size: 14px;}&__actions {padding: 10px;}}.btn {&--red,&--black {color: white;}&--red {background: red;}&--black {background: black;}&-icon,&-text {color: white;}&-text {font-size: 12px;}}

It looks a lot better, right? By doing this, you’ll avoid long class names. Having small blocks also makes it easy toreuse, so you’ll save time when making updates later. Also, it makes it easier to maintain the look of basicelements which are shown across all of your project (e.g. buttons, links).

There are times when refactoring the markup is not an option (e.g. large project with altered BEM syntax), so let’sreturn to the original example. If you’re in a situation like this and you find it hard to target given elements fromSCSS, try debugging.

“You can debug SASS?" That’s right. You can debug and output your compiled nested styling using @debug.

...&__save,&__delete {&__icon,&__text {color: white;@debug &;}}...

The output will be:

.profile-card__actions__save__icon, .profile-card__actions__save__text, .profile-card__actions__delete__icon, .profile-card__actions__delete__text

Debugging makes it easy to spot syntax mistakes and fix them.

As mentioned, this way of working may or may not be the right fit for you. Feel free to give it a go and see if it is.You can even adapt it to your style, but make sure you remain consistent with the methodology you use/modify.

Happy coding.

Catalin regularly posts helpful development tips and guides on Twitter. Be sure to follow him at @catalinmpit

Share this post

About PullRequest

HackerOne PullRequest is a platform for code review, built for teams of allsizes. We have a network of expert engineers enhanced by AI,to help you ship secure code, faster.

Learn more about PullRequest

BEM and SASS? What Are They And How To Use Them (2024)

FAQs

What is SASS and BEM? ›

SASS nesting and the BEM (Block - Element - Modifier) structure will help get us to get rid of this concern. This is a method that consists of naming your CSS classes based on the role they play in your HTML. The Block Element Modifier structure is a powerful structure to follow when naming our classes.

What is SASS and how do you use it? ›

Short for Syntactically Awesome Style Sheets, SASS is a popular CSS pre-processor. SASS code is processed by the program and compiled into CSS code, which can be used to style HTML elements. SASS controls how it appears on the web page. It is compatible with all CSS versions.

What is BEM used for? ›

BEM stands for Block Element Modifier. The main idea behind it is to speed up the development process, and ease the teamwork of developers by arranging CSS classes into independent modules. If you ever saw a class name like header__form--search , that is BEM in action.

What does BEM do? ›

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.

Should I use BEM with SASS? ›

You've created proper BEM selectors utilizing the clean structure of Sass, which will help you keep your code organized and simple to update. Plus, you've saved yourself from typing the block name over, and over, and over again.

When should I use SASS? ›

Why Use Sass? Stylesheets are getting larger, more complex, and harder to maintain. This is where a CSS pre-processor can help. Sass lets you use features that do not exist in CSS, like variables, nested rules, mixins, imports, inheritance, built-in functions, and other stuff.

Why is Sass needed? ›

Functions: SASS provides a range of built-in functions that can be used to perform calculations and manipulate values in your stylesheets. Improved code organization: SASS provides features such as partials and imports, which allow you to organize your code into smaller, more manageable files.

Why use Sass vs CSS? ›

Sass offers web developers a powerful set of tools that significantly improve their productivity and code maintainability compared to traditional CSS. With features like modularity, nesting, extending, mixins, and functions, Sass empowers developers to write clean, reusable, and efficient stylesheets.

How do I start using Sass? ›

Setting Up Sass: A complete guide for beginners
  1. Install Node if you haven't already. You'll need Node or npm (node package manager) to install Sass. ...
  2. Use npm to globally install Sass. ...
  3. Create your styling files. ...
  4. Make your CSS file watch your Sass file. ...
  5. Install a code highlighter. ...
  6. Enjoy the power of Sass!
May 26, 2020

Why is BEM important? ›

BEM primary role is to facilitate the registration of Engineers, Engineering Technologists, Inspectors of Works, Sole Proprietorships, Partnerships and Bodies Corporate providing professional engineering services and; to regulate the professional conduct and practice of registered person in order to safeguard the ...

How do you implement BEM? ›

Steps to implement BEM:
  1. Start by defining the block: A block should be represented by a single CSS class and should be prefixed with the block name, such as “header”, “button”, or “form”.
  2. Identify the elements within the block: The elements are the parts of the block that make it up.
Feb 8, 2023

What are the disadvantages of BEM? ›

Disadvantages of BEM:
  • Verbosity: The BEM naming convention can result in longer class names that can make the HTML code more verbose and difficult to read.
  • Overhead: Implementing BEM requires a significant amount of upfront work and planning, which can be a burden for small projects or teams with limited resources.
Feb 2, 2023

What are the rules of BEM? ›

Two Dashes style
  • Names are written in lowercase Latin letters.
  • Words within the names of BEM entities are separated by a hyphen ( - ).
  • The element name is separated from the block name by a double underscore ( __ ).
  • Boolean modifiers are separated from the name of the block or element by a double hyphen ( -- ).

Is BEM CSS still used? ›

We have all heard about it, it is commonly used when writing custom CSS, SCSS, SASS. The naming convention is referred to as BEM and it stands for. Short for Blocks, Elements and Modifiers.

What does the BEM stand for? ›

British Empire Medal (BEM)

What does SASS do? ›

SASS (Syntactically Awesome Stylesheets) is a CSS pre-processor that lets you use variables, mathematical operations, mixins, loops, functions, imports, and other interesting functionalities that make writing CSS much more powerful.

What does SASS command do? ›

Sass is a stylesheet language that's compiled to CSS. It allows you to use variables, nested rules, mixins, functions, and more, all with a fully CSS-compatible syntax. Sass helps keep large stylesheets well-organized and makes it easy to share design within and across projects.

What does SASS mean coding? ›

Sass (short for syntactically awesome style sheets) is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). SassScript is the scripting language itself.

What does BEM stand for CSS? ›

The term BEM stands for Block-Element-Modifier, which is a naming convention for CSS class names that helps to maintain a clear and consistent structure in a project's styling.

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6132

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.