Sub topic 1
Declaring styles on a component
Every Angular component can carry its own CSS. This chapter covers the places where you can declare those styles, how Angular combines them, and the mistakes that cause styles to be silently ignored. How the scoping works internally is the subject of the next chapter. Here the focus is on declaration.
Prerequisites
This chapter assumes you can:
- Write CSS selectors and reason about specificity.
- Create a standalone Angular component with a template.
- Read a
@Componentdecorator and its metadata object in TypeScript. - Use basic template syntax such as property binding.
Why styles belong to the component
A component bundles a class, a template and styles into one unit. Keeping the styles in that unit means you can read, move or delete a component without searching a global stylesheet for rules that might affect it. Angular also scopes component styles to the component by default, so a rule like p { color: red; } in one component does not change paragraphs elsewhere.
You declare component styles in the @Component decorator. There are five ways to do it, and you can mix them in one component.
| Mechanism | Where it lives | Typical use |
|---|---|---|
| `styles` | Decorator metadata | A few rules, small components |
| `styleUrl` | Separate file, one path | The usual choice for anything larger |
| `styleUrls` | Separate files, array of paths | Splitting styles across files, or older code |
| `<style>` in the template | Template | Rules that sit next to the markup they affect |
| `<link>` in the template | Template | Pulling in a stylesheet by URL |
CSS @import inside a component stylesheet is a sixth route. It lets one style file include another.
Inline styles with `styles`
The styles property holds CSS directly in the decorator. Since Angular 17 you can pass a single string, which is easiest to write with a template literal:
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class="label"><ng-content /></span>`,
styles: `
:host {
display: inline-block;
}
.label {
padding: 2px 8px;
border-radius: 4px;
background: #e0e7ff;
}
`,
})
export class BadgeComponent {}Older code passes an array of strings instead: styles: ['.label { color: red; }']. Both forms are valid. Each array entry is treated as a separate stylesheet. Use the array form when you need to support Angular versions before 17.
Inline styles suit components with a handful of rules. Once the style block grows past a screen, a separate file is easier to edit and gets proper editor support for CSS.
External files with `styleUrl`
styleUrl points to one stylesheet file. The path is relative to the component's TypeScript file:
import { Component } from '@angular/core';
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrl: './alert.component.css',
})
export class AlertComponent {}/* alert.component.css */
:host {
display: block;
border-left: 4px solid crimson;
padding: 8px 12px;
}
p {
margin: 0;
}The p rule affects only paragraphs in AlertComponent's template. Paragraphs elsewhere keep their own styling.
`styleUrl` and `styleUrls`
styleUrl takes a string and was added in Angular 17. styleUrls takes an array of strings and works in every version:
import { Component } from '@angular/core';
@Component({
selector: 'app-report',
template: `<h2>Report</h2><table><tr><td>Total</td></tr></table>`,
styleUrls: ['./report.base.css', './report.print.css'],
})
export class ReportComponent {}Use styleUrls when you want to split styles across several files. If you have only one file in new code, prefer styleUrl.
Do not declare both a styleUrl and a styleUrls property on the same component. Pick one for each component and keep it consistent.
Styles inside the template
You can also put a <style> element directly in a template. Angular treats it as part of the component's styles, so it is scoped like any other:
import { Component } from '@angular/core';
@Component({
selector: 'app-note',
template: `
<style>
.note { font-style: italic; }
</style>
<p class="note">Remember to save.</p>
`,
})
export class NoteComponent {}A <link rel="stylesheet" href="..."> element in the template is handled the same way: Angular collects it into the component's styles. Both options work, but they split the component's styling between two places. Most teams use styles or styleUrl for consistency and reserve template-level styles for rare cases.
How the pieces combine
You can use several mechanisms in one component. Angular collects every style source (the styles property, the files from styleUrl or styleUrls, and any <style> or <link> elements in the template). It then applies them all to the component, with the same scoping.
Because all sources end up in one scoped set, ordinary CSS rules decide conflicts: higher specificity wins, and for equal specificity the rule that comes later in the final output wins. Do not rely on the order between different sources. If two rules from different sources target the same element and property, raise the specificity of one of them or merge them into one place. That is more reliable than depending on how Angular orders its output.
Sharing rules with `@import`
A component stylesheet can import another file:
/* alert.component.css */
@import './shared-buttons.css';
:host {
display: block;
}The import path is relative to the file that contains it. This helps when several components need the same small set of rules. If you share much more than a few rules, consider whether those rules belong in a global stylesheet instead, as discussed later in this tutorial.
Where component styles stop
Component styles apply to the component's own template. They do not reach into child components' templates. Suppose ParentComponent uses <app-badge> in its template and its stylesheet contains .label { color: red; }. That rule does not change the .label span inside BadgeComponent. It would only affect a .label element that ParentComponent itself renders.
This surprises people on first contact, and it is by design. The next chapter explains the mechanism. Later chapters cover the supported ways to style across the boundary: CSS custom properties, global styles and, as a last resort, ::ng-deep.
Content projected with <ng-content /> belongs to the parent that wrote it, so the parent's styles apply to projected elements, not the child's.
Global styles are separate
Rules in the project's global stylesheet, typically src/styles.css, are listed under the styles option of the build target in angular.json. They are not component styles, and they are not scoped. Do not confuse that option with the styles property of @Component. They share a name but configure different things. Use global styles for resets, typography, design tokens and utility classes. Use component styles for rules that belong to one component.
Common errors
- Wrong property name.
styleUrltakes a string andstyleUrlstakes an array. WritingstyleUrl: ['./a.css']orstyleUrls: './a.css'fails type checking or compilation. Match the property to the value. - Wrong path. Paths in
styleUrlandstyleUrlsare relative to the component file, not to the project root. A missing file stops the build with an error that names the path. - File paths in
styles.styles: './a.css'does not load a file. The text is treated as CSS and is invalid. UsestyleUrlfor files. - Expecting parent rules to reach child templates. The rule is scoped, so it silently does nothing. Check which component's template actually renders the element.
- Sass in inline styles. Inline
stylesare treated as plain CSS unless the project setsinlineStyleLanguageinangular.json. Nested rules in an inline block therefore fail by default. Use a.scssfile withstyleUrl, which the build compiles automatically. Sass is covered in a later chapter. - Mixing up the two
styles. Adding a component rule toangular.jsonmakes it global, and adding a global rule to a component limits it to that component.
Choosing a declaration style
Start with the simplest option that stays readable:
- Under about 20 lines: use
styleswith a template string. - Larger or likely to grow: use
styleUrlwith a file next to the component. - Several separate concerns: use
styleUrls, or use@importto share rules. - Avoid template-level
<style>and<link>unless you have a specific reason.
Whatever you choose, apply it the same way across the codebase. Consistent placement makes it faster to find where a rule lives.
Exercise
A component's decorator contains styleUrl: ['./card.css'] and the build fails. Name the cause and give two valid fixes.
Show answer
styleUrl accepts a single string, but an array was provided. One fix is styleUrl: './card.css'. The other is to change the property to styleUrls: ['./card.css'].
Exercise
ParentComponent has the rule .label { color: red; } in its stylesheet and renders <app-badge>, whose template contains <span class="label">. The badge text stays its original colour. Why, and which kind of rule would have to change to affect it?
Show answer
Component styles are scoped to the component whose template they belong to, so ParentComponent's rule does not apply to elements in BadgeComponent's template. The styling for that span has to come from BadgeComponent's own styles, or from a mechanism that crosses the boundary on purpose, such as CSS custom properties or a global stylesheet.