BEM helps keep CSS organized by naming parts clearly. SASS nesting lets you write CSS inside CSS, making it easier to follow BEM rules.
BEM naming with SASS nesting
.block {
// block styles
&__element {
// element styles
}
&--modifier {
// modifier styles
}
}The & symbol means "use the parent selector here".
BEM uses block, block__element, and block--modifier naming.
.button { background: blue; &__icon { margin-right: 0.5rem; } &--large { font-size: 1.5rem; } }
.card { border: 1px solid #ccc; &__header { font-weight: bold; } &__body { padding: 1rem; } &--highlighted { border-color: gold; } }
This example shows a blue button with an icon and larger size using BEM naming and SASS nesting compiled CSS.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>BEM with SASS Nesting Example</title> <style> /* Compiled CSS from SASS below */ .button { background-color: #007bff; color: white; padding: 0.75rem 1.5rem; border: none; border-radius: 0.375rem; font-size: 1rem; display: inline-flex; align-items: center; cursor: pointer; } .button__icon { margin-right: 0.5rem; font-size: 1.25rem; } .button--large { font-size: 1.5rem; padding: 1rem 2rem; } </style> </head> <body> <button class="button button--large" aria-label="Send message"> <span class="button__icon" aria-hidden="true">📧</span> Send </button> </body> </html>
Use aria-label and aria-hidden for accessibility on icons and buttons.
Nesting helps avoid repeating the block name but don't nest too deep to keep CSS simple.
BEM naming makes it easy to find styles related to a component in your CSS files.
BEM naming breaks CSS into blocks, elements, and modifiers for clarity.
SASS nesting lets you write BEM styles inside the block for cleaner code.
Use BEM with nesting to keep your CSS organized and easy to 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
Ready to prove it?
Pass the test and this pattern moves unknown
