Do you avoid adding unnecessary CSS classes?

Last updated by Geordie Robinson [SSW] 3 months ago.See history

When making or editing CSS or HTML content it is important to avoid adding classes and ID's unnecessarily.

It can be tempting to add classes on elements, as it is often the most obvious solution to CSS problems... but doing so will lead to overly cluttered code and a host of overly specific solutions. When working on CSS it is almost always better to reuse rather than adding additional classes.

You should use a front-end framework, like Bootstrap or Tailwind. The best front-end frameworks will include most of the clases you will need on a project. Basically you will only need new classes for very specific layout elements.

HTML:

<a class="view-all-link" href="https://www.youtube.com/playlist?list=PLIzW_0dAIKv3mjBeK8eyJbe1bOGWJX_UV">View All</a>

CSS:

.view-all-link{
  margin-top: 0;
}

Figure: Bad example - The "view-all-link" class was added unnecessarily

HTML:

<a class="mt-0" href="https://www.youtube.com/playlist?list=PLIzW_0dAIKv3mjBeK8eyJbe1bOGWJX_UV">View All</a>

Figure: Good example - Using Bootstrap's class "mt-0" has the same affect without adding any class

We open source. Powered by GitHub