Academy / Advanced Level: HTML and CSS
CSS Architecture and Methodologies
SMACSS (Scalable and Modular Architecture for CSS)
SMACSS is a flexible CSS architecture that organizes your CSS into modules, making it easier to scale and maintain large codebases. SMACSS defines different types of styles, each with a specific purpose. The focus is on grouping styles into logical categories based on their role.
SMACSS Categories:
- Base Rules: Default styles for HTML elements (e.g.,
body,h1,p). - Layout Rules: Styles that define the major sections of the page (e.g.,
header,footer,main). - Module Rules: Styles for reusable components (e.g.,
card,button). - State Rules: Styles for different states of a component (e.g.,
button--disabled,card--active). - Theme Rules: Styles that define visual themes (e.g., dark mode, color schemes).
Example of SMACSS:
/* Base Rules */
body {
font-family: Arial, sans-serif;
margin: 0;
}
/* Layout Rules */
.header, .footer {
width: 100%;
padding: 20px;
background-color: #333;
color: white;
}
/* Module Rules */
.card {
padding: 20px;
background-color: #f9f9f9;
}
/* State Rules */
.card--active {
background-color: #3498db;
color: white;
}
/* Theme Rules */
.theme-dark .card {
background-color: #2c3e50;
color: #ecf0f1;
}
Key Benefits of SMACSS:
- Scalability: Makes large projects easier to manage by organizing styles into logical categories.
- Flexibility: Provides a framework without enforcing strict rules, allowing developers to adapt it to their needs.
- Modularity: Encourages reusable and maintainable styles by separating base, layout, module, and state styles.