Common mistakes when writing CSS with BEM

Software development is a team effort

When doing software development it is essential to agree on guidelines, technology, and methodologies. Those agreements should be the result of discussions, proof of concepts, knowledge, and sometimes vo…



Software development is a team effort

When doing software development it is essential to agree on guidelines, technology, and methodologies. Those agreements should be the result of discussions, proof of concepts, knowledge, and sometimes votes. The whole team should be invited to participate because software development is a team effort and everyone likes engaging team members.

At visuellverstehen, we are divided into multiple teams. Some teams agreed on using Block-Element-Modifier (BEM) and other teams agreed on using Tailwind CSS. I think it is super important to agree on one way or the other, while both ways are totally fine for the success of our client projects.

One thing is for sure. If you do not agree, it will become a mess. I have been there.



Learn proper BEM

Now and then new colleagues join our team. Some of them never have heard about BEM before. So they have to learn it and naturally some mistakes will happen. Mistakes are not a problem at all. They are part of the learning process.



Four most common mistakes

To help you learn proper BEM, I wrote down some of the most common mistakes I see in my day-to-day work life.



1. Wrongly nested blocks and elements

It is not allowed to nest blocks. If you start a new block, you are not allowed to proceed with elements from another block.



❌ Wrong

<article class="card">
    <header class="header">
        <h2 class="card__headline"></h2>
    </header>
</article>



✅ Correct

<article class="card">
    <header class="card__header">
        <h2 class="card__headline"></h2>
    </header>
</article>



2. Great-grandchildren

There are no great-grandchildren in BEM. Instead, »normal« elements of the block can be used.



❌ Wrong

<article class="card">
    <header class="card__header">
        <h2 class="card__header__headline"></h2>
    </header>
</article>



✅ Correct

<article class="card">
    <header class="card__header">
        <h2 class="card__headline"></h2>
    </header>
</article>



3. Modifiers without a base class

Modifiers cannot exist without a base block or element.



❌ Wrong

<article class="card--highlight">
    <header class="card__header"></header>
</article>



✅ Correct

<article class="card card--highlight">
    <header class="card__header"></header>
</article>



❌ Wrong

<article class="card">
    <header class="card__header--important"></header>
</article>



✅ Correct

<article class="card">
    <header class="card__header card__header--important"></header>
</article>



4. Too big blocks

It is not a good idea to create really big blocks. The idea of BEM is to create modular and reusable blocks.



❌ Wrong

<body class="body">
    <header class="body__header"></header>
    <main class="body__main"></main>
    <footer class="body__footer"></footer>
</body>



✅ Correct

<body class="body">
    <header class="header"></header>
    <main class="main"></main>
    <footer class="footer"></footer>
</body>



Automate stuff

Sometimes it is hard to find mistakes manually. Yesterday I learned there is a BEM linter. I will look into it.


Print Share Comment Cite Upload Translate
APA
Malte Riechmann | Sciencx (2024-03-29T01:01:33+00:00) » Common mistakes when writing CSS with BEM. Retrieved from https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/.
MLA
" » Common mistakes when writing CSS with BEM." Malte Riechmann | Sciencx - Friday October 22, 2021, https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/
HARVARD
Malte Riechmann | Sciencx Friday October 22, 2021 » Common mistakes when writing CSS with BEM., viewed 2024-03-29T01:01:33+00:00,<https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/>
VANCOUVER
Malte Riechmann | Sciencx - » Common mistakes when writing CSS with BEM. [Internet]. [Accessed 2024-03-29T01:01:33+00:00]. Available from: https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/
CHICAGO
" » Common mistakes when writing CSS with BEM." Malte Riechmann | Sciencx - Accessed 2024-03-29T01:01:33+00:00. https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/
IEEE
" » Common mistakes when writing CSS with BEM." Malte Riechmann | Sciencx [Online]. Available: https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/. [Accessed: 2024-03-29T01:01:33+00:00]
rf:citation
» Common mistakes when writing CSS with BEM | Malte Riechmann | Sciencx | https://www.scien.cx/2021/10/22/common-mistakes-when-writing-css-with-bem/ | 2024-03-29T01:01:33+00:00
https://github.com/addpipe/simple-recorderjs-demo