CSS Architecture Block-Element-Modifier (BEM) - SitePoint
BEM (Block-Element-Modifier) is a front-end development methodology, a naming convention, and a set of associated tools. Originating from Yandex, it's designed for efficient development by large teams. This explanation focuses on the core concept and naming system.
BEM promotes viewing websites as collections of reusable component blocks, combinable to build interfaces. A block is a website section (header, footer, sidebar, etc.), as shown in Figure 2.3. Note that "block" here refers to HTML page segments.
Blocks can nest. For example, a header block might contain logo, navigation, and search form blocks (Figure 2.4). A footer could contain a sitemap block.
Elements are more granular than blocks. As the BEM documentation states: "An element is a part of a block that performs a specific function. Elements are context-dependent; they only make sense within their parent block."
A search form block, for instance, includes a text input element and a submit button element (Figure 2.5). Here, "element" refers to design elements, not HTML elements.
A main content block might contain an article list block, which in turn contains article promo blocks. Each promo block could have image, excerpt, and "Read More" elements (Figure 2.6).
Blocks and elements form the core of BEM's naming convention:
- Block names must be unique project-wide.
- Element names must be unique within a block.
- Block variations (e.g., a dark search box) use modifiers in the class name.
Block and element names are separated by two underscores (__
). Modifiers are separated from block/element names by two hyphens (--
).
Here's a BEM-styled search form example:
<div class="search"> <div class="search__wrapper"> <label for="s" class="search__label">Search for:</label> <input type="text" id="s" class="search__input" /> <input type="submit" class="search__submit" value="Search" /> </div> </div>
A dark-themed version:
<div class="search search--inverse"> <div class="search__wrapper search__wrapper--inverse"> <label for="s" class="search__label search__label--inverse">Search for:</label> <input type="text" id="s" class="search__input search__input--inverse" /> <input type="submit" class="search__submit search__submit--inverse" value="Search" /> </div> </div>
Corresponding CSS:
<div class="search"> <div class="search__wrapper"> <label for="s" class="search__label">Search for:</label> <input type="text" id="s" class="search__input" /> <input type="submit" class="search__submit" value="Search" /> </div> </div>
In the markup and CSS, search--inverse
and search__label--inverse
are added classes, not replacements. Only class selectors are used; child and descendant selectors are permitted but should also target classes. Element and ID selectors are avoided. This keeps selector specificity low, prevents side effects, and makes CSS independent of markup patterns. Unique block and element names prevent naming conflicts. Benefits include:
- Improved code readability and understanding for new team members.
- Increased team productivity.
- Reduced naming collisions and side effects.
- CSS independence from markup.
- High CSS reusability.
BEM's scope extends beyond this overview. The official BEM website provides comprehensive details, tools, and tutorials. "Get BEM" is another excellent resource for the naming convention.
(The Frequently Asked Questions section is omitted as it is a repetition of information already present and would significantly increase the length of the output without adding new content.)
The above is the detailed content of CSS Architecture Block-Element-Modifier (BEM) - SitePoint. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
