Learn the best tips and practices to write clean, scalable, and maintainable CSS styles in 2021 – from BEM naming to TailwindCSS and modern techniques.
Vishal Kumar Verma
Feb 25 2021 - 4 min read
Namaste Developers! 👋
If you’ve been working on web development, you know how tricky CSS can get. Over time, your stylesheets can turn into a messy, unmanageable jungle, especially when your project grows.
Here, I’ll share simple and practical CSS best practices that will help you write clean, scalable, and maintainable CSS.
When your project has a lot of styles, naming your classes properly is the first step to avoiding confusion.
BEM stands for Block-Element-Modifier. It’s a simple way to name CSS classes in a clear and structured manner.
Example of BEM:
<div class="card"> <h2 class="card__title">Welcome to BEM</h2> <p class="card__description">This is a clean way to structure your CSS classes.</p> <button class="card__button card__button--primary">Click Me</button> </div>
CSS:
.card { border: 1px solid #ddd; border-radius: 8px; padding: 16px; } .card__title { font-size: 1.5rem; color: #333; } .card__description { font-size: 1rem; color: #555; } .card__button { background: #ccc; border: none; padding: 8px 12px; cursor: pointer; } .card__button--primary { background: #007bff; color: white; }
What’s Happening Here?
This way, your CSS becomes modular and easy to read.
CSS variables (also known as custom properties) make your styles reusable and easier to update.
For example, if you are using the same color or font size across your project, define it as a variable.
CSS Variables Example:
:root { --primary-color: #007bff; --secondary-color: #6c757d; --font-size-base: 16px; } body { font-size: var(--font-size-base); color: var(--secondary-color); } .button { background-color: var(--primary-color); color: white; padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; } .button:hover { background-color: darken(var(--primary-color), 10%); }
Why Use CSS Variables?
Writing long custom stylesheets can get repetitive. That’s where frameworks like TailwindCSS come in.
TailwindCSS lets you use utility classes to style your HTML directly.
TailwindCSS Example:
<div class="bg-blue-500 text-white p-4 rounded-md shadow-md"> <h1 class="text-2xl font-bold">Hello, TailwindCSS!</h1> <p class="text-sm">Utility-first CSS makes styling faster.</p> </div>
Instead of writing custom CSS, you use predefined utility classes like:
Tailwind helps you write faster, cleaner CSS without creating new classes unnecessarily.
In 2021, most users browse on their phones first. So, always write mobile-first CSS.
Start by designing for small screens, and then add media queries for larger devices.
Example:
/* Base styles (Mobile) */ .container { font-size: 14px; padding: 8px; } /* Tablet */ @media (min-width: 768px) { .container { font-size: 16px; padding: 16px; } } /* Desktop */ @media (min-width: 1024px) { .container { font-size: 18px; padding: 24px; } }
Benefits of Mobile-First Approach:
If you’re working on a big project, plain CSS can get hard to manage. Tools like SCSS (Sass) help you organize your styles better.
Here’s an example of using SCSS nesting:
.card { border: 1px solid #ddd; border-radius: 8px; padding: 16px; &__title { font-size: 1.5rem; color: #333; } &__button { background: #007bff; color: white; border: none; &:hover { background: #0056b3; } } }
When you compile this SCSS, it will turn into clean, standard CSS.
Using inline CSS like style="color: red;" might be quick, but it’s not scalable. Always keep your styles separate.
Instead, use classes or external stylesheets to keep things clean and maintainable.
Too much unused CSS can slow down your site. Use tools like:
For example, you can use npm to minify your styles:
npm install clean-css-cli -g cleancss -o styles.min.css styles.css
In 2021, writing clean and scalable CSS is a must. Follow these practices:
If you follow these tips, trust me, your CSS will be cleaner, easier to manage, and ready to scale.
Happy Styling! 🎨