# Set up Master CSS in Angular

Connect CLI-generated CSS to Angular, run both development watchers, and verify the production stylesheet or optional browser runtime.

Keep Angular's builder and use the Master CSS CLI to generate a stylesheet it can load. During development, run both watchers: Master CSS updates the generated CSS, and Angular serves the app that imports it.

This setup assumes an existing browser-rendered Angular app with a global `src/styles.css`, a working `npm start` command and Node.js 24 or later. It uses Angular's default [emulated encapsulation](https://angular.dev/guide/components/styling#viewencapsulationemulated), which allows global styles to reach component templates. Shadow DOM components need styles inside their own shadow roots.

<a id="installation"></a>

## Installation

<a id="add-master-css"></a>

### Add Master CSS

Run the installer from the Angular project root:

```bash
npm create @master/css@2.1.8 -- --framework angular --yes
```

The static setup adds `@master/css` and `@master/css-cli`. For the installer's optional tooling and preview options, see [Installation](/guide/installation#installation).

<a id="check-the-stylesheet-connection"></a>

### Check the stylesheet connection

The installer creates a Master CSS source entry at the project root:

```css name=master.css
@import '@master/css';
```

It also adds this import to Angular's global stylesheet, preserving its existing contents:

```css name=src/styles.css
@import './master.generated.css';
```

Keep `src/styles.css` in the application's `build.options.styles` array in `angular.json`. Angular's [global styles configuration](https://angular.dev/reference/configs/workspace-config#styles-and-scripts-configuration) determines whether the generated CSS reaches the page. For a nonstandard workspace layout, adjust the output path and import together.

Put Master CSS definitions such as `@theme` and `@utility` in `master.css`. Angular consumes the generated native CSS; its ordinary CSS builder does not compile those definitions. Do not edit `src/master.generated.css` or import it back into `master.css`.

<a id="generate-the-css"></a>

### Generate the CSS

In one terminal, generate the stylesheet and keep watching for changes:

```bash
npx master-css generate --output src/master.generated.css --watch
```

Wait for the initial generation to finish. Then start Angular in a second terminal:

```bash
npm start
```

Keep both processes running. Angular's watcher alone does not regenerate Master CSS when you add a class.

<a id="start-using-master-css"></a>

### Start using Master CSS

Add this markup to the template referenced by your root component. With Angular's [current naming convention](https://angular.dev/cli/new#file-name-style-guide), that is usually `src/app/app.html`; an existing project may use `src/app/app.component.html`.

```html
<p id="master-check" class="w-fit m(0) p-md b-solid b(1) b-line-divider r-md text-sm font-regular bg-surface-raised fg-text-strong">
Hello Angular
</p>
```

```css disclosure=generated-css
@layer theme{:root,:host{--color-zinc-90:oklch(24.6% .0022 265);--color-zinc-100:oklch(15% .0016 265)}:root,:host{--color-surface-raised:light-dark(var(--color-white), var(--color-zinc-90));--color-line-divider:light-dark(oklch(0% 0 none / .12), oklch(100% 0 none / .12));--color-text-strong:light-dark(var(--color-zinc-100), var(--color-white))}:root,:host{--color-white:oklch(100% 0 none)}:root,:host{--font-weight-regular:400;--font-size-sm:.875rem;--text-sm:var(--font-size-sm);--text-sm--line-height:1.5;--text-sm--letter-spacing:0;--radius-md:.375rem;--spacing-md:1rem}}@layer utilities{.text-sm{font-size:var(--text-sm);line-height:var(--text-sm--line-height, normal);letter-spacing:var(--text-sm--letter-spacing, normal)}.bg-surface-raised{background-color:var(--color-surface-raised)}.b-line-divider{border-color:var(--color-line-divider)}.r-md{border-radius:var(--radius-md)}.b-solid{border-style:solid}.b\(1\){border-width:calc(1 * 1px)}.fg-text-strong{color:var(--color-text-strong)}.font-regular{font-weight:var(--font-weight-regular)}.m\(0\){margin:calc(0 * 1px)}.p-md{padding:var(--spacing-md)}.w-fit{width:fit-content}}
```

The paragraph has a neutral surface, a fine border, small corners and 1rem of padding on every side.

<a id="verify-the-result"></a>

## Verify the result

Open the Angular app. The paragraph should match the example's spacing and surface. Change `p-md` to `p-lg` in the template: with both watchers running, its padding should grow from `1rem` to `1.5rem`. Change it back after checking the update.

If the style does not appear, trace the connection in order:

- Check the Master CSS terminal for generation errors and confirm `src/master.generated.css` exists.
- Check the import in `src/styles.css` and that file's registration in `angular.json`.
- Inspect the paragraph in browser devtools for a matching rule and any component style overriding it.

Static generation needs complete class strings in source or an explicit safelist. Keep conditional class names complete; use CSS custom properties for dynamic values. See [Scanning class names](/guide/scanning-and-pruning) when a class is missing from the generated output.

Enjoying Master CSS? [Star it on GitHub](https://github.com/master-co/css) to help other developers find it.

<a id="build-for-deployment"></a>

## Build for deployment

Generate CSS before Angular packages the app:

```bash
npx master-css generate --output src/master.generated.css
npm run build
```

Use the same order in CI. Serving an old generated file can hide missing styles, so check the production build after a fresh generation.

Static describes the CSS generation step. A browser-rendered Angular app still needs JavaScript to create its HTML.

<a id="rendering-modes"></a>

## Choose another rendering mode

Static rendering is the default. Use runtime rendering when classes must be generated from browser DOM changes rather than known source strings.

| Mode | Installer option | CSS delivery |
| --- | --- | --- |
| Static | None | CLI output imported by `src/styles.css` |
| Runtime | `--mode runtime` | Browser runtime started from `src/main.ts` |

**Generate new classes in the browser**

For the runtime setup, run:

```bash
npm create @master/css@2.1.8 -- --framework angular --mode runtime --yes
```

The installer starts `MasterCSSRuntime` from `src/main.ts`, loads `@master/css-runtime/default-program` and observes the document for class changes.

**Finish the global stylesheet setup before starting Angular.** The installer currently adds `@import '@master/css';` to `src/styles.css`. Replace that import with the native base stylesheet below. Keep other native CSS imports and rules your app needs. If switching from the static setup, also remove the `./master.generated.css` import; this runtime path does not need the CSS watcher.

```css name=src/styles.css
@import '@master/css/base.css';
```

The default program supplies the preset vocabulary. Adding `@theme` or `@utility` to Angular's ordinary CSS does not compile a custom runtime program. For project-defined vocabulary, use the static CLI path or supply a separately compiled program through the [runtime API](/reference/packages#css-runtime).

Build and serve the Angular app, then run this in its browser console. The example paragraph should gain an underline without rebuilding:

```js
document.querySelector('#master-check').classList.add('underline')
```

This runtime starts in the browser entry. An Angular SSR app needs a separate initial-CSS strategy; this guide covers the browser-rendered setup.
