50 CSS Best Practices & Guidelines to Write Better CSS

Featured image for “50 CSS Best Practices & Guidelines to Write Better CSS”

I recently wrote about 15 CSS things to master to become a better web developer and then realized that writing CSS requires so much more than focusing on certain features. Oftentimes, what makes CSS hard is that developers don’t have a set of tools, guidelines, and best practices to follow and use to help them enjoy the art of writing CSS.

Over the years, I have collected a set of rules and tools that helped me a lot in my CSS journey and I would like to share 50 of them with you.

1 — Use a Preprocessor

CSS preprocessors help you write less CSS faster and in a better way. They are full of tools and utilities that help you organize, avoid repetition, and modularize your CSS. I prefer SASS but there are also LESS and Stylus which I enjoy equally as well. The reason I love them is that they don’t introduce a “new way” to style your page. It is still CSS with extra syntax and features.

2 — Separate global vs local style

It is vital to distinguish what styles are meant for any or a set of HTML selectors vs. those meant for something specific. I keep all global styles in a separate file (especially when using a preprocessor) but you can also put it at the top of your CSS file and then focus on setting specific styles for specific components, elements, or sections of the site.

3 — Modularize your style

You don’t need to bundle all CSS in one file unless it will be used. If a user lands on the home page, only include the styles for that page, nothing else. I even go as far as to separate stylesheets into essential vs non-essential styles. Essential styles are those that once the page loads the user sees and non-essential styles are those for components that remain hidden like dialog, Snack-bars, and notifications. Elements or components that require user action to be displayed.

4 — Lazy load stylesheets correctly

There are many ways to lazy load your CSS and it is often easier when using bundlers like WebPack and playing around with dynamic import. You can create your own Javascript CSS loader or you can defer non-critical CSS by playing with the tag when including stylesheets in your page.

5 — Be specific & not too specific

Being specific is good as it defines which style applies to what but once you are too specific it becomes overkill, reduces performance, and increases your bundle size as well. Sometimes it is even an indication of bad CSS or design system. Example of over-specific selectors:

  • *section#sample-section*— (ask why you need to specify “section” along with id)
  • *main div p.title*— (ask why you need to specify anything besides the .title )
  • [disabled] — not specific enough and expensive
  • #sample — the most specific and efficient selector
  • * — global and super expensive (slowest)

Being overly specific sometimes is needed but look at it as an exception rather than a common practice. Not being specific enough can cause style conflict and be too specific makes it hard for the browsers.

6 — Read CSS as the Browser does

Look at the following selector:

*nav ul li a*

You most likely read it from left to right but browsers read it from right to left which means, it finds all the tags on the page then filters it to include only those inside

  • , then again filters it to include only those inside a
      then finally only those inside a
  • Share this article