Detailed explanation of CSS BEM writing standards

Detailed explanation of CSS BEM writing standards

BEM is a component-based approach to web development. The idea is to separate the user interface into independent blocks, making it easier and faster to develop complex UI interfaces, and to reuse existing code without copying and pasting. BEM consists of Block, Element, and Modifier. The following connectors are used in the selector to expand their relationship:

  • `__: Double underscores are used to connect blocks and their sub-elements
  • ` : used only as a hyphen to connect multiple words of a block or element or modifier (can also be written directly in camel case)
  • --: Double hyphens are used to connect the states of blocks or elements (single underscores can also be used)

Example:

block-name_modifier-name
block-name__element-name--modifier-name
block-name_modifier-name--modifier-value
block-name__element-name--modifier-name--modifier-value

Basic Concepts

Block

The code snippet may be reused and this code does not depend on other components, that is, Block can be used. Blocks can be nested within each other to any depth.
Features:

  • The name of a block describes its purpose. Such as menu, button
  • Blocks cannot affect their environment. This means that you cannot set margin or position for blocks.
  • Only class name selectors can be used, not tag or id selectors.
  • Does not depend on other blocks or elements in the page

Element

Element is a part of Block and has no independent existence. Any Element is semantically bound to a Block.

Features:

  • Use '__' to connect with the block. Such as: block__item
  • Used to describe its purpose. Such as: item, text
  • Elements can be nested within each other, up to any level.
  • Elements are always part of a block. So naming like block__item1__item2 is illegal.

Modifier

Modifier is a tag on a Block or Element. Use them to change style, behavior, or state. The connector for blocks or elements is '--'.

application

Positioning a Block relative to other Blocks

The best approach is to use a mix of blocks and elements. Solve the problem that margin and position cannot be set on block.

example:

<body class="page">
    <!-- header and navigation-->
    <header class="header page__header">...</header>

    <!-- footer -->
    <footer class="footer page__footer">...</footer>
</body>
.page__header {
    padding: 20px;
}

.page__footer {
    padding: 50px;
}

Positioning Elements within a Block

Nesting is achieved by creating additional child elements of Block.

example:

<body class="page">
    <div class="page__inner">
      <!-- header and navigation-->
      <header class="header">...</header>

      <!-- footer -->
      <footer class="footer">...</footer>
    </div>
</body>
.page__inner {
    margin-right: auto;
    margin-left: auto;
    width: 960px;
}

About naming

Selector names must fully and accurately describe the BEM entity they represent.

example:

.button {}
.button__icon {}
.button__text {}
.button_theme_islands {}

We can directly instruct that we are dealing with a block element. Use in html like:

<button class="button button_theme_islands">
    <span class="button__icon"></span>

    <span class="button__text">...</span>
</button>

The following CSS makes it difficult for us to make the same judgment:

.button {}
.icon {}
.text {}
.theme_islands {}

I adopted the BEM specification in my git project miniui and implemented BEM using sass. If you are interested, you can check it out: https://github.com/banyaner/miniui

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Installing Alibaba Cloud Server with Docker and the pitfalls encountered in installing it in a virtual machine (summary of problems)

>>:  JavaScript Sandbox Exploration

Recommend

Detailed explanation of TIMESTAMPDIFF case in MySQL

1. Syntax TIMESTAMPDIFF(unit,begin,end); Returns ...

How to implement input checkbox to expand the click range

XML/HTML CodeCopy content to clipboard < div s...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

js precise calculation

var numA = 0.1; var numB = 0.2; alert( numA + num...

JavaScript CollectGarbage Function Example

First, let's look at an example of memory rel...

How to solve the error "ERROR 1045 (28000)" when logging in to MySQL

Today, I logged into the server and prepared to m...

HTML Code Writing Guide

Common Convention Tags Self-closing tags, no need...

How to configure Nginx's anti-hotlinking

Experimental environment • A minimally installed ...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...

Why are the pictures on mobile web apps not clear and very blurry?

Why? The simplest way to put it is that pixels are...

How to write asynchronous tasks in modern JavaScript

Preface In this article, we'll explore the ev...

Implementation of LNMP for separate deployment of Docker containers

1. Environmental Preparation The IP address of ea...

Native JS to implement drag position preview

This article shares with you a small Demo that ad...

How to implement batch deletion of large amounts of data in MySQL large tables

The question is referenced from: https://www.zhih...