en	Using BEM to solve common issues in web development

en	The BEM methodology defines CSS selector naming conventions../naming-convention/naming-convention.en.md that solve a range of web development problems and address the following issues:

en	How to simplify code and facilitate refactoring#how-to-simplify-code-and-facilitate-refactoring

en	How to get self-documenting code#how-to-get-self-documenting-code

en	How to start reusing code without letting components influence each other#how-to-start-reusing-code-without-letting-components-influence-each-other

en	How to host multiple entities on the same DOM node and avoid copy and paste#how-to-host-multiple-entities-on-the-same-dom-node-and-avoid-copy-and-paste

en	How to simplify code and facilitate refactoring

en	Problem

en	During project layout, interface components are given names based on the context the developer is working with.

en	The context is usually a page or a part of one.

en	When just one developer is working with the layout for a brief period, name collisions are easily avoided.

en	But if multiple people are working on a project, or edits need to be made at some later point, it is difficult to keep track of component name dependencies.

en	In large projects, modifying a single class may lead to dozens of affected pages.

en	For example, the following class names can be used for creating a navigation menu:

en	<ul class="nav"> <li class="item active"><a class="link">One</a></li> <li class="item"><a class="link">Two</a></li> <li class="item"><a class="link">Three</a></li> </ul>

en	They might have the following CSS rules:

en	If we need to add another component containing menu items to the page, the styles for the new item will affect the items from the existing navigation menu.

en	Or let's say we need to change the rules for the .active class in the navigation menu.

en	We can't tell from the name which components use it.

en	For example, there could be a <div class="button active">Click me!</div> button on another page.

en	In that case, changing the rules for .active would affect the style of that button.

en	In order to find out whether styles can be painlessly changed for the .active class, the developer will have to examine the structure of the entire page or project.

en	Any change consumes a significant amount of time just searching for dependent components.

en	Solution

en	The BEM methodology solves the collision issue using naming conventions for CSS classes../naming-convention/naming-convention.en.md#css-selector-naming-convention, providing unique names for all components and their parts.

en	Using naming conventions allows us to:

en	Define unique names for BEM entities../key-concepts/key-concepts.en.md#bem-entity.

en	Track hierarchical relationships within a block.

en	Simplify the code.

en	Get self-documenting code#how-to-get-self-documenting-code.

en	Let's take the same navigation menu example:

en	<ul class="nav"> <li class="item active"><a class="link">One</a></li> <li class="item"><a class="link">Two</a></li> <li class="item"><a class="link">Three</a></li> </ul>

en	But we'll apply BEM naming conventions to it.

en	The nav class will designate the block name, nav__item and nav__link will designate the element names, and nav__item_active will indicate the name of a modifier for the item element.

en	In this case, the entry will look like this:

en	<ul class="nav"> <li class="nav__item nav__item_active"><a class="nav__link">One</a></li> <li class="nav__item"><a class="nav__link">Two</a></li> <li class="nav__item"><a class="nav__link">Three</a></li> </ul>

en	And the CSS will look like this:

en	The new CSS class names contain all the information about the block structure.

en	This means that we no longer need to look at the page HTML code to determine all the dependencies.

en	The selector always contains knowledge of which blocks or elements are affected by its rules (in this case, the nav__item element).

en	The developer doesn't need to think about the possibility of a <div class="button active">Click me!</div> button existing somewhere, because its CSS rules will be written as .button_active and won't depend on the rules for the active modifier for the menu item (nav__item_active).

en	Using long names has the following disadvantages:

en	The resulting code base is heavier.

en	This problem is solved by using gzip, which compresses repetitive sequences in names.

en	More time is spent on writing classes.

en	This burden is eased by using autocomplete in the editor, along with CSS preprocessors and template engines that automatically add prefixes.

en	Long class names provide explicit connections between parts of components, which saves time on analyzing the project architecture.

en	How to get self-documenting code

en	Problem

en	When switching to a new project, a developer invests considerable effort learning how everything is organized, where things are, and how the code works.

en	In some cases, more time is spent on this investigation than on implementing functionality or fixing bugs.

en	Solution

en	One of the goals of BEM is to make it clear what a piece of code does just by the class names.

en	The idea of self-documenting code is that looking at CSS classes, variables, and functions provides enough information on how the code works and how the interface components interact.

en	Using BEM, you can get HTML with class names that show the interaction of the following parts of code:

en	Independent blocks../key-concepts/key-concepts.en.md#block.

en	Elements../key-concepts/key-concepts.en.md#element (child components) of a block.

en	Modifiers../key-concepts/key-concepts.en.md#modifier of a block or an element.

en	Here is an example with a search form on a website.

en	We won't look at the HTML.

en	We'll try to just read the CSS and understand which part of the interface it describes.

en	Implementation of the form in a classical layout:

en	This doesn't reflect the relationships between:

en	Components and their parts.

en	Selectors and specific components of the interface that they are related to.

en	Using global selectors makes the project code non-extensible, as even the smallest changes will entail edits to all the dependent rules.

en	Let's write the CSS for classes:

en	The code is more informative.

en	Now it is clear that there is a form, a field, and a submit component.

en	But these names still don't tell us whether field belongs to form, or what will happen if there are multiple fields or forms on the page.

en	Once again we must return to the HTML.

en	We'll rewrite the example using BEM naming conventions../naming-convention/naming-convention.en.md:

en	Now we can see how this code works.

en	The CSS class names show that:

en	There is a form implemented by the form block.

en	The form_search modifier indicates that this is a search form.

en	The form has nested elements: form__field and form__submit-button.

en	Following the BEM naming convention allows us to understand the structure of a block without studying the HTML in detail.

en	Even if another field appears on the page (besides form__field), its rules won't affect the search form elements in any way.

en	The new field will be implemented as an element of another block, and will have its own unique name.

en	For example, attach__field.

en	The BEM naming rules help to make the project code unambiguous and informative.

en	This reduces the learning curve for other developers.

en	How to start reusing code without letting components influence each other

en	Problem

en	The developer uses a similar set of components when developing pages in the same project.

en	For example, a page may have several types of menu blocks.

en	We'll use the navigation menu example to examine this problem:

en	<ul class="nav"> <li class="item"><a class="link">One</a></li> <li class="item"><a class="link">Two</a></li> <li class="item"><a class="link">Three</a></li> </ul>

en	The CSS styles and item can be defined like this:

en	If additional components containing items will be added to the page, another code block will appear with the item class, for example:

en	<div class="snippets"> <div class="item"> <h2 class="title"></h2> <img class="thumb"> </div> </div>

en	In this case, the CSS can be formatted with cascades.

en	To do this, you only need to extend the rules already written for .item:

en	Code like this will work until the page has to be changed.

en	For example, until you have to move the menu items, use the code somewhere else separate from the parent component, or nest a navigation menu in the snippets block.

en	Using cascades increases coupling of interface components of the interface.

en	You can't fix one component without affecting the style of another one.

en	Solution

en	CSS selector naming rules../naming-convention/naming-convention.en.md make it possible to change specific points without affecting dependent components.

en	In BEM, every block has a unique name and is self-sufficient.

en	Let's write the same code in conformance with BEM naming rules:

en	<ul class="nav"> <li class="nav__item"><a class="nav__link">One</a></li> <li class="nav__item"><a class="nav__link">Two</a></li> <li class="nav__item"><a class="nav__link">Three</a></li> </ul>

en	In this case, adding a new item to the page will look like this:

en	<div class="snippets"> <div class="snippets__item"> <h2 class="snippets__title"></h2> <img class="snippets__thumb"> </div> </div>

en	The snippets__item item will have unique CSS rules that only apply to itself:

en	Changes to nav__item do not affect snippets__item, since the items get different unique names due to the namespacehttps://en.wikipedia.org/wiki/Namespace#In_programming_languages defined by the block name.

en	This allows forming independent CSS rules for all elements of a block.

en	This approach makes it possible to protect elements from mutually affecting each other — elements are always part of a block.

en	The same principle is used by Shadow DOM in Web Components.

en	However, in contrast to Shadow DOM, BEM naming conventions are applied without regard for browser compatibility.

en	The snippets and nav blocks can be reused and moved within the page or project.

en	The uniqueness of class names based on BEM naming rules allows blocks to be independent of each other.

en	Using cascades in BEM

en	The BEM methodology allows using cascades.

en	For example, a cascade is appropriate for changing elements depending on the state of the block or the theme assigned to it:

en	Important! Applying a cascade increases code coupling and makes reuse impossible.

en	How to host multiple entities on the same DOM node and avoid copy and paste

en	Problem

en	When working with projects, an implemented functionality may need to be reused.

en	In many cases, this issue is resolved by copying the desired chunk of code to the new component.

en	This approach has the following disadvantages:

en	The project's code base increases.

en	Debugging becomes more difficult if errors are detected.

en	The consequence is that the developer has to support more lines of code, and fixes must be made separately in each implementation.

en	This increases the time spent on debugging and maintaining the project.

en	Solution

en	We'll use an example that implements a universal navigation menu block and follows all the BEM naming rules../naming-convention/naming-convention.en.md.

en	<ul class="nav"> <li class="nav__item"><a class="nav__link">One</a></li> <li class="nav__item"><a class="nav__link">Two</a></li> <li class="nav__item"><a class="nav__link">Three</a></li> </ul>

en	This block could be used, for example, for navigating articles in a news section.

en	Let's assume that the news section already has the articles block with all the necessary CSS rules.

en	We can use a mix../key-concepts/key-concepts.en.md#mix to combine the implementation of the two different blocks without copying code.

en	In other words, host the nav block and the articles__nav element on the same DOM node.

en	The code will look like this:

en	<ul class="nav articles__nav"> <li class="nav__item"><a class="nav__link">One</a></li> <li class="nav__item"><a class="nav__link">Two</a></li> <li class="nav__item"><a class="nav__link">Three</a></li> </ul>

en	This implementation allows combining the functionality of the nav block with the specific implementation of the articles__nav element (the appearance of news articles in the menu).

en	However, there is no need to copy the existing CSS rules.

en	If a bug is detected, only one part of the code will have to be fixed.

en	In addition to blocks and elements, you can mix other BEM entities, as well.

en	For more information about ways to use mixes in BEM, see the section Basic BEM concepts../key-concepts/key-concepts.en.md#mix.