Sub topic 6
Global styles and where they belong
This chapter assumes you can write a standalone Angular component, know CSS selectors and specificity, and understand that the default Emulated encapsulation scopes component styles to one template. Global styles are the counterpart: CSS that is not scoped to any component.
What counts as a global style
A global style is a rule that Angular does not rewrite. It applies to the whole document, so it reaches every component's elements, including those inside Emulated components.
A new Angular CLI project registers a global stylesheet in angular.json:
{
"styles": ["src/styles.css"]
}Rules in src/styles.css are added to the page as ordinary CSS. Because Emulated encapsulation only adds an attribute selector to component rules, a global rule such as h1 { margin: 0; } matches every <h1> in the app. Component styles can still win over it, because they have a higher specificity from the added attribute.
What belongs in global styles
Put a rule in the global stylesheet when it describes the whole page and not one component.
- Resets and base typography, such as
box-sizingand the body font. - Design tokens, defined as CSS custom properties on
:root. - Utility classes used across many templates, such as
.visually-hidden. - Styles for content Angular does not render, such as markup inserted with
innerHTMLor elements created by a third-party library. - Theme overrides for a component, applied through custom properties on its host element.
Anything that describes how one component looks should stay in that component's own styles.
/* styles.css (global) */
:root {
--brand-color: #3b5bdb;
--font-body: system-ui, sans-serif;
}
*, *::before, *::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: var(--font-body);
}
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
}A component can then use these tokens without importing anything:
import { Component } from '@angular/core';
@Component({
selector: 'app-cta',
template: `<button class="cta"><ng-content /></button>`,
styles: `
.cta {
background: var(--brand-color);
color: white;
border: 0;
padding: 8px 16px;
}
`,
})
export class CtaComponent {}The component defines layout and spacing itself and takes its color from the global token. Changing --brand-color in one place updates every component that uses it.
Ways to register global styles
| Method | Where | Use it for |
|---|---|---|
| `styles` array in `angular.json` | Build configuration | The main stylesheet, and extra files such as a theme |
| `ViewEncapsulation.None` on a component | The component's metadata | A component whose styles must apply app-wide (covered in a later chapter) |
| `::ng-deep` | A component rule | Reaching into child or third-party markup (deprecated) |
The first row is the normal choice. You can list several files, and a bundle can be named with the object form ({ "input": "src/themes/dark.css", "bundleName": "dark", "inject": false }) so it is built but loaded only when you choose.
Common errors
- Global rule seems ignored. A component rule has an extra attribute selector, so it can outrank a global rule with the same selector. Raise the global specificity deliberately, or move the rule into the component.
- Styles apply everywhere unexpectedly. A global element selector like
button { ... }affects every button, including those in third-party components. Prefer a class. - Global file not loading. The file must be listed in the
stylesarray of the build target, and the dev server restarted after changingangular.json. - Component rules duplicated in
styles.css. This hides where a style comes from. Keep the rule next to the template that needs it.
Exercise
A team adds p { color: red; } to styles.css, but one component's own styles contain p { color: blue; }. Which color do paragraphs in that component get, and why?
Show answer
Blue. Emulated encapsulation rewrites the component rule to include an attribute selector (like p[_ngcontent-abc]), which has higher specificity than the global p rule, so the component rule wins for paragraphs in its template. Paragraphs elsewhere stay red.
Exercise
You need the brand color available to every component and changeable in one place. What do you define, and where?
Show answer
Define a CSS custom property such as --brand-color on :root in the global stylesheet listed in angular.json. Components read it with var(--brand-color).