Junior
What are the advantages of the BEM methodology?
sobes.tech AI
Answer from AI
BEM (Block, Element, Modifier) – a methodology for naming CSS classes based on dividing the interface into independent blocks.
Advantages:
- Modularity: Allows creating interface components that can be reused independently.
- Maintainability: Thanks to a clear naming structure, it is easier to find and modify styles without affecting other parts of the project.
- Scalability: Suitable for large projects with many developers, as it reduces the risk of class name conflicts.
- Clarity: Class names reflect the structure and purpose of elements, improving code readability.
- Predictability: Block styles do not depend on the context, which eliminates unexpected behavior.
- Isolation: Styles of one block do not affect styles of another.
Example:
/* Block button */
.button {
display: inline-block;
padding: 10px 20px;
border: 1px solid blue;
}
/* Element icon inside the button block */
.button__icon {
margin-right: 5px;
}
/* Modifier primary for the button block */
.button--primary {
background-color: blue;
color: white;
}
/* Modifier disabled for the button block */
.button--disabled {
opacity: 0.5;
cursor: not-allowed;
}