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)