Loading...
Loading...
Expert guidance for writing, refactoring, and structuring CSS using BEM (Block Element Modifier) methodology. Provides proper CSS class naming conventions, component structure, and Optics design system integration for maintainable, scalable stylesheets.
npx skill4agent add rolemodel/rolemodel-skills bem-structure| Pattern | Syntax | Example |
|---|---|---|
| Block | | |
| Element | | |
| Block Modifier | | |
| Element Modifier | | |
| Multi-word names | | |
&.block {
.block__element {
&.block__element--modifier { }
}
&.block--modifier { }
}<div class="block">
<div class="block__element">...</div>
<div class="block__element block__element--modifier">...</div>
</div>
<div class="block block--modifier">
<div class="block__element">...</div>
<div class="block__element">...</div>
</div>.block {
.block--modifier { } /* Use `&` before all Modifier class names */
}
.block_element { /* Use `__` between Blocks and Elements and nest Element within the Block */
&.block__element-modifier { } /* Use `--` between the Modifier and its Element */
}<div class="block">...</div>
<div class="block__element">...</div> <!-- Incorrect; Element has become the Block -->
<div class="block__element--modifier">...</div> <!-- Incorrect; Modifier must be accompanied by a corresponding Element -->
</div>
<div class="block__element"> <!-- Incorrect; Element must be accompanied by a corresponding Block -->
<div class="block__element">...</div> <!-- Incorrect; Do not nest an element within itself -->
</div>&&&--&__&.card {
&.card--featured {}
&.card--compact {}
&.card--featured {
&.card--compact {}
}
}.card {
&--featured {}
&__title {}
&__title--large {}
}.block<div class="card">...</div>
<div class="button">...</div>
<div class="menu">...</div>
<div class="header">...</div>
<div class="search-form">...</div>
<div class="user-profile">...</div>
<div class="modal">...</div>
<div class="navigation">...</div>
<div class="dropdown-menu">...</div><div class="searchForm">...</div>
<div class="button_primary">...</div>
<div class="userProfile">...</div>
<div class="dropdown__menu">...</div> <!-- Don't use element syntax for blocks -->.card { }
.button { }
.menu { }
.header { }
.search-form { }
.user-profile { }
.modal { }
.navigation { }
.dropdown-menu { }.searchForm {}
.button_primary {}
.userProfile {}
.dropdown__menu {} /* Don't use element syntax for blocks */
}.block__elem<div class='block'>
<div class='block__element'>
<div class='block2'>
<div class='block2__element'>...</div>
</div>
</div>
</div><div class='block'>
<div class='block__element'>
<div class='element__element'>...</div> <!-- Incorrect; element has become the block -->
</div>
</div>/* Card block with elements */
.card { /* This is the block; elements are inside */
.card__header { }
.card__title { }
.card__body { }
.card__footer { }
.card__image { }
}
/* Menu block with elements */
.menu {
.menu__item { }
.menu__link { }
.menu__icon { }
}
/* Search form block with elements */
.search-form {
.search-form__input { }
.search-form__button { }
.search-form__label { }
}/* Don't create deeply nested element names */
.menu {
.menu__item { }
.menu__item__link { } /* Improper naming structure */
.menu__item__link__icon { } /* Improper naming structure */
}.block--modifier.block__elem--modifier.block--color-black.block--color-red.music-entry__artwork-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.music-entry__artwork-placeholder {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--color-tan) 0%, var(--color-beige) 100%);
}.music-entry__artwork-image {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
&.music-entry__artwork-image--placeholder {
object-fit: unset;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(135deg, var(--color-tan) 0%, var(--color-beige) 100%);
}
}<div class="block block--modifier">...</div>
<div class="block block--size-big block--shadow-yes">...</div><div class="block--modifier utility">...</div> <!-- Missing base block class and using utility class -->.menu {
.menu__item {
&.menu__item--active { }
}
.menu__link { }
.menu__icon { }
&.menu--padded { }
}.menu {
.menu__item {
.menu__item--active { } /* No ampersand */
}
.menu__link { }
.menu-link-active { } /* Bad name structure */
.menu__icon { }
.menu__padded { } /* Improper modifier structure; should be using `&` and `--`, not `__` */
}.user-profile.dropdown-menu__--theme: "xmas"simple: trueinputsubmitsubmitdisabled: true<form class="form form--theme-xmas form--simple">
<input class="form__input" type="text" />
<input
class="form__submit form__submit--disabled"
type="submit" />
</form>.form {
.form__input { }
.form__submit {
&.form__submit--disabled { }
}
&.form--simple { }
&.form--theme-xmas { }
}<form class="form form-simple form--theme-xmas "> <!-- Incorrect; cannot have multiple blocks -->
<input class="form_input" type="text" /> <!-- Incorrect; must use `__` between Block and Element -->
<input
class="form__submit--disabled" <!-- Incorrect; missing Element -->
type="submit" />
</form>.form {
.form__input { }
.form_submit { /* Use `__` between Block and Element */
.form__submit--disabled { } /* Use `&` before all Modifier class names */
}
&.form-simple { } /* Use `--` between Element and Modifier */
}
&.form--theme-xmas { } /* Must nest Elements and Modifiers within Block */