SCSS (SASS) combined with Block-Element-Modifier (BEM) methodology helps create maintainable, scalable, and reusable CSS for modern web applications. Following best practices ensures a consistent and structured approach to styling.
π What is BEM?
BEM (Block-Element-Modifier) is a methodology for writing clean and organized CSS.
Block (.block) β A standalone component (e.g., .card)
Element (.block_element) β A part of the block (e.g., .card_title)
Modifier (.block--modifier) β A variation of the block or element (e.g., .card--dark)
π‘ Example:
`.card {
background: white;
padding: 16px;
border-radius: 8px;
&__title {
font-size: 18px;
font-weight: bold;
}
&__button {
padding: 8px 12px;
border: none;
cursor: pointer;
}
&--dark {
background: #333;
color: white;
}
}`
β Why?
Ensures clarity and modularity
Avoids CSS conflicts
Increases reusability
1οΈβ£ Keep Nesting Minimal
SCSS allows nesting, but deep nesting increases specificity and makes styles harder to override.
π¨ Avoid this:
.card {
&__title {
.icon {
color: red;
}
}
}
β Better approach:
.card__title .icon {
color: red;
}
β Keep nesting limited to 2 levels for maintainability.
2οΈβ£ Use SCSS Variables for Consistency
Define common styles using variables to maintain consistency and make updates easier.
`$primary-color: #007bff;
$secondary-color: #6c757d;
$border-radius: 8px;
.button {
background-color: $primary-color;
border-radius: $border-radius;
color: white;
}`
β
Why?
Easier maintenance and updates
Consistency across styles
3οΈβ£ Use Mixins for Reusability
SCSS mixins allow reusable style patterns.
`
@mixin button($bg-color) {
background-color: $bg-color;
padding: 10px 15px;
border: none;
cursor: pointer;
}
.button {
@include button(#007bff);
}
.button--secondary {
@include button(#6c757d);
}`
β
Why?
Avoids repetition
Improves scalability
4οΈβ£ Organize SCSS Using the 7-1 Pattern
For large projects, follow the 7-1 architecture pattern for better organization.
/scss
βββ abstracts/ (variables, mixins, functions)
βββ base/ (reset, typography, global styles)
βββ components/ (buttons, cards, modals)
βββ layout/ (grid, header, footer)
βββ pages/ (home, about, contact)
βββ themes/ (light, dark theme)
βββ vendors/ (third-party styles)
βββ main.scss (imports everything)
π‘ Example import structure:
@import "abstracts/variables";
@import "base/reset";
@import "layout/grid";
@import "components/button";
β
Why?
Enhances code maintainability
Avoids CSS conflicts
5οΈβ£ Avoid Using ID Selectors
ID selectors (#id) have high specificity and make styles harder to override.
π¨ Bad Practice:
#header {
background: red;
}
β
Use a BEM class instead:
.header {
background: red;
}
β Stick to class-based selectors for flexibility.
6οΈβ£ Use @extend with Caution
@extend can reduce repetition, but overusing it may cause bloated styles.
π¨ Avoid this:
`.error {
color: red;
}
.alert {
@extend .error; // Might create unintended dependencies
}
β
Better approach:
.alert {
color: red;
}`
7οΈβ£ Use Utility Classes for Common Styles
Instead of adding extra modifiers, use utility classes for repeated styles.
β Example:
`.u-text-center {
text-align: center;
}
.u-mt-20 {
margin-top: 20px;
}`
π Usage:
Welcome
β Keeps BEM focused on components while utilities handle layout styles.
8οΈβ£ Write Mobile-First SCSS
Use mobile-first media queries for responsiveness.
`
.button {
padding: 10px 15px;
@media (min-width: 768px) {
padding: 12px 20px;
}
}`
β
Why?
Optimized for mobile devices first
Ensures better performance
9οΈβ£ Keep SCSS Modular with Separate Files
Instead of writing all styles in one file, split them into separate SCSS files.
β Example:
/components/_button.scss
/components/_card.scss
Then import them into main.scss:
@import "components/button";
@import "components/card";
β
Why?
Increases reusability
Keeps styles organized
π Use CSS Grid & Flexbox Instead of Floats
SCSS works well with modern layout techniques like Flexbox and CSS Grid.
π¨ Avoid using floats:
`
.container {
float: left;
width: 50%;
}`
β
Use Flexbox instead:
.container {
display: flex;
justify-content: space-between;
}
π Final Thoughts
Writing SCSS in BEM style improves code maintainability, readability, and scalability.
β
Key Takeaways:
β Follow BEM naming conventions
β Limit nesting depth
β Use variables & mixins
β Organize SCSS using the 7-1 pattern
β Avoid ID selectors & deep specificity
β Write mobile-first styles
By following these best practices, you can write clean, efficient, and scalable SCSS! π―
Top comments (0)