This is an example of well-structured CSS using BEM naming conventions.
1. Use CSS Variables for Maintainability: Define reusable values like colors, fonts, and spacing using --variables inside :root. This way, you can change your theme colors globally just by tweaking variables, instead of hunting down every instance in your CSS.
Example:
:root {
--primary-color: #3f51b5;
--font-stack: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.button {
background-color: var(--primary-color);
font-family: var(--font-stack);
}
2. Follow Consistent Naming Conventions: Use BEM (Block Element Modifier) or similar conventions to keep your CSS organized and avoid clashes. Itβs easier to read and maintain, especially when your project grows big.
Example:
.card { /* Block */
padding: 1rem;
}
.card__title { /* Element */
font-weight: bold;
}
.card--featured { /* Modifier */
border-color: gold;
}
π Quick Quiz
Why should you avoid using !important in CSS?