Sub topic 4
Building components with BEM
This chapter assumes you know CSS selectors, specificity, and how to link a stylesheet to an HTML page. It uses the naming ideas from the previous chapter and applies them to one convention: BEM.
What BEM is
BEM stands for Block, Element, Modifier. It is a naming convention. Every class name says what part of the interface it belongs to, so you can read a rule and know where it applies without looking at the HTML.
- A block is a standalone component, such as
cardorbutton. - An element is a part of a block that has no meaning outside it, such as
card__title. Elements are joined to the block with two underscores. - A modifier is a variation of a block or element, such as
card--featured. Modifiers are joined with two hyphens.
Why it keeps specificity low
Each BEM rule uses one class selector. Every rule therefore has the same specificity, (0,1,0), and source order decides which rule wins. You never need .sidebar .card .title or an ID to beat another rule.
.card {
padding: 1rem;
border: 1px solid #ddd;
}
.card__title {
font-size: 1.25rem;
}
.card--featured {
border-color: gold;
}The modifier comes after the block in the file. Both selectors have equal specificity, so .card--featured overrides the border-color from .card.
Using the classes in HTML
A modifier does not replace the block class. You apply both, and the modifier changes only what differs.
<article class="card card--featured">
<h2 class="card__title">Release notes</h2>
<p class="card__body">What changed this week.</p>
</article>Notice that elements are named after the block, not after their position in the DOM. If you later wrap the title in a <header>, the class stays card__title.
Common errors
- Chaining elements. Writing
card__header__titleencodes the DOM depth into the name. Usecard__titleinstead. An element belongs to the block, not to another element. - Using a modifier alone.
<div class="card--featured">loses the base styles. Always keep the block class too. - Nesting selectors. Writing
.card .card__titleraises specificity to (0,2,0) for no benefit. The class name already carries the context. - Styling bare tags inside a block. A rule like
.card h2depends on markup and has higher specificity than a single class. Give the element a class. - Making a block depend on its parent. If
.buttonlooks different inside.card, add a modifier or an element such ascard__action. Do not write.card .button.
When to create a new block
Ask whether the part could be reused alone. A card__title is only a card's title, so it is an element. A button inside a card can appear in many places, so it is a separate block. You place it in the card's markup, optionally with a card__action class on the same tag as a "mix" for layout.
Exercise
A teammate writes .card .card__title { color: red; } and <div class="card--featured">. Name two problems and give the fix for each.
Show answer
First, .card .card__title has specificity (0,2,0) when (0,1,0) is enough, so use .card__title. Second, the modifier is used without its block class, so the base .card styles are missing. Use <div class="card card--featured">.
Exercise
You need a "compact" version of a button where only the padding changes. Write the class name and the CSS rule, following BEM.
Show answer
Use the modifier button--compact with the rule .button--compact { padding: 0.25rem 0.5rem; }. In HTML, apply both classes: class="button button--compact". Place the rule after .button in the file.