{ "id": "guide/component-styles", "title": "Component styles", "contents": "\n\n\n
\n mode_edit\n
\n\n\n
\n

Component styleslink

\n

Angular applications are styled with standard CSS. That means you can apply\neverything you know about CSS stylesheets, selectors, rules, and media queries\ndirectly to Angular applications.

\n

Additionally, Angular can bundle component styles\nwith components, enabling a more modular design than regular stylesheets.

\n

This page describes how to load and apply these component styles.

\n

You can run the in Stackblitz and download the code from there.

\n

Using component styleslink

\n

For every Angular component you write, you may define not only an HTML template,\nbut also the CSS styles that go with that template,\nspecifying any selectors, rules, and media queries that you need.

\n

One way to do this is to set the styles property in the component metadata.\nThe styles property takes an array of strings that contain CSS code.\nUsually you give it one string, as in the following example:

\n\n@Component({\n selector: 'app-root',\n template: `\n <h1>Tour of Heroes</h1>\n <app-hero-main [hero]=\"hero\"></app-hero-main>\n `,\n styles: ['h1 { font-weight: normal; }']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n\n

Style scopelink

\n
\n

The styles specified in @Component metadata apply only within the template of that component.

\n
\n

They are not inherited by any components nested within the template nor by any content projected into the component.

\n

In this example, the h1 style applies only to the HeroAppComponent,\nnot to the nested HeroMainComponent nor to <h1> tags anywhere else in the application.

\n

This scoping restriction is a styling modularity feature.

\n\n\n\n\n\n\n

Special selectorslink

\n

Component styles have a few special selectors from the world of shadow DOM style scoping\n(described in the CSS Scoping Module Level 1 page on the\nW3C site).\nThe following sections describe these selectors.

\n

:hostlink

\n

Use the :host pseudo-class selector to target styles in the element that hosts the component (as opposed to\ntargeting elements inside the component's template).

\n\n:host {\n display: block;\n border: 1px solid black;\n}\n\n\n

The :host selector is the only way to target the host element. You can't reach\nthe host element from inside the component with other selectors because it's not part of the\ncomponent's own template. The host element is in a parent component's template.

\n

Use the function form to apply host styles conditionally by\nincluding another selector inside parentheses after :host.

\n

The next example targets the host element again, but only when it also has the active CSS class.

\n\n:host(.active) {\n border-width: 3px;\n}\n\n\n

The :host selector can also be combined with other selectors.\nAdd selectors behind the :host to select child elements, for example using :host h2 to target all <h2> elements inside a component's view.

\n
\n

You should not add selectors (other than :host-context) in front of the :host selector to style a component based on the outer context of the component's view. Such selectors are not scoped to a component's view and will select the outer context, but it's not native behavior. Use :host-context selector for that purpose instead.

\n
\n

:host-contextlink

\n

Sometimes it's useful to apply styles based on some condition outside of a component's view.\nFor example, a CSS theme class could be applied to the document <body> element, and\nyou want to change how your component looks based on that.

\n

Use the :host-context() pseudo-class selector, which works just like the function\nform of :host(). The :host-context() selector looks for a CSS class in any ancestor of the component host element,\nup to the document root. The :host-context() selector is useful when combined with another selector.

\n

The following example applies a background-color style to all <h2> elements inside the component, only\nif some ancestor element has the CSS class theme-light.

\n\n:host-context(.theme-light) h2 {\n background-color: #eef;\n}\n\n\n

(deprecated) /deep/, >>>, and ::ng-deeplink

\n

Component styles normally apply only to the HTML in the component's own template.

\n

Applying the ::ng-deep pseudo-class to any CSS rule completely disables view-encapsulation for\nthat rule. Any style with ::ng-deep applied becomes a global style. In order to scope the specified style\nto the current component and all its descendants, be sure to include the :host selector before\n::ng-deep. If the ::ng-deep combinator is used without the :host pseudo-class selector, the style\ncan bleed into other components.

\n

The following example targets all <h3> elements, from the host element down\nthrough this component to all of its child elements in the DOM.

\n\n:host ::ng-deep h3 {\n font-style: italic;\n}\n\n\n

The /deep/ combinator also has the aliases >>>, and ::ng-deep.

\n
\n

Use /deep/, >>> and ::ng-deep only with emulated view encapsulation.\nEmulated is the default and most commonly used view encapsulation. For more information, see the\nView Encapsulation section.

\n
\n
\n

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools.\nAs such we plan to drop support in Angular (for all 3 of /deep/, >>> and ::ng-deep).\nUntil then ::ng-deep should be preferred for a broader compatibility with the tools.

\n
\n\n

Loading component styleslink

\n

There are several ways to add styles to a component:

\n\n

The scoping rules outlined earlier apply to each of these loading patterns.

\n

Styles in component metadatalink

\n

You can add a styles array property to the @Component decorator.

\n

Each string in the array defines some CSS for this component.

\n\n@Component({\n selector: 'app-root',\n template: `\n <h1>Tour of Heroes</h1>\n <app-hero-main [hero]=\"hero\"></app-hero-main>\n `,\n styles: ['h1 { font-weight: normal; }']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n\n
\n

Reminder: these styles apply only to this component.\nThey are not inherited by any components nested within the template nor by any content projected into the component.

\n
\n

The Angular CLI command ng generate component defines an empty styles array when you create the component with the --inline-style flag.

\n\nng generate component hero-app --inline-style\n\n

Style files in component metadatalink

\n

You can load styles from external CSS files by adding a styleUrls property\nto a component's @Component decorator:

\n\n \n@Component({\n selector: 'app-root',\n template: `\n <h1>Tour of Heroes</h1>\n <app-hero-main [hero]=\"hero\"></app-hero-main>\n `,\n styleUrls: ['./hero-app.component.css']\n})\nexport class HeroAppComponent {\n/* . . . */\n}\n\n\n \nh1 {\n font-weight: normal;\n}\n\n\n\n\n
\n

Reminder: the styles in the style file apply only to this component.\nThey are not inherited by any components nested within the template nor by any content projected into the component.

\n
\n
\n

You can specify more than one styles file or even a combination of styles and styleUrls.

\n
\n

When you use the Angular CLI command ng generate component without the --inline-style flag, it creates an empty styles file for you and references that file in the component's generated styleUrls.

\n\nng generate component hero-app\n\n

Template inline styleslink

\n

You can embed CSS styles directly into the HTML template by putting them\ninside <style> tags.

\n\n@Component({\n selector: 'app-hero-controls',\n template: `\n <style>\n button {\n background-color: white;\n border: 1px solid #777;\n }\n </style>\n <h3>Controls</h3>\n <button (click)=\"activate()\">Activate</button>\n `\n})\n\n\n\n

You can also write <link> tags into the component's HTML template.

\n\n@Component({\n selector: 'app-hero-team',\n template: `\n <!-- We must use a relative URL so that the AOT compiler can find the stylesheet -->\n <link rel=\"stylesheet\" href=\"../assets/hero-team.component.css\">\n <h3>Team</h3>\n <ul>\n <li *ngFor=\"let member of hero.team\">\n {{member}}\n </li>\n </ul>`\n})\n\n\n
\n

When building with the CLI, be sure to include the linked style file among the assets to be copied to the server as described in the CLI wiki.

\n\n

Once included, the CLI will include the stylesheet, whether the link tag's href URL is relative to the application root or the component file.

\n
\n

CSS @importslink

\n

You can also import CSS files into the CSS files using the standard CSS @import rule.\nFor details, see @import\non the MDN site.

\n

In this case, the URL is relative to the CSS file into which you're importing.

\n\n/* The AOT compiler needs the `./` to show that this is local */\n@import './hero-details-box.css';\n\n\n

External and global style fileslink

\n

When building with the CLI, you must configure the angular.json to include all external assets, including external style files.

\n

Register global style files in the styles section which, by default, is pre-configured with the global styles.css file.

\n

See the CLI wiki to learn more.

\n\n

Non-CSS style fileslink

\n

If you're building with the CLI,\nyou can write style files in sass, less, or stylus and specify those files in the @Component.styleUrls metadata with the appropriate extensions (.scss, .less, .styl) as in the following example:

\n\n@Component({\n selector: 'app-root',\n templateUrl: './app.component.html',\n styleUrls: ['./app.component.scss']\n})\n...\n\n

The CLI build process runs the pertinent CSS preprocessor.

\n

When generating a component file with ng generate component, the CLI emits an empty CSS styles file (.css) by default.\nYou can configure the CLI to default to your preferred CSS preprocessor\nas explained in the CLI wiki.

\n\n
\n

Style strings added to the @Component.styles array must be written in CSS because the CLI cannot apply a preprocessor to inline styles.

\n
\n\n \n
\n\n\n" }