Home Web Front-end CSS Tutorial CSS Architecture Block-Element-Modifier (BEM) - SitePoint

CSS Architecture Block-Element-Modifier (BEM) - SitePoint

Feb 18, 2025 am 09:33 AM

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.

CSS Architecture Block-Element-Modifier (BEM) - SitePoint

Figure 2.3. A homepage might include header, main, and footer blocks.

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.

CSS Architecture Block-Element-Modifier (BEM) - SitePoint

Figure 2.4. A header block encompassing logo, navigation, and search blocks.

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.

CSS Architecture Block-Element-Modifier (BEM) - SitePoint

Figure 2.5. A search block with text input and submit button 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).

CSS Architecture Block-Element-Modifier (BEM) - SitePoint

Figure 2.6. A promotional block for a website article.

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;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.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

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&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

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.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

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

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

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

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

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...

See all articles