Vishalkrv

HomeBlog

CSS Best Practices 2021: Writing Scalable and Maintainable CSS Styles

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.

1. Use a Naming Convention – BEM Methodology

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?

  • Block: The main container, e.g., card.
  • Element: Child elements, e.g., card__title, card__description.
  • Modifier: Variations, e.g., card__button--primary.

This way, your CSS becomes modular and easy to read.

2. Use CSS Variables for Reusability

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?

  • Change one value, and it reflects everywhere.
  • Keeps your styles consistent.
  • Helps in maintaining a theme.

3. Use Utility-First CSS Frameworks Like TailwindCSS

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:

  • bg-blue-500 → Blue background
  • p-4 → Padding
  • rounded-md → Rounded corners

Tailwind helps you write faster, cleaner CSS without creating new classes unnecessarily.

4. Write Mobile-First Responsive CSS

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:

  • Works for smaller devices by default.
  • Easy to scale up for bigger screens.
  • Better performance for mobile users.

5. Use SCSS for Better Organization

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.

6. Avoid Inline 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.

7. Optimize Your CSS for Performance

Too much unused CSS can slow down your site. Use tools like:

  • PurgeCSS – Removes unused CSS.
  • CSS Minification – Compresses your CSS files.

For example, you can use npm to minify your styles:

npm install clean-css-cli -g
cleancss -o styles.min.css styles.css

Conclusion 🌟

In 2021, writing clean and scalable CSS is a must. Follow these practices:

  • ✅ Use the BEM Naming Convention.
  • ✅ Use CSS Variables for reusability.
  • ✅ Try utility-first frameworks like TailwindCSS.
  • ✅ Write Mobile-First Responsive CSS.
  • ✅ Use tools like SCSS for better organization.

If you follow these tips, trust me, your CSS will be cleaner, easier to manage, and ready to scale.

Happy Styling! 🎨