Sub topic 1
Declaring styles on a component
Prerequisites
This chapter assumes you know the following:
- Basic HTML and CSS, including selectors and specificity.
- How to create an Angular standalone component with a template.
- Basic TypeScript and the
@Componentdecorator. - Template syntax and property binding.
The three ways to attach styles
A component's styles are part of its @Component metadata. Angular gives you three ways to declare them. All three produce the same result: CSS that is scoped to that component's template.
| Metadata property | Value | Use it when |
|---|---|---|
| `styles` | A string or an array of strings | The CSS is short and fits next to the template |
| `styleUrl` | One file path as a string | The CSS deserves its own file |
| `styleUrls` | An array of file paths | You split CSS across several files |
Inline styles
The styles property holds CSS written directly in the decorator. A single template string works well for a few rules.
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 {}The .label rule applies only to elements in this component's template. The :host rule styles the <app-badge> element itself. Chapter 3 covers :host in detail.
Style files
For longer CSS, move the rules into a file next to the component and point to it with styleUrl.
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
templateUrl: './badge.component.html',
styleUrl: './badge.component.css',
})
export class BadgeComponent {}The path is relative to the component's TypeScript file. The build tool reads the file at compile time, so you do not fetch it at runtime. Use styleUrls with an array when you need more than one file. Both styles and styleUrl can appear in the same component, and Angular applies all of them.
Styles inside the template
You can also put a <style> element in the template. Angular scopes it like any other component style.
import { Component } from '@angular/core';
@Component({
selector: 'app-note',
template: `
<style>
p { font-style: italic; }
</style>
<p>Scoped by the component</p>
`,
})
export class NoteComponent {}Prefer styles or styleUrl in most code. Keeping CSS in the metadata makes it easier to find and review.
What scoping does
By default, Angular uses Emulated view encapsulation. When it renders a component, it adds a generated attribute to every element in that component's template. It then rewrites each selector in the component's CSS so that it includes the matching attribute.
Your .label rule becomes something like .label[_ngcontent-abc-c1]. Elements in other components do not carry that attribute, so the rule cannot match them. This gives you scoping without using the browser's native Shadow DOM.
Two consequences follow from this.
- A rule in one component does not affect another component's template. You can reuse a class name such as
.titlein many components without collisions. - Scoped styles do not reach into child components. The parent's
.labelrule will not style a.labelinside a child's template, because those elements carry the child's attribute.
Global rules from styles.css still apply everywhere, and they can still conflict with your component rules through normal specificity. Scoping only limits where component rules apply. It does not block outside rules.
A common mistake
Developers often write a rule in a parent component and expect it to style a child component's inner elements.
import { Component } from '@angular/core';
import { BadgeComponent } from './badge.component';
@Component({
selector: 'app-page',
imports: [BadgeComponent],
template: `<app-badge>New</app-badge>`,
styles: `
.label { background: red; }
`,
})
export class PageComponent {}The red background never appears. The .label span lives in BadgeComponent's template, so the parent's rewritten selector cannot match it. The parent can style the <app-badge> host element, because that element is in the parent's template. To change the inside of a child, use the approaches from later chapters: CSS custom properties, global styles, or the deprecated ::ng-deep.
Summary of the rules
- Declare CSS with
styles,styleUrlorstyleUrlsin@Component. - Component CSS is scoped to that component's template by default.
- A parent can style a child's host element but not the child's inner elements.
Exercise
A component declares styles: ".title { color: navy; }". A child component in its template also contains an element with class="title". Does the child's element turn navy? Explain why.
Show answer
No. Under the default Emulated encapsulation, Angular rewrites the parent's .title selector to require the parent's generated attribute. The child's elements carry the child's attribute, so the rule does not match them.
Exercise
Your component's CSS has grown to 150 lines. Which metadata property should you use to move it into a separate file, and how is the path written?
Show answer
Use styleUrl with a path relative to the component's TypeScript file, for example styleUrl: './badge.component.css'. Use styleUrls with an array if you need several files.