8 Things You Must Do in CSS
Tags: css, web, frontend
Published: 2021-08-03 10:14:12

---
title: 8 Things You Must Do in CSS
published: true
description: 8 things you must do to write clean code in CSS
tags: css, web, frontend
cover_image: https://images.unsplash.com/photo-1605859465655-84c45e14a0af?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1651&q=80
---

## What is Clean Code?
This term might have different meaning for developers. Although there are several best practices you can follow but there is no exact definition for clean code. 

But for what I have learned and read, this is what a clean code is,
> Clean code is code that is easy to understand, straightforward and consistent, therefore it would be reusable and refactorable.

This is important because as a developer, most of the time we will collaborate with other developers so we need to make sure that our code can be understood easily by everyone in team.
Now, let's take a look on what are the things we must do to write clean code in CSS.


## 1. Write Meaningful and Self Descriptive Name
You should use meaningful and self descriptive names. There are several naming conventions or methodologies that you can follow, such as [BEM](http://getbem.com/introduction/), [OOCSS](https://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/), [SMACSS](http://smacss.com/), [Atomic CSS](https://acss.io/) and many more.


## 2. Prefer CSS Preprocessor
By using CSS Preprocessor, it enables you to use things like variables, nested rules, inline imports, prefix, and more. 
There are several CSS preprocessor, you can choose such as [SASS](https://sass-lang.com/), [LESS](https://lesscss.org/), [PostCSS](https://postcss.org/) etc.

CSS preprocessor is very handy when we are working with team and when our app grows bigger, and by combining with naming convention of your choice it can also help us to avoid repetition in our CSS.


## 3. Write Modular CSS
You should split your CSS files into modules for easier maintenance. If you are using SASS, you can follow the [7-1 pattern](https://sass-guidelin.es/#architecture) for the architecture.


## 4. Use Shorthand properties
Write your CSS in a simpler way using CSS shorthands, you can use shorthands for border, margin, padding, background, font etc.

For example, this is how we create border in CSS without shorthand properties
```css
.border {
  border-width: 1px;
  border-style: solid;
  border-color: #aeaeae;
}

```
with shorthand, we can refactor into this
```css
.border {
  border: 1px solid #aeaeae;
}
```


## 5. Write Flexible CSS Selectors
When writing CSS selectors, don’t make it overly specific, otherwise it would be hard to overwrite. You should maintain low specificity in your selectors.
- You can use element selectors (`h1`, `h2`, `p`) to write styling for your project's basic typography.
- Related to naming, plain css names are great for defining class with low specificity that intended for reusability. For example: `.container`, `.shadow-md`, `.shadow-lg`.


## 6. Avoid `!important`
It has the highest [specificity](https://web.dev/learn/css/specificity/) of all CSS selectors, and if you want to override, you need another `!important` and another `!important` and another. This would make your CSS hard to maintain. 
To avoid this, in any circumstances, please please avoid using `!important` as much as you can. 


## 7. Follow CSS Styleguide
Aside from naming convention, there is CSS styleguide that could help you create maintainable CSS. The most famous one is [Airbnb styleguide](https://github.com/airbnb/css). It is recommended to combine their style guide with BEM or OOCSS.


## 8. Use Code Formatter Tool
You can use a code formatter tool called [Prettier](https://prettier.io/) to help you auto format your code based on your team's preference. You can also integrate with linter, such as [ESlint](https://eslint.org/) for bugs prevention.


## Key Takeaways
Consistent on what you and your team has decided. And just like any other programming language, keep your code DRY (Don't Repeat Yourself) and KISS (Keep It Simple Stupid).


### Reference
- https://dev.to/maimohamed/css-clean-code-5d3p
- https://www.sitepoint.com/golden-guidelines-for-writing-clean-css/ 
- https://levelup.gitconnected.com/writing-your-css-like-this-will-make-your-code-super-clean-f82d4bfeb468 

---

Thanks for reading!
If you want to know more on Javascript clean code, you can head over to [this article](https://dev.to/chrysillala/writing-less-smelly-javascript-code-for-beginners-3jdg)

Would really appreciate if you could share your thoughts and feedback in the comment ✨