Component Styles
Angular applications are styled with standard CSS. That means you can apply everything you know about CSS stylesheets, selectors, rules, and media queries directly to Angular applications.
Additionally, Angular can bundle component styles with components, enabling a more modular design than regular stylesheets.
Using component styles
For every Angular component you write, you may define not only an HTML template, but also the CSS styles that go with that template, specifying any selectors, rules, and media queries that you need.
One way to do this is to set the styles property in the component metadata. The styles property takes an array of strings that contain CSS code. Usually you give it one string, as in the following example:
@Component({
selector: 'app-root',
template: `
<h1>CSS styles in Angular</h1>
`,
styles: ['h1 {font-weight: normal;}']
})
export class AngularCssComponent {
/* . . . */
}
Style scope
The styles specified in @Component metadata apply only within the template of that component.
They are not inherited by any components nested within the template nor by any content projected into the component.
This scoping restriction is a styling modularity feature.
- You can use the CSS class names and selectors that make the most sense in the context of each component.
- Class names and selectors are local to the component and don't collide with classes and selectors used elsewhere in the application.
- Changes to styles elsewhere in the application don't affect the component's styles.
- You can co-locate the CSS code of each component with the TypeScript and HTML code of the component, which leads to a neat and tidy project structure.
- You can change or remove component CSS code without searching through the whole application to find where else the code is used.
Loading component styles
There are several ways to add styles to a component:
- By setting styles or styleUrls metadata.
- Inline in the template HTML.
- With CSS imports.
Styles in the component metadata
You can add a styles array property to the @Component decorator.
Each string in the array defines some CSS for this component.
@Component({
selector: 'app-root',
template: `
<h1>Angular CSS</h1>
`,
styles: ['h1 { font-weight: normal; }']
})
export class AngularCssComponent {
/* . . . */
}
Note: these styles apply only to this component. They are not inherited by any components nested within the template nor by any content projected into the component.
The Angular CLI command ng generate component defines an empty styles array when you create the component with the --inline-style flag.
````ng generate component hero-app --inline-style```
Style files in the component metadata
You can load styles from external CSS files by adding a styleUrls property to a component's @Component decorator:
@Component({
selector: 'app-root',
template: `
<h1>Angular CSS Tutus</h1>
`,
styleUrls: ['./angular-app.component.css']
})
export class AngularCssComponent {
/* . . . */
}
Note: the styles in the style file apply only to this component. They are not inherited by any components nested within the template nor by any content projected into the component.
You can specify more than one styles file or even a combination of styles and styleUrls.
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.
```ng generate component hero-app```
Template inline styles
You can embed CSS styles directly into the HTML template by putting them inside <style> tags.
@Component({
selector: 'app-hero-controls',
template: `
<style>
button {
background-color: white;
border: 1px solid #777;
}
</style>
<h3>Controls</h3>
<button (click)="activate()">Activate</button>
`
})
Template link tags
You can also write <link> tags into the component's HTML template.
@Component({
selector: 'app-hero-team',
template: `
<link rel="stylesheet" href="../assets/hero-team.component.css">
<h3>Team</h3>
<ul>
<li *ngFor="let member of hero.team">
{{member}}
</li>
</ul>`
})
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.
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.
CSS @imports
You can also import CSS files into the CSS files using the standard CSS @import rule. For details, see @import on the MDN site.
In this case, the URL is relative to the CSS file into which you're importing.
/* The AOT compiler needs the `./` to show that this is local */
@import './hero-details-box.css';
External and global style files
When building with the CLI, you must configure the angular.json to include all external assets, including external style files.
Register global style files in the styles section which, by default, is pre-configured with the global styles.css file.
Non-CSS style files
If you're building with the CLI, you 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:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
The CLI build process runs the pertinent CSS preprocessor.
When generating a component file with ng generate component, the CLI emits an empty CSS styles file (.css) by default. You can configure the CLI to default to your preferred CSS preprocessor as explained in the CLI wiki.
Style strings added to the @Component.styles array must be written in CSS because the CLI cannot apply a preprocessor to inline styles.
Previous: Component lifecycle hooks overview
Next:
Dynamic Components
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/angular/component-styles.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics