For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/styling/component-styles.md.

Component styles

src/app/card/card.ts
import { Component, input } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-card',
  imports: [LYNX_ELEMENTS],
  template: `<text class="title">{{ title() }}</text>`,
  styles: `
    :host {
      padding: 16px;
      border-radius: 12px;
    }
    .title {
      font-size: 18px;
    }
  `,
})
export class Card {
  readonly title = input.required<string>();
}

styles and styleUrl are scoped to the component, as in Angular on the web:

  • ViewEncapsulation.Emulated, the default, applies a component's rules only to the elements in its own template. The .title rule above doesn't touch a .title in any other component.
  • :host styles the component's own element. :host(.active) and :host-context(.dark) work too. A class set with host: { class: '…' } is on that element, so style it with :host(.…).
  • ::ng-deep turns scoping off for the rest of the selector, to reach into child components. Angular deprecates it: prefer an input or a class on the child.
  • ViewEncapsulation.None makes the component's rules apply app-wide.
  • ViewEncapsulation.ShadowDom falls back to None, with a warning at build time and run time. Lynx has no shadow DOM.
  • A rule that depends on an ancestor's class, such as :host(.active) .title, re-matches when the class toggles at runtime, as other combinators do.
  • Component stylesheets are plain CSS. A .scss or .less file in styleUrl isn't compiled.

How scoping works

Lynx's CSS engine can't match attribute selectors. So AngularLynx scopes with classes where the web uses attributes. In the element tree:

  • A component's elements carry a _ngcontent-<id> class.
  • Its host element carries a _nghost-<id> class.

Type selectors

app-chart {
  height: 100%;
}

Type selectors match the tag the template wrote, as on the web. That holds even though a component's host renders as a <view>. An HTML tag renders as the Lynx element it stands for, such as a <p> as a <text>, and still matches too.

  • How it works: the renderer gives each such element a _ngtag-<tag> class. The build rewrites a type selector naming a tag that isn't a Lynx element to that class.
  • It applies in component and global styles, as in app-chart.active, app-chart > view and ::ng-deep app-chart.
  • Lynx elements' type selectors stay as they are, such as view and text.
  • A rewritten tag ties with a class. A class is more specific than a type selector. So app-chart { color: red } ties with .title { color: blue } on the same element, and the later rule wins. On the web, the class wins.
  • app-root { … } doesn't match the root component. Its host is the page. Style it with page, or with :host in its own styles.

CSS variables keep their names

var(--accent) in a component's styles stays --accent on Lynx.

  • On the web, provideCssVarNamespacing() prefixes the CSS variables in component styles with a namespace: the one you pass it, or else the app's APP_ID. Two apps on one page with different namespaces then can't overwrite each other's variables.
  • On Lynx, the build compiles CSS ahead of time and keeps each variable's name as written. An app has its page to itself, so there's nothing to collide with.
  • What to write: leave provideCssVarNamespacing() out, since nothing on Lynx applies its namespace. Set and read variables by the names your styles use, in stylesheets and in [style.--x] bindings alike.