Discover how a simple naming trick with nesting can save you hours of styling headaches!
Why BEM naming with SASS nesting? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are styling a website with many buttons, cards, and headers. You write CSS classes like .button, .button--primary, .card__title, and so on, but you write each style separately and repeat the block names everywhere.
Writing all these class names fully every time is tiring and easy to mess up. You might forget part of the name or write inconsistent styles. Also, your CSS file becomes long and hard to read because related styles are scattered.
BEM naming combined with SASS nesting lets you write styles in a clear, organized way. You write the block once, then nest elements and modifiers inside it. This keeps your code clean and easy to maintain.
.button { color: blue; }
.button--primary { color: white; background: blue; }
.button__icon { margin-right: 0.5rem; }.button {
color: blue;
&--primary {
color: white;
background: blue;
}
&__icon {
margin-right: 0.5rem;
}
}You can write CSS that is easier to read, update, and scale, making your stylesheets neat and your workflow faster.
When building a website with many components like menus, cards, and buttons, using BEM with SASS nesting helps you keep all related styles grouped, so you quickly find and change them without confusion.
BEM naming keeps class names meaningful and consistent.
SASS nesting groups related styles inside blocks.
Together, they make CSS easier to write and maintain.
Practice
__ double underscore represent in BEM naming?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 AQuick Check:
BEM element separator = __ [OK]
- Confusing __ with modifier separator --
- Thinking __ joins two blocks
- Using __ as a CSS property
block--active inside .block?Solution
Step 1: Understand SASS nesting with &
The ampersand&represents the parent selector inside nesting.Step 2: Apply modifier syntax
For modifierblock--active, use&--activeinside.blocknesting.Final Answer:
&--active { color: red; } -> Option CQuick Check:
SASS & + --modifier = &--modifier [OK]
- Writing full class name inside nesting
- Using __ instead of -- for modifiers
- Omitting & causing invalid selector
.card {
&__title {
font-weight: bold;
}
}Solution
Step 1: Understand & usage in SASS
The&represents the parent selector.card.Step 2: Combine & with __title
Combining.cardand__titleforms.card__title.Final Answer:
.card__title { font-weight: bold; } -> Option AQuick Check:
SASS & + __element = block__element [OK]
- Thinking & is literal text in output
- Confusing __title with -title
- Ignoring font-weight value changes
.menu {
&__item {
color: blue;
}
&--active {
color: red;
}
&__item--active {
font-weight: bold;
}
&-extra {
padding: 10px;
}
}Solution
Step 1: Check each nested selector
&__item,&--active, and&__item--activefollow BEM rules correctly.Step 2: Analyze
&-extra&-extrais invalid because BEM modifiers use double hyphen--, not single hyphen.Final Answer:
Incorrect use of &-extra for a modifier -> Option DQuick Check:
BEM modifiers need --, not - [OK]
- Using single hyphen for modifiers
- Thinking &--active is invalid
- Missing nesting for combined element-modifier
.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?Solution
Step 1: Nest element inside block
The element__inputis nested inside.formusing&__input.Step 2: Nest modifier inside element
The modifier--errorapplies to the element, so inside&__inputnest&--error.Step 3: Confirm correct CSS output
This produces.form__input--errorselector with red border, applying only when input has error modifier.Final Answer:
Correct nesting with &__input then &--error inside -> Option BQuick Check:
Modifier nested inside element = &__element { &--modifier } [OK]
- Placing modifier outside element nesting
- Using block--modifier for element modifier
- Not nesting modifier with & inside element
