TExcelTAD
Home › Angular › Styling Components

Styling Components

Angular components carry their own CSS. Styles written in a component apply only to that component's template — a .title rule in one component can't accidentally restyle a .title somewhere else. This lesson covers how that scoping works, the special selectors you get, and where global styles belong.

Component styles

Use styles for inline CSS or styleUrl for a separate file (styleUrls for several files). SCSS works too if you chose it when creating the project.

angular · try it yourself
import { Component } from '@angular/core';

@Component({
  selector: 'app-price-card',
  template: `
    <h3 class="title">Pro plan</h3>
    <p class="price">12 USD <small>/ month</small></p>
    <button>Choose</button>
  `,
  styles: `
    :host { display: block; max-width: 220px; padding: 16px; border-radius: 12px;
            border: 1px solid #f0c2cc; background: #fff7f9; text-align: center; }
    .title { margin: 0; color: #dd0031; }
    .price { font-size: 28px; font-weight: bold; margin: 8px 0; }
    button { background: #dd0031; color: #fff; border: 0; padding: 8px 20px; border-radius: 20px; }
  `,
})
export class PriceCard {}

:host targets the component's own element (<app-price-card>). Custom elements are display: inline by default, so setting display: block on :host is a common first rule.

How scoping works: emulated encapsulation

By default (ViewEncapsulation.Emulated) Angular adds a unique attribute to every element in a component's template, and rewrites the component's CSS selectors to require that attribute:

css
/* you write */
.title { color: #dd0031; }

/* Angular generates something like */
.title[_ngcontent-abc-c12] { color: #dd0031; }

Here is that rewriting, simplified, in plain TypeScript:

ts · try it yourself
function scopeCss(css: string, id: string): string {
  return css.replace(/(\s*)([^{}]+?)\s*\{/g, (_m, ws: string, selectorList: string) => {
    const scoped = selectorList
      .split(',')
      .map((sel) => sel.trim())
      .map((sel) => (sel === ':host' ? `[_nghost-${id}]` : `${sel}[_ngcontent-${id}]`))
      .join(', ');
    return `${ws}${scoped} {`;
  });
}

const css = ':host { display: block; }\n.title, .price { margin: 0; }\nbutton { color: white; }';
console.log(scopeCss(css, 'abc-c12'));
[_nghost-abc-c12] { display: block; }
.title[_ngcontent-abc-c12], .price[_ngcontent-abc-c12] { margin: 0; }
button[_ngcontent-abc-c12] { color: white; }

Because only elements in this component's template carry _ngcontent-abc-c12, the rules can't leak into children or siblings.

Encapsulation modes

Mode Behaviour
ViewEncapsulation.Emulated (default) Scoped via generated attributes
ViewEncapsulation.ShadowDom Uses the browser's real Shadow DOM
ViewEncapsulation.None Styles become global — use sparingly
ts
@Component({ selector: 'app-x', encapsulation: ViewEncapsulation.None, ... })

Special selectors

Selector Meaning
:host The component's host element
:host(.active) The host element when it has class active
:host-context(.dark) Applies when any ancestor has class dark (theming)
::ng-deep .x Pierces encapsulation into children — deprecated, avoid

Styling the host based on state is clean with :host(...) plus a host class binding:

angular · try it yourself
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-chip',
  host: { '[class.selected]': 'selected()' },
  template: `<ng-content />`,
  styles: `
    :host { display: inline-block; padding: 4px 12px; margin: 4px; border-radius: 16px;
            border: 1px solid #ccc; font-size: 14px; }
    :host(.selected) { background: #dd0031; border-color: #dd0031; color: #fff; }
  `,
})
export class Chip {
  selected = input(false);
}

@Component({
  selector: 'app-root',
  imports: [Chip],
  template: `
    <app-chip>All</app-chip>
    <app-chip [selected]="true">Frontend</app-chip>
    <app-chip>Backend</app-chip>
  `,
})
export class App {}

Global styles and theming

Put app-wide rules — fonts, resets, CSS variables — in src/styles.css. CSS custom properties pass through encapsulation, which makes them the best way to theme components:

css
/* styles.css */
:root { --brand: #dd0031; --radius: 8px; }
body { font-family: system-ui, sans-serif; margin: 0; }
css
/* any component */
button { background: var(--brand); border-radius: var(--radius); }

To restyle a child component from a parent, prefer exposing CSS variables or inputs over ::ng-deep.

Tip: Component libraries like Angular Material and CSS frameworks like Tailwind work with Angular. Tailwind classes go straight into templates once you follow Tailwind's Angular installation guide.

Test yourself

What does :host select in a component's styles?

  1. The <body> element
  2. The component's own host element
  3. All child components
  4. The first element in the template

With default encapsulation, can a parent component's .title rule style a .title inside a child component?

  1. Yes, always
  2. No — styles are scoped to the component's own template
  3. Only if the child uses OnPush
  4. Only in production builds

What is the recommended way to let parents theme child components?

  1. ::ng-deep
  2. ViewEncapsulation.None everywhere
  3. CSS custom properties (variables)
  4. Inline styles only