Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Understanding of CSS BEM
Preethi⚡
Preethi⚡

Posted on • Edited on

     

Understanding of CSS BEM

Hey Gang, Feel lucky to see you again. Every year design trends getting changed and then we plan to adapt our websites for trendy and present day feel. Sadly, website may sometimes styled without any structure or naming conventions. This leads to anunmaintainable CSS codebase and crack out our plans😥😥 be like.
Image description

Uh, feel awkward right. But, don't getting trouble with that because every problem definitely have a solution. Likewise, we convertunmaintainable CSS codebase to maintainable using BEM naming convention.

BEM -BlockElementModifier

BEM is constructed on basis ofclass name selector only which not useid ortag name selector.

Image description

Above image seems little bit weired😥. It's not a big deal and hope keep continue reading and you grasp that tricks🎉.

Block

Block is a standalone entity that is meaningful on its own and is a top-level abstraction of a new component.

For instance,

  • header
  • container
  • menu
  • checkbox
  • input

Naming

Block names may consist ofLatin letters, digits, and dashes. To form a CSS class, add a short prefix for namespacing:.block

//BlocksarenamedasstandardCSSclasses.block{}//example.card{}
Enter fullscreen modeExit fullscreen mode

Element

Element is a parts of a block (Child items of block) and have no standalone meaning. Any element is semantically tied to its block.

For instance,

  • menu item
  • list item
  • checkbox caption
  • header title

Naming

Element names may consist of Latin letters, digits, dashes and underscores.
CSS class is formed as block name followed by two underscores and then element name:.block__elem

//Elementsdeclaredwith2underscores,afterblock.block__element{}//example.card__title{}
Enter fullscreen modeExit fullscreen mode

Modifier

Flags on blocks or elements. Use them to change appearance, behavior or state.

In simple words, the use of a ‘modifier’ to apply the additional unique styles.

For instance,

  • disabled
  • highlighted
  • checked
  • fixed

Naming

Modifier names may consist of Latin letters, digits, dashes and underscores.
CSS class is formed as block’s or element’s name followed by two dashes (--):

  • If block's modifier, then.block--mod.
  • If Element's modifier, then.block__elem--mod.
//Modifiersdeclaredwith2dashes,afterblock.block--modifier{}//example.card--dark{}//Modifiersdeclaredwith2dashes,afterelement.block__element--modifier{}//example.card__btn--large{}
Enter fullscreen modeExit fullscreen mode

Hey still with me🥳, I feel you getting curious about BEM. So, It's my pleasure to show the live demo. Just hover the containers, you may getting an confidence about block-element relationship (parent-child relationship).

I can get your question🤔

there are so many naming conventions likeOOCSS,SMACSS,SUITCSS,Atomic. But, why we choose BEM?? right.

Image description

The answer is..

If we are reading the markup instead of CSS, we should be able to quickly get an idea of which element depends on another (in the previous example we can see that .card__title depends on .card, even if we don’t know what that does just yet.)

The BEM approach ensures that everyone who participates in the development of a website works with a single codebase and speaks the same language. Using proper naming will prepare you for the changes in design of the website.

Use BEM as it way

ofcourse you can break down the rule of BEM like below,

//completelyunrelated.nav.nav__listItem.btn--orange{background-color:green;}
Enter fullscreen modeExit fullscreen mode

yes you are right which seems BEM naming convention. But, it's not😑. which only make confusion and incosistent code. Hope you grasp my point.

Final instance for BEM

Hope you getting curious for using BEM in your projects. So, getting clear thought about BEM. I will show a little massive example below,

Image description

Image credits goes to99elements

let's test BEM skills. From above image, can you identify which one isblock /element /modifier. Don't hesitate, post your answer below as comment.

If you loved this blog, Then give an endearing heart💝and drop your thought about this blog😍 which really a lot to me. I love the discussion with you, If you feel not comfortable at styling concepts or have any doubts.

Thanks for Reading!!
Preethi
- Make your CSS life easier

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️
Blog: https://blog.but.gayMastodon: @darkwiiplayer@tech.lgbtPronouns: en.pronouns.page/@darkwiiplayer
  • Pronouns
    she/her;q=1, they/them;q=0.8, */*;q=0.2
  • Joined
• Edited on• Edited

I still think the problem these naming conventions are trying to solve result directly from people deeply misunderstanding the design and purpose of CSS.

Luckily, the direction CSS is evolving in seems to still follow the path of its intended design, with@scope addressing many of the problems people attempt to fix with conventions like BEM.

Soon, selectors like.nav__link {/* Rules */} could turn into@scope (#main-nav) { a:link {/* Rules */} }

CollapseExpand
 
anandbaraik profile image
Anand-Baraik
  • Joined

Nicely put everything. Very well explained. 👌🏻

CollapseExpand
 
preethi_dev profile image
Preethi⚡
Frontend Engineer | Working with design system
  • Location
    India
  • Education
    B.E Computer Science and Engineering
  • Joined

Hii Anand-Baraik, appreciate your response and which shows you grasped that concepts🎉. hope we will meet on upcoming blogs. Happy learning.

CollapseExpand
 
anandbaraik profile image
Anand-Baraik
  • Joined

Yeah sure. Looking forward for your upcoming contents.

CollapseExpand
 
suhakim profile image
sadiul hakim
I am a learner .

very nice job❤

CollapseExpand
 
preethi_dev profile image
Preethi⚡
Frontend Engineer | Working with design system
  • Location
    India
  • Education
    B.E Computer Science and Engineering
  • Joined

Hii sadiul hakim, Happy to see your comment and really appreciate your support.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Frontend Engineer | Working with design system
  • Location
    India
  • Education
    B.E Computer Science and Engineering
  • Joined

More fromPreethi⚡

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp