bem
What is CSS BEM?
Answer
The BEM methodology is a naming convention for CSS classes in order to keep CSS more maintainable by defining namespaces to solve scoping issues. BEM stands for Block Element Modifier which is an explanation for its structure. A Block is a standalone component that is reusable across projects and acts as a โnamespaceโ for sub components (Elements). Modifiers are used as flags when a Block or Element is in a certain state or is different in structure or style.
Here is an example with the class names on markup:
In this case, navbar
is the Block, navbar__link
is an Element that makes no sense outside of the navbar
component, and navbar__link--active
is a Modifier that indicates a different state for the navbar__link
Element.
Since Modifiers are verbose, many opt to use is-*
flags instead as modifiers.
These must be chained to the Element and never alone however, or there will be scope issues.
Good to hear
Alternative solutions to scope issues like CSS-in-JS
Additional links
Last updated