Angular + CSS Modules

Angular + CSS Modules

We will take a look at different scenarios where using CSS Modules with Angular is a good option, and understand when it's not a perfect choice.

There are some nice articles explaining what are CSS Modules, so I recommend you reading them first:

  • What are CSS Modules and why do we need them?
  • CSS Modules repository

Why would you want to use CSS Modules with Angular?

CSS Modules are useful in the following scenarios:

  1. Your web application is used as web-component or inside an iframe on other website and you want to prevent the host website to override your styles.
  2. You use  ViewEncapsulation.None for your styles so it will help you to prevent conflicts in the styles of your components.
  3. You use external libraries that override your application styles and you want to prevent that.

How to use CSS Modules with Angular?

We can use CSS Modules with Angular through  postcss-modules and  posthtml-css-modules.

First,  postcss-modules hash all the class names in the styles files in build time.

Example: takes app.component.scss

.grid-container {
    display: grid; grid-template-rows: auto;
    grid-template-rows: 90px calc(100vh - 170px) 80px;
    width: 100vw; height: 100vh;

.header {
    background-color: #1ba0f7;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    img {
        width: 64px;
        height: 64px; 

.footer {
    background-color: #1ba0f7;

angular css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Install Angular - Angular Environment Setup Process

Install Angular in easy step by step process. Firstly Install Node.js & npm, then Install Angular CLI, Create workspace and Deploy your App.

Angular Sass: How To Use Sass In Angular 9 Tutorial

Sass in Angular is an extension of CSS that allows you to use things like variables, nested rules, inline imports. Angular supports Sass, CSS, and Less.

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer]( "Hire CSS Developer")**...

Basics of Angular: Part-1

What is Angular? What it does? How we implement it in a project? So, here are some basics of angular to let you learn more about angular. Angular is a Typesc

How to CSS Centering for Angular

Centering elements in CSS either horizontally or vertically was always tricky and developers have used many methods which sometimes didn't even make sense particularly for beginners. But with the advent of Flexbox, CSS centering became easier and clearer than ever.