How can many developers write CSS for a large interface without naming collisions and style leaks?
When to use it
Large front-end codebases with several contributors. Design systems with reusable components. Projects without CSS-in-JS or scoped styles.
Where it comes from
BEM emerged inside Yandex from 2006 work on Yandex.Music and Ya.Ru; the independent-block layout was presented at ClientSide'07 (Moscow, fall 2007); Lego 2.0 in March 2009 is described by Yandex as the start of BEM; Yandex went open source on GitHub in 2010.
Yandex · 2009
Sheet 2/4
How it works
Every interface part is a block that stands alone. Parts that only make sense inside a block are elements, named block__element. Variations in look or state are modifiers, named block--modifier. Because each class name states what it belongs to, selectors stay flat and low in specificity, and a block can be moved without breaking styles elsewhere.
You need
A component inventory or design system and a team agreement on the naming format.
You get
A consistent set of class names and a flat stylesheet organised by block.
Sheet 3/4
Case
Illustrative case
Orchard Bank's web team has three squads editing one stylesheet and keeps breaking each other's buttons. They adopt BEM. The card component becomes block 'card', with elements 'card__title' and 'card__action', and a modifier 'card--highlighted' for promoted offers. A lint rule rejects selectors that nest more than one class deep. After the rewrite of the account page, a design change to the highlighted card touches one file and no other page changes. Code review comments about unexpected style bleed drop noticeably in the following sprint.
A fictional company used to explain the model.
Sheet 4/4
Caution
When to avoid it
Small one-off pages.
Frameworks that already scope styles per component, where BEM adds verbosity without benefit.
Common mistakes
Nesting elements inside elements (block__a__b).
Using modifiers without the base class.
Creating blocks that depend on their position in the page.
Mixing BEM with unrelated naming styles.
Known weaknesses
Long class names inflate markup.
Relies on discipline since the browser enforces nothing.
Overhead in projects that already scope CSS.
Debate on how to name states and nested parts.
How to apply it in 5 steps
How to apply it
BEM (Block, Element, Modifier)
5 steps
01of 5
Step 1 of 5: Identify blocks
List the independent components of the interface.
01
02of 5
Step 2 of 5: Name elements
Name each part inside a block as block__element.
02
03of 5
Step 3 of 5: Add modifiers
Express variants and states as block--modifier or block__element--modifier.
03
04of 5
Step 4 of 5: Keep selectors flat
Style by single class and avoid nesting on tags or ids.
04
05of 5
Step 5 of 5: Enforce
Add linting and code review checks for the convention.
05
Click to unfold
Use the arrow keys to move between steps, Home and End for the first and last, and Enter to fold or unfold the booklet.
View all 5 steps
01
Identify blocks
List the independent components of the interface.
02
Name elements
Name each part inside a block as block__element.
03
Add modifiers
Express variants and states as block--modifier or block__element--modifier.
04
Keep selectors flat
Style by single class and avoid nesting on tags or ids.
05
Enforce
Add linting and code review checks for the convention.
Which decisions it helps with
Decision profile
A tool to take action on a product or service.
Keyboard: Tab to the sheet, use the arrows to explore the assessments and their questions, and press Escape to close the note.
IIdentity
Type of tool
Other formatDoes not fit neatly into the available categories
The questionWhat kind of tool is it?Assessment · Other format · 41 %
Decision stage
Plan the work, roles and deadlinespossiblealso: generate ideas or options (33 %)
The questionWhat stage of the decision are you at?Assessment · Plan the work, roles and deadlines · 39 % · also Generate ideas or options · 33 %
What it applies to
A product or servicelikely
The questionWhat is the decision about?Assessment · A product or service · 79 %
Discipline
Agile and software developmentvery likely
The questionWhich discipline does it come from?Assessment · Agile and software development · 100 %
IIIResources required
TimeWeeks or monthsThe questionHow much time can you spend on this?Assessment · Weeks or months · confidence 71 %
PeopleSeveral teamsThe questionHow many people will take part?Assessment · Several teams · confidence 85 %
DataA few readily available factsThe questionWhat data do you have available?Assessment · A few readily available facts · confidence 60 %
ExpertiseAnyone who has read a page of instructionsThe questionDoes anyone have experience with methods like these?Assessment · Anyone who has read a page of instructions · confidence 70 %
Time horizonOne to three yearsThe questionHow far ahead do you need to plan?Assessment · One to three years · confidence 54 %
ScopeBetween “a unit or department” and “the whole organization”The questionWho will this decision affect?Assessment · Between “a unit or department” and “the whole organization” · confidence 41 %
FrequencyOccasionallyThe questionIs this a one-off decision or something you expect to repeat?Assessment · Occasionally · confidence 49 %
The team’s own knowledge is enoughThe questionCan you speak to customers, users or people outside your organisation?Assessment · The team’s own knowledge is enough
IITen axes
The dot shows which end the model favours. A hollow dot means no clear preference.
DirectionExecutionThe questionAre you setting direction or improving day-to-day operations?Assessment · Execution · confidence 63 %
Top-downBottom-upThe questionShould leadership set the direction, or should the answer come from the people doing the work?Assessment · Mostly top-down · confidence 62 %
PeopleProcessesThe questionIs this mainly a people problem or a process problem?Assessment · Processes · confidence 65 %
Generate optionsNarrow down optionsThe questionDo you need to generate more options or narrow them down?Assessment · Narrow down options · confidence 62 %
Step-by-step analysisIntuition and experienceThe questionWould you rather work through the evidence step by step or draw on the group’s experience?Assessment · Mostly analysis · confidence 64 %
A strict sequence of stepsA flexible frameworkThe questionWould you prefer a precise set of steps or a framework you can adapt?Assessment · Mostly prescribed steps · confidence 40 %
Protect what we haveTake a riskThe questionDo you need to protect what you have or take a chance on something new?Assessment · Protect what we have · confidence 68 %
Improve what we havemixedFundamental changeThe questionDo you want to improve what you have or change it fundamentally?Assessment · no clear preference (probability is spread across several levels)
Inside the organisationOutside the organisationThe questionWhere do you expect to find the answer, inside your organisation or in the market?Assessment · Inside the organisation · confidence 78 %
Understand firstmixedAct nowThe questionDo you need to understand the situation first or act now and learn as you go?Assessment · no clear preference (probability is spread across several levels)
IVUseful when
Applicable traits
How it supports actionDecide what comes firstThe questionWhat do you need in order to act?Assessment · Decide what comes first · 97 %
Product stageGrowing an existing productThe questionWhat stage is your product at?Assessment · Growing an existing product · 95 %
How was this profile calculated?
An automated system reads each model card and answers the same questions for every model. This profile has answers for 77 of 77. No answers are entered by hand.
A very high probability counts as “yes” and a very low one as “no”. Intermediate answers are hidden.
On the axes, mixed means the model favours neither end.
Wathan, A. (2017). CSS Utility Classes and "Separation of Concerns". adamwathan.me.Objects that BEM-style component classes tied to content force you either to duplicate styles between similar components or to give up the separation of concerns the naming was meant to protect.View source: CSS Utility Classes and "Separation of Concerns" (opens in a new tab) ↗