Code

BEM Methodology: What It Is and How It Helps Layout Designers and Developers

BEM Methodology: What It Is and How It Helps Layout Designers and Developers

Learn: Profession Front-end developer

Learn more

Christophor Bonifatievich emphasized the importance of the correct name in a famous cartoon. Apparently, Yandex understands this meaning as well. Since 2005, they began developing the BEM methodology, which emphasizes correct naming. In this article, we will consider what BEM is, its usefulness and relevance of use in modern web development. The BEM methodology helps to structure code and improves readability, which contributes to more efficient work on projects. In modern conditions, BEM continues to be a relevant tool for developers, allowing them to create scalable and maintainable interfaces.

What is BEM

The BEM (Block, Element, Modifier) ​​methodology is a set of rules for developing front-end projects. BEM simplifies the creation and maintenance of interfaces by providing a structured approach to code organization. The methodology's core components include blocks, elements, and modifiers, enabling the creation of flexible and reusable components. BEM improves readability and simplifies teamwork, making it a popular choice among web application developers.

A block is a completely self-contained component of a web page that is solely responsible for the visual presentation of elements. Importantly, blocks cannot have margins. They can contain other blocks, enabling the creation of complex structures and hierarchies. The block name corresponds to the class selector name, simplifying its identification and styling in CSS. Using blocks allows you to better organize your code and make it easier to work with page elements.

An element is a component of a block that has meaning solely in the context of its block and is not used separately. A selector name consists of the class name followed by a double underscore and the element name.

A modifier is an entity that characterizes the attributes of a block or element, such as position, state, and behavior. Hyphens are used to separate words in the names of all entities. In a selector, a modifier is indicated by a single underscore following the element or block name. Correct use of modifiers allows you to improve the semantics of your code and make it easier to maintain, which is especially important in the development of responsive and interactive web applications.

There are several popular approaches to naming. One of these is CamelCase, which capitalizes each word in a name. Another option is to use two hyphens to separate modifiers. These methods help improve the readability and structure of code, which is especially important in software development and web design. Correct naming promotes better perception of code and simplifies its maintenance.

Reworked text:

Be sure to check out the additional materials:

Programming notations: camel,

There are various notations in programming that help developers improve the readability and structure of their code. Some of the most popular are camel, snake, and shashlik notations.

Camel case notation is used to write identifiers, where each new word begins with a capital letter, for example, «myVariableName». This notation is often used in programming languages ​​such as Java and C# and improves the visual perception of long variable names.

Snake case notation uses underscores to separate words, for example, «my_variable_name». This notation is widely used in Python and is preferred for naming variables and functions because it improves readability.

Kebab-case notation also uses word separators but uses hyphens instead of underscores, for example, «my-variable-name». This notation is often found in URLs and CSS styles because it avoids conflicts with other characters.

The choice of notation depends on the programming language and the preferences of the development team. Proper use of notations contributes to the creation of clear and maintainable code, which is an important aspect of successful software development.

BEM (Block, Element, Modifier) ​​does not set strict rules for class naming, emphasizing clarity and uniformity. It is important that class names are intuitive and the approach to their creation remains unified across all company projects. You can choose your own naming style and stick to it, which provides flexibility and ease of working with code.

How it all began

Selectors have become the starting point for learning CSS. They play a key role in styling web pages, allowing developers to precisely define which HTML elements certain styles will be applied to. Knowing selectors and their types, such as class, ID, and universal, is the foundation for creating efficient and structured code. Understanding how selectors work allows you to optimize your site's performance and improve its visual perception, which in turn has a positive impact on SEO. The better organized your styles and their application, the higher your chances of achieving high rankings in search engines. Therefore, learning selectors is an important step towards creating high-quality web content.

Selectors in CSS Yandex developers initially rejected the use of ID selectors, then tag selectors. ID selectors were previously thought to provide faster execution speeds, but with the advancement of caching technologies in modern browsers, the performance of class selectors has improved significantly, making the difference in execution speed insignificant. This change in approach to selectors allows developers to more flexibly manage styles and simplifies the process of developing web applications.

The BEM (Block, Element, Modifier) ​​methodology uses exclusively class selectors. This allows for the efficient implementation of functionality similar to that provided by other selector types, while minimizing the risk of selector name clashes. This approach promotes the creation of more structured and understandable code, making projects easier to maintain and expand. BEM provides a high degree of modularity, making interface development more predictable and compatible with various style solutions.

A completely independent block is a key concept in modern web development. With the introduction of this term, developers have effectively adopted a component-based approach, which significantly simplifies the process of creating and managing projects. Independent blocks are convenient for teamwork, as they allow team members to focus on their tasks. They can be effectively combined into libraries, which ensures their reuse across different projects. This approach not only speeds up development but also helps maintain a consistent style and functionality, which is an important aspect for modern web applications. Using completely independent blocks increases the flexibility and scalability of projects, making them more adaptable to changes and market demands.

Modifiers are a convenient tool for avoiding the increase in the number of blocks when changing their attributes. This concept was developed by specialists at Yandex and involves the use of classes that act as "labels" that impart specific properties to a block. For example, the illustration shows a single block with three different modifiers: "red," "yellow," and "green." Thus, modifiers allow you to effectively manage the style and behavior of interface elements, significantly simplifying the process of developing and maintaining code.

Image: Skillbox Media

You can view the code, including CSS, on the Codepen platform.

If a modifier describes attributes that are assigned a specific value, it is recommended to use the "key-value" format for its naming. It is important to carefully choose both the key name and the value so that they accurately reflect the essence and functionality of the attribute. This will improve the readability of the code and increase its SEO optimization, since search engines take into account the structure and semantics of HTML content. Correct naming of modifiers promotes more effective perception of information by users and makes it easier to work with the code.

Modifiers such as traffic__light_color_red may seem convenient, but they have the disadvantage of fragility. Although the colors of the traffic light generally remain unchanged, other interface elements may change their attributes. This will require adjusting the class names, which complicates the maintenance of the code. Therefore, a more robust solution is to use a description of the state in the modifier name, rather than a specific attribute associated with this state. This approach provides greater flexibility and simplifies the process of making changes in the future.

  • trafic__light_action_stop;
  • trafic__light_action_wait;
  • trafic__light_action_go.

The file system plays a key role in organizing projects. Developers use various strategies for organizing files into directories. One approach is to classify them by technology or file type, such as CSS, HTML, JS, and XML. Another method is to separate files into common files, used across all projects, and service files, needed only for a specific project. Proper file system organization improves project structure, simplifies navigation, and facilitates code maintenance, which in turn positively impacts the efficiency of the development team.

As a result, it was decided to organize the file structure into blocks. Each block is represented by a separate directory that contains everything necessary for its implementation: functionality, design, templates, documentation, and tests. The directory name corresponds to the block name and is also used as the class name. Optional block elements are placed in separate subdirectories within the main block directory. This structure simplifies project management and makes the code more organized and flexible.

One approach to project organization using the BEM (Block, Element, Modifier) ​​methodology involves clearly structuring the code and file system. The basic idea of ​​BEM is to divide the interface into independent blocks that can be easily reused and modified. Each block is an independent component containing the elements that comprise it and modifiers that change the block's appearance or behavior.

For effective BEM implementation, it is important to adhere to a consistent class naming style, which ensures clarity and predictability of the code structure. For example, a block class name might look like «header», a block element would be called «header__logo», and a modifier would be «header__logo—small».

Furthermore, it is recommended to organize files into categories, creating separate folders for blocks, elements, and modifier styles. This approach makes it easy to maintain the project, add new components, and minimize code conflicts.

Implementing BEM in interface design helps create more structured, understandable, and scalable code, which is especially important for teamwork and long-term project support.

Directory structureImage: Skillbox Media

Tools. Yandex developed the i-bem.js framework and its own bundler for BEM projects, initially called bem-tools and later renamed ENB. The bundler plays a vital role in preparing project files for publication. It can process all JavaScript files, remove excess whitespace, and combine files into a single, ready-to-publish JS file, called a bundle. This process optimizes project performance and simplifies deployment.

The i-bem.js framework, which can also be considered a library, provides the core i-bem block. This block serves as the foundation for creating new blocks and writing JavaScript code using the concepts of blocks, elements, and modifiers. i-bem.js simplifies development by allowing you to organize and reuse code across projects, making it ideal for creating complex interfaces. The BEM tree is a tool that streamlines the process of writing HTML code. It defines the structure and order of blocks, significantly simplifying development. The BEMHTML template engine converts this BEM tree into clean HTML code. Visually, the BEM tree can be represented as a class hierarchy, providing clarity and simplifying the management of styles and web page components. Using a BEM tree improves the readability of code and simplifies its further maintenance and scalability.

Screenshot: Skillbox Media

In BEMJSON format The hierarchy is presented as follows:

Screenshot: Skillbox Media

Creating code using template engines significantly speeds up the process compared to manually writing HTML. Template engines automate routine tasks, allowing developers to focus on more important aspects of the project. This not only saves time but also reduces the likelihood of errors, which makes development more efficient and reliable.

Study additional materials:

To quickly layout, you need to master several key principles and tools. First and foremost, it's important to use modern frameworks like Bootstrap or Tailwind CSS, which can significantly speed up the development process. These frameworks offer pre-built components and grids, reducing the time spent coding from scratch.

It's also worth considering using CSS preprocessors like SASS or LESS. They simplify working with CSS by allowing you to use variables, nested selectors, and functions, making the code more structured and easier to understand.

Automating processes with tools like Gulp or Webpack allows you to optimize tasks like building files and automatically refreshing the page when changes are made. This significantly speeds up the workflow.

Don't forget about the importance of code organization. Well-structured HTML and CSS make the project easier to work with and understand. Using semantic markup improves accessibility and SEO.

Finally, regular practice and learning new technologies will help develop skills and increase the speed of your code. By following these recommendations, you can significantly speed up the website development process and improve its quality.

Benefits of Implementing BEM

BEM, or Block, Element, Modifier, was developed in 2005 and remains a relevant method in web development. This approach to creating interfaces ensures modularity and reusability of components, which facilitates the maintenance and scalability of projects. Thanks to a clear structure and understandable hierarchy, BEM allows developers to create clean and organized code, which significantly improves collaboration between teams and speeds up the development process. Given the constant growth and change of technology, the use of BEM remains an important aspect for creating modern and effective web applications.

Those who are interested in this topic and consider it relevant are already actively using these capabilities or at least aware of them. At the same time, those who are not interested in this area prefer to ignore this area.

A commentary on an article on Habr in 2016 was devoted to current issues in the field of technology and its impact on various areas of life. The commentary discussed key trends such as the development of artificial intelligence, process automation, and their impact on the labor market. Topics of data security and user privacy in the context of increasing digitalization were also raised. Importantly, such discussions contribute to a better understanding of the consequences of technological change and the need to adapt to them. Technologies continue to evolve rapidly, and the discussion of their impact on society remains relevant today.

Benefits of BEM

  • Clear project structure. Just by looking at the names of the selectors, you can understand how the project is structured and what each class is responsible for.
  • Simultaneous work on different components. Naming rules ensure selector uniqueness, and the likelihood of duplication in modifier names is low. Therefore, developers can work simultaneously without creating collisions.
  • Quick onboarding of new employees. Learning a project takes less time because BEM components document themselves.
  • High development speed. A clear structure facilitates refactoring, and the ability to reuse facilitates the creation of new projects.
  • Security when involving third-party specialists. The project is built from independent blocks, so each developer involved does not need to know the details of the entire project. Specialists can work within individual components. This allows for the involvement of third-party specialists in large projects.
  • Flexibility. You can use only part of the methodology, rather than the entire methodology. And even this can make working on a project more convenient.

BEM (Block, Element, Modifier) ​​has come a long way in its development and continues to evolve. Today, it is no longer a standalone solution, but successfully integrates with various tools, frameworks, and template engines. This interaction allows developers to create more flexible and scalable web applications, significantly simplifying the development and maintenance of code. BEM remains a relevant and in-demand approach in web development, facilitating effective collaboration across various technologies and improving the overall structure of projects.

Vladimir Grinenko, Head of Yandex Ecosystem Interfaces, commented on the latest changes and innovations in user interfaces on the platform. He noted the importance of user-friendliness and intuitiveness in interface development, which contributes to improved user interaction with the company's services. Grinenko emphasized that the team is constantly working on improvements to ensure a more efficient and enjoyable user experience using Yandex products. His commentary also outlined plans to implement new technologies aimed at optimizing the user experience.

BEM Cons

Not all developers have responded positively to the BEM methodology. Their dissatisfaction is often related to the need to master a new approach, as well as envy of those who previously developed such a simple and understandable way of organizing code. BEM offers a structured approach to development that can significantly simplify the process of creating and maintaining web applications.

It is necessary to ban the promotion of BEM and establish fines for public demonstrations of this ideology. Such measures will help limit the spread of BEM and protect society from its negative consequences. It is important to create a legal framework to combat this trend to prevent the further penetration of BEM into the public consciousness.

Comments on the Habr platform provide an opportunity for users to share opinions, ask questions, and discuss topics related to technology, programming, and internet culture. Community members can leave comments under articles, facilitating the exchange of experience and knowledge. This is an important aspect of interaction on the site, allowing users not only to express their point of view but also to receive feedback from other readers and authors of materials. Active participation in discussions on Habr can help users expand their horizons and better understand modern trends in the IT field.

Constructive comments on the shortcomings of the BEM methodology do exist. Let's consider the main points of criticism. First, many developers note the difficulty of understanding and learning BEM, especially for beginners. Second, the presence of a large number of classes can lead to code redundancy, making it difficult to maintain. Critics also point out that strict naming rules can limit developer creativity. Additionally, BEM is sometimes perceived as an overly cumbersome methodology for smaller projects, where simpler approaches may be more effective. Despite these shortcomings, BEM remains a popular development tool because it provides a clear structure and simplifies code organization.

Using blocks in situations where they are not necessary can lead to code redundancy. In small projects, code for small components, such as a logo or a single button, designed according to BEM principles often appears cumbersome and complicates the structure. This can negatively impact performance and code readability. Optimizing the component structure, especially in small projects, can simplify development and improve code efficiency.

Long element and modifier names can significantly complicate the process of creating selectors in the BEM methodology. For developers unfamiliar with this system, understanding such names can be difficult. Therefore, it is important to carefully consider names to ensure that the advantages of BEM do not turn into disadvantages. Proper use of short, clear names improves code readability and simplifies its maintenance, which ultimately leads to more efficient development and reduces the likelihood of errors.

The main and significant drawback of the BEM methodology is that it violates the principle of cascading, turning CSS into a static style (SS). This limits the flexibility and reusability of styles, which can complicate the development and maintenance of large projects.

BEM does not eliminate the shortcomings of cascading inherent to CSS, but seeks to minimize their impact. The BEM system helps organize code, improving readability and maintainability, but does not solve the problems associated with the cascading style.

Comments on the Habr platform are an opportunity for users to share their opinions, ask questions, and discuss articles posted on the site. Habr is a popular resource for IT, technology, and business professionals, where everyone can contribute to the discussion of relevant topics. Comments facilitate the exchange of knowledge and experience, creating a valuable community. Participating in discussions on Habr not only helps you improve your understanding of the topics being discussed but also helps you establish connections with like-minded people. Posting comments can also increase your profile's visibility and draw attention to your own projects or ideas. Knowledge of the BEM methodology is essential for successful project management, especially when it comes to understanding the team's selector naming scheme. Mastering new technologies takes time and effort, and BEM naming may seem daunting at first. However, this methodology ensures clarity and order in your code, which significantly simplifies development later on. Taking the time to learn BEM will help you navigate the project more quickly and make changes more effectively, which will undoubtedly save you time in the future.

At first, I was unsatisfied with the syntax, particularly the double underscores and other quirks. However, I ultimately accepted this decision because it proved to be extremely convenient. The syntax options are not very diverse, and I don't prefer camelCase in CSS, so I chose the BEM methodology, based on Harry Roberts' approach, which is used by companies like Google and Salesforce. The benefits of this approach become apparent over long periods of time.

Comments on the Habr platform are an important part of user interaction. They allow you not only to share opinions but also to exchange experiences, ask questions, and receive answers from experts and other community members. Comments on Habr can strengthen your understanding of a topic, deepen the discussion, and suggest new ideas. Furthermore, active participation in comments can increase your profile visibility and increase your follower count. It's important to formulate your thoughts clearly and to the point so that your commentary is useful to other readers and contributes to constructive dialogue.

The vagueness of the definition "BEM-compliant" is one aspect worth considering. While the flexibility of the BEM methodology allows it to be adapted to various projects, this also leads to difficulties in explaining the specific rules and principles you apply. As a result, it can be difficult for external developers or new team members to quickly understand how exactly BEM is used in your project. Clearly documenting and standardizing approaches to BEM can help eliminate this ambiguity and improve teamwork.

Presentation slide "BEM in the Wild" Screenshot: wsd.events

Prospects

Despite criticism and certain shortcomings, the BEM (Block, Element, Modifier) ​​methodology is widely used by developers worldwide. This approach to structuring code allows for the creation of scalable and easily maintainable projects, making it a popular choice in web development. BEM helps improve code organization and increase its readability, which is especially important in large teams and long-term projects.

BEM has undoubtedly become popular not only in Russia but also abroad, which is a rare achievement for web development methodologies. This technology provides a structured approach to creating interfaces, allowing developers to create scalable and maintainable projects. Implementing BEM into development practices improves code quality and simplifies collaboration between teams. Thus, it can be argued that BEM deservedly occupies an important place in modern web design and development.

Comments on Habr offer an opportunity to express your opinion, discuss current topics, and share experiences with other users. The platform actively supports interaction among specialists and enthusiasts in various fields, making comments an important part of the discussions. Writing comments on Habr not only allows you to express your thoughts but also receive feedback, broaden your horizons, and learn new perspectives on issues of interest. Participating in discussions promotes the development of professional skills and the exchange of knowledge.

Gary Roberts is a fan of the BEM methodology and uses his own extended syntax for naming classes. Google developers share their understanding of BEM on their GitHub, explaining the principles and approaches to this methodology. It's important to note that BEM (Block, Element, Modifier) ​​provides a structured and convenient way to write code, which improves CSS workflow and simplifies the development process.

The popularity of the BEM methodology and its application in MDL require a clear separation between class naming and tools like bem-tools. BEM class naming is indeed effective and widely used, although we employ minor customizations at Google. However, tools like bem-tools have their drawbacks, including slowness, making them impractical for use at Google.

Comments on the Habr platform are an important element of user interaction. On this resource, commenting allows users to exchange opinions, ask questions, and share experiences on various topics related to IT, technology, and startups. Participating in discussions helps broaden horizons, deepen knowledge, and establish connections with other specialists. Furthermore, active users have the opportunity to improve their reputation on the site, which can lead to new career opportunities. Comments on Habr foster a community where everyone can contribute and gain valuable insights. Knowledge of the BEM (Block, Element, Modifier) ​​methodology is becoming increasingly important for both web designers and developers. The HeadHunter platform lists over 150 job postings that mention BEM in their candidate requirements. This underscores the importance of this methodology in modern web development and layout, as well as its role in creating flexible and maintainable interfaces. Mastering BEM can significantly improve your chances of finding a job in the development field.

Example vacancy from HeadHunter Screenshot: hh.ru
Example vacancy from HeadHunterScreenshot: hh.ru

BEM libraries containing universal components (blocks) are available for public use. Integrating them doesn't require specialized tools or templating engines. You can easily include library files in your page's HTML code using standard link and script tags. This allows you to quickly and efficiently implement ready-made solutions in your projects, simplifying the development process and ensuring a high degree of modularity and code reusability.

Conclusion

Today, many developers don't strive to strictly adhere to the BEM methodology. Two main components of this methodology can be identified: development tools and the approach to class naming. The second aspect, time-tested, is actively used not only in Russia but also abroad. The principles embodied in BEM are definitely worth attention and study, as they help create clear and structured code, which in turn improves the readability and support of projects. Implementing these principles can significantly simplify the development process and improve the quality of the final product.

Read also:

  • Quick layout: what tools to use and what rules to follow
  • “I was surprised when I wrote the code, and it worked”: how a beginner Python developer lives
  • The Go language: what's under the hood and why a programmer should learn it as a second language

Frontend Developer Profession

Thanks to frontend developers, we leave likes and comments, add products to carts, and quickly understand online maps. On this course, you will learn to create web service interfaces using programming languages ​​and additional technologies. You will be able to develop task schedulers, instant messengers, and online stores.

Find out more