our

Latest Blogs

How to style Angular Components?

iFour Team -June 18, 2021

Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

  •  
  •  
  •  
How to style Angular Components?

Introduction

Angular is an HTML and Typescript-based platform and framework for creating single-page client applications. Typescript is used to write Angular. As a set of Typescript libraries that you load into your apps, it implements core and optional functionality. The architecture of an Angular application is based on a few basic concepts. The Angular framework's fundamental building pieces are Angular components, which are arranged into NgModules. NgModules are functional groupings of linked code; an Angular application is defined by a set of NgModules.

Components are an Angular app's most fundamental UI building block. A tree of Angular components makes up an Angular app. A subset of directives, Angular components are always coupled with a template. Only one component can be instantiated for each element in a template, unlike other directives.

Standard CSS is used to style Angular apps. That means you can directly use your knowledge of CSS stylesheets, selectors, rules, and media queries to Angular applications.

Angular may also package component styles with components, allowing for a more modular design than traditional stylesheets.

The use of component styles

You may define not just an HTML template for each Angular component you create, but also the CSS styles that go with it, including any selectors, rules, and media queries you require

The styles attribute in the component metadata is one approach to accomplish this. An array of strings containing CSS code is passed to the styles property. Normally, you only provide it one string, as in this example:

Page-app.component.ts

@Component({
                  selector: 'app-root',
                  template: `

component style

`, styles: ['h1 { font-weight: normal; }'] }) export class PageAppComponent { /*…………..*/ }

Style scope

The styles supplied in the @Component metadata are only applied to that component's template.

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

This scoping constraint is a feature of the style modularity.

1.You can utilize the CSS selectors and class names that make the most sense in each component's context.

2.The component's class names and selectors are unique to it and do not conflict with any other classes or selectors in the application.

3.Changes to the application's styles have no effect on the component's styles.

4.The CSS code of each component can be co-located with the component's Typescript and HTML code, resulting in a clean and tidy project structure.

5.You can alter or remove component CSS code without having to scan the entire program for other instances of the code.

Styles of components are being loaded.

A component can be styled in a variety of ways:

1.Styles or styleUrls metadata can be set.

2.Inline in the HTML template.

3.With the help of CSS imports.

Each of these loading patterns follows the scope rules stated before.

Component metadata styles

The @Component decorator can have a styles array property.

The CSS for this component is defined by each string in the array.

src/app/page-app.component.ts (CSS inline)

@Component({
                    selector: 'app-root',
                    template: `
                      

component style

`, styles: ['h1 { font-weight: normal; }'] }) export class PageAppComponent { /*…………..*/ }

Remember that these styles are specific to this component. They are not inherited by any nested components in the template, nor are they inherited by any content projected into the component.

When you use the —inline-style flag with the Angular CLI command ng produce component, an empty styles array is defined.

ng generate component page-app --inline-style

In the component metadata, there are style files

By adding the styleUrls attribute to a component's @Component decorator, you can load styles from external CSS files:

@Component({
                selector: 'app-root',
                template: `

component style

`, styleUrls: ['./page-app.component.css'] }) export class PageAppComponent { /* . . . */ }

It's worth noting that the styles in the style file only apply to this component.

You can use a combination of styles and styleUrls or provide several styles files.

When you execute the Angular CLI command ng produce component without the —inline-style argument, it generates an

ng generate component page-app

One-stop-oplossing voor Angular-webontwikkeling Uw zoekopdracht eindigt hier.

Using the style tag, create an inline style template

The style and link tags can be be used to specify style within the component template file, as seen below.

@Component({
                    selector: 'app-page-controls',
                    template: `
                                    

Controls

` })

Using the link tag, create an inline style template

The external style sheets can be added using the link tag, as illustrated below. The path must start with index.html and end with index.html.


 

page works!

You can also use an external source to load the CSS, as seen in the example below.

page works!

@imports in CSS

The usual CSS @import rule can also be used to import CSS files into CSS files. See @import on the MDN site for more information.

The URL in this situation is relative to the CSS file that you're importing.

src/app/page-details.component.css (excerpt)

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

Style files that are external and global

You must setup angular.json to include all external assets, including external style files, when using the CLI.

The styles section, which is pre-configured with the global styles.css file by default, is where you register global style files.

Style files that aren't in the CSS format

You can write style files in sass, less, or stylus and specify them in the @Component.styleUrls metadata with the relevant extensions (.scss,.less,.styl) if you're building using the CLI, as in the following example:

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

The relevant CSS preprocessor is invoked during the CLI build process.

By default, the CLI generates an empty CSS styles file (.css) when generating a component file with ng generate component. You can set your favourite CSS preprocessor as the default in the CLI.

Because the CLI can't apply a preprocessor to inline styles, style strings added to the @Component.styles array must be written in CSS.

Conclusion

With practical examples, this blog has showed you three ways ([1] Styles or styleUrls metadata can be set. [2] Inline in the HTML template. [3] With the help of CSS imports.) to decorate your Angular components.

This ensures that the styles defined are exclusive to the component in question, and that they do not spread to other sections of the program.

Work with us

Well do everything we can to make our next best project!

Our Insights

Check out our most recent blogs

An in-depth guide on Angular Dependency Providers
An in-depth guide on Angular Dependency Providers

What is Angular? Angular is a frontend development framework used for building single-page client applications using HTML and Typescript. It is written in Typescript. What...

A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe
A simple guide to Build Angular Reactive Templates with Ngif and Async Pipe

Angular Templates seamlessly help to expedite the development process with flawless assistance of components and other specific elements. These are dynamic and renders according...

A simple guide on AOT Compilation in Angular
A simple guide on AOT Compilation in Angular

What is a Compiler? A compiler is nothing but a part of code that converts one programming language to another. If we talk about some simple programming languages like C, C++,...

Our Partners

Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo
Logo

Work With Us

Well do everything we can to make our next best project!