Avoid global styles in your Angular projects
Tags: angular
Published: 2022-01-04 00:00:42

---
title: Avoid global styles in your Angular projects
published: true
date: 2021-05-27 16:09:56 UTC
tags: angular
canonical_url: 
---

 ![Avoid global styles in your Angular projects](https://images.unsplash.com/photo-1466784828399-9a9921e8bdfd?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwxMTc3M3wwfDF8c2VhcmNofDF8fG1lc3N8ZW58MHx8fHwxNjIyMTMxNzMw&ixlib=rb-1.2.1&q=80&w=2000)

Originally posted on my blog: https://blog.merckx.fr/avoid-using-global-styles-in-your-angular-projects/

CSS is the language used to style HTML pages. It has a simple set of rules to compute values of properties of every DOM element on a page. Angular provides view encapsulation to make sure component's styles only apply to the given component. However, the style of the DOM elements generated by those components are still affected by global styles. This makes debugging CSS hard.

In every project I worked on, custom CSS code is always at least 10% of total amount of code (in number of lines of code). And we always tend to underestimate the effort needed to maintain it. I never actually measured the time spent working on CSS files but I wouldn't be surprised if it would be around 10% of my time as well. CSS deserves a better place in our all web projects.

In a [good article](https://medium.com/@benlorantfy/avoid-global-styles-3992a759f616), [Ben Lorantfy](https://medium.com/@benlorantfy) compared global styles in CSS with global variables in JS. To summarize, they both share the following issues:

- break encapsulation
- poor readability
- break information hiding
- namespace pollution

Unfortunately, it is very rare to use a UI library that doesn't come with global styles. Bootstrap, Material Design, Prime NG and NG Zorro all come with global styles. However, it doesn't mean that you should add more mess to the mess.

Global styles and especially classes are handy because they enable developers to apply styles to elements by labelling (hopefully, those labels are semantically meaningful but that's another debate). That is something we want to keep in a new approach. The attribute we want to get rid of is that **global styles are globally applied**. Instead, we want **global styles that are locally applied**.

In this post, I'm going to present an approach based on SASS mixins. I quote [the SASS language's website](https://sass-lang.com/documentation/at-rules/mixin):

> Mixins allow you to define styles that can be re-used throughout your stylesheet.

## File structure

The first thing to this approach is to organize your SASS code the same way you would organize your JS/HTML code. Don't neglect it. I put all the global styles under a folder called `styles`. I suggest putting all the mixins in under the `styles/mixins`. Modularize mixins in meaningful packages: containers, lists, tables, inputs, buttons, typography etc. At this point, I would suggest something that would look like:

```
styles/
|__ mixins/
    |__ typography.sass
    |__ containers.sass
    |__ quote.sass
    |__ tables/
        |__ compact-table.sass
        |__ data-table.sass

```

## Mixins in actions

Let's have a look at the `quote` mixin. This mixin can add some quote-like style to an element: a grey background, some padding, some font style and color.

_quote.sass_
```sass
@mixin quote
  font-style: italic
  background: rgb(240,240,240)
  border-left: 2px solid rgb(200,200,200)
  color: rgb(100,100,100)
  padding: 10px
```



Here is how we can use our mixin.

_app.component.html_
```html
<p class="main-quote">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
<p class="second-quote">Molestiae tempore officiis animi sint, corporis officia, vel eaque in, quaerat exercitationem ut.</p>

```

_app.component.sass_
```sass
@use 'mixins/quote'

.main-quote
  @include quote.quote
  font-size: 20px

.second-quote
  @include quote.quote

```

At this point, there are several things to note.

## Styles stay local

We don't have any style applied globally. We apply global mixins (available to the SASS pre-processor) to local styles.

## It keeps the template clean

We assign one class to each `<p>`, the classes are then defined in the SASS file of the component. We can then apply several mixins to each class.

While assigning one class to each `<p>` seems to be a tedious process, it actually makes sure our template stays clean: we don't overload class attributes. Style definitions are specified in the SASS file.

## "Class check" for free

The SASS pre-processor will complain if a mixin doesn't exist, while CSS won't complain if you have a typo in a class.

I will try this approach along with the one described in [An elegant way to enable CSS-customizable Angular components](https://dev.to/kevinmerckx_47/an-elegant-way-to-enable-css-customizable-angular-components-m48) in the next big project I have to lead. I'm curious about the results. The intermediate results on a small project were already promising.

___

KM

Photo by <a href="https://unsplash.com/@ricardoviana?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Ricardo Viana</a> on <a href="https://unsplash.com/s/visual/603b0e9c-d0d6-4d15-b61e-c5f35b1edcd4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>