Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
BEM Naming with SASS Nesting
📖 Scenario: You are creating styles for a simple card component on a webpage. The card has a title, a description, and a button. You want to use the BEM naming method to keep your CSS organized and easy to read.
🎯 Goal: Build a SASS stylesheet using BEM naming conventions with nesting to style a card component. The card block is card. It has elements __title, __description, and __button. The button also has a modifier --primary for a special style.
📋 What You'll Learn
Create a SASS block selector for .card.
Nest element selectors for .card__title, .card__description, and .card__button inside .card.
Nest a modifier selector .card__button--primary inside .card__button.
Use nesting to keep the code clean and follow BEM naming exactly.
💡 Why This Matters
🌍 Real World
Using BEM with SASS nesting helps keep CSS organized and easy to maintain in real web projects with many components.
💼 Career
Front-end developers often use BEM and SASS nesting to write scalable and readable stylesheets for websites and apps.
Progress0 / 4 steps
1
Create the BEM block selector
Write a SASS selector for the BEM block .card.
SASS
Hint
Start by writing .card { } to define the block.
2
Add nested element selectors
Inside the .card block, nest selectors for the elements .card__title, .card__description, and .card__button using SASS nesting.
SASS
Hint
Use &__element inside .card to nest elements.
3
Add a modifier selector for the button
Inside the nested &__button selector, nest a modifier selector &--primary to style the primary button variant.
SASS
Hint
Inside &__button, write &--primary { } to create the modifier.
4
Add simple styles to complete the card
Add a background color lightgray to .card, a font size 1.5rem to &__title, a font size 1rem to &__description, a padding 0.5rem 1rem to &__button, and a background color blue with white text color to &--primary.
SASS
Hint
Use CSS properties inside each selector to add the styles.
Practice
(1/5)
1. What does the __ double underscore represent in BEM naming?
easy
A. It separates a block from its element
B. It separates a modifier from a block
C. It is used to join two blocks
D. It indicates a CSS property
Solution
Step 1: Understand BEM structure
BEM uses blocks, elements, and modifiers. Elements are parts of blocks.
Step 2: Identify the role of double underscore
The double underscore __ connects a block to its element, e.g., block__element.
Final Answer:
It separates a block from its element -> Option A
Quick Check:
BEM element separator = __ [OK]
Hint: Double underscore links block and element in BEM [OK]
Common Mistakes:
Confusing __ with modifier separator --
Thinking __ joins two blocks
Using __ as a CSS property
2. Which of the following is the correct SASS nesting syntax for a BEM modifier block--active inside .block?
easy
A. &__active { color: red; }
B. .block--active { color: red; }
C. &--active { color: red; }
D. --active { color: red; }
Solution
Step 1: Understand SASS nesting with &
The ampersand & represents the parent selector inside nesting.
Step 2: Apply modifier syntax
For modifier block--active, use &--active inside .block nesting.
Final Answer:
&--active { color: red; } -> Option C
Quick Check:
SASS & + --modifier = &--modifier [OK]
Hint: Use & before --modifier inside block nesting [OK]
Common Mistakes:
Writing full class name inside nesting
Using __ instead of -- for modifiers
Omitting & causing invalid selector
3. Given this SASS code, what CSS selector will be generated for the nested element?
.card {
&__title {
font-weight: bold;
}
}
medium
A. .card__title { font-weight: bold; }
B. .card &__title { font-weight: bold; }
C. .card__title { font-weight: normal; }
D. .card-title { font-weight: bold; }
Solution
Step 1: Understand & usage in SASS
The & represents the parent selector .card.
Step 2: Combine & with __title
Combining .card and __title forms .card__title.
Final Answer:
.card__title { font-weight: bold; } -> Option A
Quick Check:
SASS & + __element = block__element [OK]
Hint: & plus __element creates block__element selector [OK]
Common Mistakes:
Thinking & is literal text in output
Confusing __title with -title
Ignoring font-weight value changes
4. Identify the error in this SASS code using BEM nesting:
&__item, &--active, and &__item--active follow BEM rules correctly.
Step 2: Analyze &-extra
&-extra is invalid because BEM modifiers use double hyphen --, not single hyphen.
Final Answer:
Incorrect use of &-extra for a modifier -> Option D
Quick Check:
BEM modifiers need --, not - [OK]
Hint: Modifiers use --, not single - after & [OK]
Common Mistakes:
Using single hyphen for modifiers
Thinking &--active is invalid
Missing nesting for combined element-modifier
5. You want to style a BEM block .form with an element __input and a modifier --error on the element. Which SASS nesting code correctly applies red border only when the input has the error modifier?