Discover how a simple symbol can save you hours of repetitive typing and mistakes!
Why Combining & with BEM naming in SASS? - Purpose & Use Cases
Imagine you are styling a website with many buttons and blocks. You write CSS classes like .button, .button--primary, and .button__icon by hand, repeating the block name each time.
Writing full class names repeatedly is tiring and error-prone. If you want to rename the block, you must change every single class manually. It's easy to make mistakes and hard to keep styles consistent.
Using & in Sass lets you combine selectors smartly with BEM naming. You write the block name once, then add modifiers or elements by appending to &. This keeps your code DRY and easy to update.
.button {}
.button--primary {}
.button__icon {}.button {
&--primary {}
&__icon {}
}This approach makes your styles easier to write, read, and maintain, especially for big projects with many components.
When building a website header with blocks like header and elements like header__logo or modifiers like header--sticky, combining & with BEM saves time and reduces errors.
Writing full BEM class names manually is repetitive and error-prone.
Using & in Sass lets you append modifiers and elements easily.
This keeps your CSS cleaner and easier to maintain.
Practice
& symbol represent when used in Sass with BEM naming?Solution
Step 1: Understand the role of
The&in Sass&symbol in Sass means "use the current parent selector" inside nested rules.Step 2: Connect with BEM naming
When combined with BEM,&__elementor&--modifierappends to the parent block's class name.Final Answer:
It refers to the current parent selector in the nested block. -> Option DQuick Check:
&= current parent selector [OK]
- Thinking & creates a new unrelated class
- Confusing & with a CSS variable
- Using & to import files
& to create a BEM modifier for a block named .card?Solution
Step 1: Identify BEM modifier syntax
Modifiers use double dashes:--modifier, so&--activeappends--activeto the parent block.Step 2: Check other options
.card--active { background: green; } repeats the full class, not using&. &__active { background: green; } uses__which is for elements, not modifiers. &-active { background: green; } uses a single dash, which is incorrect for BEM modifiers.Final Answer:
&--active { background: green; } -> Option CQuick Check:
Modifier = &--modifier [OK]
- Using single dash instead of double dash for modifiers
- Confusing elements (__element) with modifiers (--modifier)
- Writing full class name instead of using &
.button {
color: blue;
&__icon {
margin-right: 0.5rem;
}
&--large {
font-size: 2rem;
}
}Solution
Step 1: Understand & usage in nested selectors
The&appends to the parent selector.button. So&__iconbecomes.button__iconand&--largebecomes.button--large.Step 2: Write the generated CSS
The CSS will have three selectors:.button,.button__icon, and.button--largewith their respective styles.Final Answer:
.button { color: blue; } .button__icon { margin-right: 0.5rem; } .button--large { font-size: 2rem; } -> Option BQuick Check:
& + BEM = combined class names [OK]
- Using underscore instead of double underscore for elements
- Not combining & with parent selector
- Leaving & in final CSS
.menu {
&__item {
color: black;
}
&-active {
font-weight: bold;
}
}Solution
Step 1: Check BEM modifier syntax
Modifiers in BEM use double dashes--, so&-activeis incorrect; it should be&--active.Step 2: Verify element syntax
&__itemis correct for elements, so no error there.Final Answer:
The modifier should use double dashes: &--active instead of &-active. -> Option AQuick Check:
Modifier syntax = &--modifier [OK]
- Using single dash for modifiers
- Confusing element and modifier syntax
- Thinking &__item is invalid
.profile with an element avatar and a modifier highlighted. Which Sass code correctly combines & with BEM naming to style both the element and modifier inside .profile?Solution
Step 1: Apply BEM element syntax
Elements use double underscore__, so&__avatarcorrectly targets.profile__avatar.Step 2: Apply BEM modifier syntax
Modifiers use double dash--, so&--highlightedcorrectly targets.profile--highlighted.Step 3: Check other options
Options A, C, and D use single dash for either element or modifier, which is incorrect in BEM.Final Answer:
.profile { &__avatar { border-radius: 50%; } &--highlighted { background-color: yellow; } } -> Option AQuick Check:
Element = &__element, Modifier = &--modifier [OK]
- Using single dash for elements or modifiers
- Mixing element and modifier syntax
- Not nesting inside the block selector
Ready to prove it?
Pass the test and this pattern moves unknown
