What is BEM (Block, Element, Modifier) methodology?
Jun 24, 2025 am 12:40 AMBEM is a naming specification for writing maintainable HTML and CSS. 1. Block is an independent and reusable component, such as .menu or .button, which should be named semantically; 2. Element is an integral part of Block, named such as .menu__item, which should not exist alone and should not be nested too deeply; 3. Modifier represents state or variant, such as .button--primary, which should only define different styles and avoid complex combinations; BEM improves naming clarity, reduces conflicts, facilitates maintenance and unify team structure.
BEM (Block, Element, Modifier) ??is a naming specification for writing maintainable, well-structured HTML and CSS. It is especially suitable for large projects or teamwork, and can help developers understand the relationship between components more clearly.
What is Block?
Block is a separate, reusable section, such as a button, navigation bar, or search box. It can exist alone without relying on other components.
- Naming method:
block-name
- Example:
.menu
,.button
Important: Block should be semantic, describing what it is rather than how it looks. For example, .search-form
is more suitable than .blue-box
.
What is Element?
Element is part of the Block and cannot exist alone from the Block. For example, a menu item is part of the menu, and the icons in the button are also elements of the button.
- Naming method:
block-name__element-name
- Example:
.menu__item
,.button__icon
Common misunderstandings:
- Don't nest too many levels, such as
.block__elem1__elem2
is not recommended. - Element belongs to only one block, do not use across blocks.
The function of Modifier
Modifier is used to represent the state or variant of Block or Element, such as "disable", "large", "red" and other style changes.
- Naming method:
block-name--modifier
orblock-name__element-name--modifier
- Example:
.button--primary
,.menu__item--active
Recommended usage:
- Modifier should only define styles that are different from the default state, avoiding duplicate code.
- Multiple Modifiers can be combined, but pay attention to naming conflicts and complexity controls.
Why is BEM useful?
In projects that are collaborative or long-term maintenance, BEM offers the following benefits:
- Clear naming: You can tell which module this class belongs to by looking at the name.
- Reduce style conflicts: class names are more unique and are not prone to global pollution.
- Easy to maintain: it is easier to locate the scope of influence when modifying styles.
- Consistent structure: Team members can write in a unified way to reduce communication costs.
Although the class name of BEM looks a bit long, the readability and stability it brings are far more important than short.
Basically that's it. It may feel a bit long-winded at first, but once you get used to it, you will find that it makes front-end development more orderly and more controllable.
The above is the detailed content of What is BEM (Block, Element, Modifier) methodology?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

A must-have for front-end developers: master these optimization modes and make your website fly! With the rapid development of the Internet, websites have become one of the important channels for corporate promotion and communication. A well-performing, fast-loading website not only improves user experience, but also attracts more visitors. As a front-end developer, it is essential to master some optimization patterns. This article will introduce some commonly used front-end optimization techniques to help developers better optimize their websites. Compressed files In website development, commonly used file types include HTML, CSS and J

Django is a web application framework built in Python that helps developers quickly build high-quality web applications. The development process of Django usually involves two aspects: front-end and back-end, but which aspect of development is Django more suitable for? This article will explore the advantages of Django in front-end and back-end development and provide specific code examples. Advantages of Django in back-end development Django, as a back-end framework, has many advantages, as follows:

To master the role of sessionStorage and improve front-end development efficiency, specific code examples are required. With the rapid development of the Internet, the field of front-end development is also changing with each passing day. When doing front-end development, we often need to process large amounts of data and store it in the browser for subsequent use. SessionStorage is a very important front-end development tool that can provide us with temporary local storage solutions and improve development efficiency. This article will introduce the role of sessionStorage,

New trends in Golang front-end: Interpretation of the application prospects of Golang in front-end development. In recent years, the field of front-end development has developed rapidly, and various new technologies have emerged in an endless stream. As a fast and reliable programming language, Golang has also begun to emerge in front-end development. Golang (also known as Go) is a programming language developed by Google. It is famous for its efficient performance, concise syntax and powerful functions, and is gradually favored by front-end developers. This article will explore the application of Golang in front-end development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

Summary of experience in JavaScript asynchronous requests and data processing in front-end development In front-end development, JavaScript is a very important language. It can not only achieve interactive and dynamic effects on the page, but also obtain and process data through asynchronous requests. In this article, I will summarize some experiences and tips when dealing with asynchronous requests and data. 1. Use the XMLHttpRequest object to make asynchronous requests. The XMLHttpRequest object is used by JavaScript to send
