国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

目錄
What Is BEM?
Why Use BEM?
How to Apply BEM in Practice
1. Start with the Block
2. Add Elements as Needed
3. Use Modifiers for Variations
Common Mistakes to Avoid
首頁(yè) web前端 css教學(xué) 了解CSS BEM方法的命名慣例

了解CSS BEM方法的命名慣例

Jul 06, 2025 am 12:31 AM

BEM是一種可擴(kuò)展且易于維護(hù)的CSS組織方法,其核心是通過(guò)塊、元素和修飾符結(jié)構(gòu)提升代碼清晰度。1. 塊(Block)代表獨(dú)立組件如.card;2. 元素(Element)是塊的組成部分如.card__title;3. 修飾符(Modifier)用于定義變體如.card--featured。這種命名方式提高了團(tuán)隊(duì)協(xié)作效率,避免樣式?jīng)_突,并增強(qiáng)項(xiàng)目可擴(kuò)展性。應(yīng)用時(shí)需注意避免嵌套過(guò)深、確保修飾符與基礎(chǔ)類(lèi)共存,并保持命名一致性。常見(jiàn)錯(cuò)誤包括過(guò)度使用修飾符或混合其他命名規(guī)范。掌握BEM需要實(shí)踐與紀(jì)律,但最終能顯著提升CSS管理效率。

Understanding CSS BEM methodology for naming conventions

When it comes to organizing CSS code in a scalable and maintainable way, the BEM methodology is one of the most widely adopted naming conventions. It helps developers clearly understand how HTML and CSS relate, especially in large projects with multiple contributors.

Understanding CSS BEM methodology for naming conventions

What Is BEM?

BEM stands for Block Element Modifier. It’s a naming convention that gives you a structured way to name your CSS classes so they’re meaningful and easy to understand.

Understanding CSS BEM methodology for naming conventions
  • Block: A standalone component that can be reused (e.g., .menu, .button).
  • Element: A part of a block that has no standalone meaning (e.g., .menu__item, .button__text).
  • Modifier: A flag on a block or element used to change its appearance or behavior (e.g., .menu--vertical, .button--primary).

This structure makes it easier to see at a glance what each class does and how it relates to others.

Why Use BEM?

There are several practical benefits to using BEM:

Understanding CSS BEM methodology for naming conventions
  • Clarity: You can look at a class name like .card__title--highlighted and immediately know it's the title inside a card that has a highlight style.
  • Avoids Conflicts: Since names are scoped within blocks, you're less likely to accidentally override styles elsewhere in the project.
  • Team Collaboration: Everyone on the team uses the same structure, which reduces confusion and speeds up development.
  • Scalability: As your project grows, BEM keeps your CSS organized and modular.

It might feel a bit verbose at first — writing something like .search-form__input-field--disabled instead of just .disabled — but that verbosity actually pays off in readability and maintainability.

How to Apply BEM in Practice

Here’s how to start applying BEM in real-world scenarios:

1. Start with the Block

Identify the main component you're building. For example:

.card {}

2. Add Elements as Needed

Elements are parts of the block:

.card__title {}
.card__content {}
.card__footer {}

3. Use Modifiers for Variations

Modifiers allow for different looks or states:

.card--featured {}
.card__title--large {}

You can also apply modifiers directly to elements if needed:

<div class="card card--featured">
  <h2 class="card__title card__title--large">Featured Post</h2>
</div>

A few notes when working with modifiers:

  • They should only affect their own block or element.
  • Don’t create modifier classes that do too many things — keep them focused.
  • Always include the base class before the modifier, like card card--featured.

Common Mistakes to Avoid

Even experienced developers sometimes misuse BEM without realizing it. Here are a few common pitfalls:

  • Nesting Elements Too Deeply
    Something like .block__element__sub-element isn't necessary. If you need that kind of depth, consider creating a new block instead.

  • Using Modifiers Without the Base Class
    Always include the original block or element class alongside the modifier. Otherwise, you lose context and risk broken styles.

  • Mixing BEM with Other Naming Conventions
    Stick to BEM throughout your project to avoid inconsistency. Mixing methods like SMACSS or utility-first styles can confuse the structure.

  • Overusing Modifiers
    Only use modifiers when there's a clear variation. Not every small style change needs a modifier class.


If you're starting a new project or refactoring an existing one, adopting BEM can make your CSS more predictable and easier to manage. It’s not overly complex, but it does require discipline to follow consistently. Once you get into the rhythm, though, it becomes second nature.

基本上就這些。

以上是了解CSS BEM方法的命名慣例的詳細(xì)內(nèi)容。更多資訊請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)

熱門(mén)話題

如何僅在某些頁(yè)面上包括CSS? 如何僅在某些頁(yè)面上包括CSS? Jun 11, 2025 am 12:01 AM

選擇性包含CSS在特定頁(yè)面上的方法有三種:1.內(nèi)聯(lián)CSS,適用於不常訪問(wèn)或需要獨(dú)特樣式的頁(yè)面;2.使用JavaScript條件加載外部CSS文件,適合需要靈活性的情況;3.服務(wù)器端包含,適用於使用服務(wù)器端語(yǔ)言的場(chǎng)景。這種方法可以?xún)?yōu)化網(wǎng)站性能和可維護(hù)性,但需平衡模塊化與性能。

Flexbox與網(wǎng)格:了解CSS佈局的關(guān)鍵差異 Flexbox與網(wǎng)格:了解CSS佈局的關(guān)鍵差異 Jun 10, 2025 am 12:03 AM

flexboxisidealforone-dimensionAllayouts,while gridsuitStwo,complex layouts.useflexboxforaligningItemsinasingLeaxisAndGridForRidForPreciseconcontroloverroverroverroverroverroverroverrowsandsininintricatientricatedesigns。

使用HTML彈出案創(chuàng)建自動(dòng)關(guān)閉通知 使用HTML彈出案創(chuàng)建自動(dòng)關(guān)閉通知 Jun 10, 2025 am 09:45 AM

HTML彈出屬性將元素轉(zhuǎn)換為頂層元素,可以使用按鈕或JavaScript打開(kāi)和關(guān)閉??梢詫棾霭格g回多種方式,但是沒(méi)有選擇自動(dòng)關(guān)閉它們。 preethi有一種技術(shù),你可以

什麼是'渲染障礙CSS”? 什麼是'渲染障礙CSS”? Jun 24, 2025 am 12:42 AM

CSS會(huì)阻塞頁(yè)面渲染是因?yàn)闉g覽器默認(rèn)將內(nèi)聯(lián)和外部CSS視為關(guān)鍵資源,尤其是使用引入的樣式表、頭部大量?jī)?nèi)聯(lián)CSS以及未優(yōu)化的媒體查詢(xún)樣式。 1.提取關(guān)鍵CSS並內(nèi)嵌至HTML;2.延遲加載非關(guān)鍵CSS通過(guò)JavaScript;3.使用media屬性?xún)?yōu)化加載如打印樣式;4.壓縮合併CSS減少請(qǐng)求。建議使用工具提取關(guān)鍵CSS,結(jié)合rel="preload"異步加載,合理使用media延遲加載,避免過(guò)度拆分與復(fù)雜腳本控制。

如何在無(wú)花果中使用Lotties 如何在無(wú)花果中使用Lotties Jun 14, 2025 am 10:17 AM

在接下來(lái)的教程中,我將向您展示如何在無(wú)花果中創(chuàng)建Lottie動(dòng)畫(huà)。我們將使用兩種彩色設(shè)計(jì)來(lái)超越如何在無(wú)花果上進(jìn)行動(dòng)畫(huà),然後向您展示如何從Figma到Lottie動(dòng)畫(huà)。您只需要免費(fèi)無(wú)花果

打破邊界:用(s)CSS構(gòu)建湯姆拼圖 打破邊界:用(s)CSS構(gòu)建湯姆拼圖 Jun 13, 2025 am 11:33 AM

我們對(duì)其進(jìn)行了測(cè)試,事實(shí)證明,至少在低級(jí)邏輯和拼圖行為時(shí),Sass可以替換JavaScript。除了地圖,混音,功能和大量數(shù)學(xué)外,我們都設(shè)法使我們的Tangram難題栩栩如生,沒(méi)有J

外部與內(nèi)部CSS:最好的方法是什麼? 外部與內(nèi)部CSS:最好的方法是什麼? Jun 20, 2025 am 12:45 AM

thebestapphachforcssdepprodsontheproject'sspefificneeds.forlargerprojects,externalcsSissBetterDuoSmaintoMaintainability andReusability; forsMallerProjectsorsingle-pageApplications,InternaltCsmightBemoresobleable.InternalCsmightBemorese.it.it'sclucialtobalancepopryseceneceenceprodrenceprodrenceNeed

我的CSS必須在較低的情況下嗎? 我的CSS必須在較低的情況下嗎? Jun 19, 2025 am 12:29 AM

否,CSSDOESNOTHAVETOBEINLOWERCASE.CHOMENDENS,使用flowercaseisrecommondendendending:1)一致性和可讀性,2)避免使用促進(jìn)性技術(shù),3)潛在的Performent FormanceBenefits,以及4)RightCollaboraboraboraboraboraboraboraboraboraboraboraboraboraboraboraboraborationWithInteams。

See all articles