Author avatar

Parul Saxena

Using Styles and StyleUrls Component Properties in Angular

Parul Saxena

  • Jul 14, 2020
  • 6 Min read
  • 813 Views
  • Jul 14, 2020
  • 6 Min read
  • 813 Views
Languages Frameworks and Tools
Front End Web Developer
Client-side Frameworks
Angular

Introduction

Every app framework offers different ways of styling HTML components. In Angular, there are two ways to keep CSS code to style your component's HTML template.

  • Keep the CSS inline
  • Keep the CSS separately in its own file

For each of these methods, a different property in component's metadata has to be used. styles property should be used when the number of style properties is less than ten or so. If you have to define a lot of CSS styles, then it is always better to put them in separate files and use the styleUrls property to point to them.

In this guide, you will learn to use those two properties to style your component's HTML template. You will try to render a few properties for a technical event and style the HTML elements.

Prerequisites

To complete this guide, you should have:

  1. Node - 12.17.0
  2. Angular CLI - 9.0.2
  3. Visual Studio Code - 1.45.1

Getting Started

Create an Angular Project

  1. Execute the following command to create an Angular project.

ng new styles-demo

  1. When prompted to add routing to the application, press N.
  2. When prompted to choose stylesheet format, press ENTER.

Opening the Generated Application in Visual Studio Code

  1. Click on the File menu in the top menu bar and select the menu option Open Folder.
  2. In the file dialog box, navigate to the folder where you generated the application.
  3. In this folder, you should see a folder named styles-demo. Select this folder and click Open in the file dialog box.

Setting up the Project

Install Twitter Bootstrap and jQuery and refer them in your project so that you can use some of the form styles to make the form look good.

  1. Go to the command prompt and run following command:

cd styles-demo

  1. Run the following command to install jquery:

npm i jquery

  1. Run the following command to install Bootstrap:

npm i bootstrap

  1. In Visual Studio Code, open the file angular.json.
  2. Add the following line in the styles array before src/style.css.

"node_modules/bootstrap/dist/css/bootstrap.min.css",

  1. Add the following lines in the scripts array:

"node_modules/jquery/dist/jquery.min.js", "node_modules/bootstrap/dist/js/bootstrap.min.js"

  1. In Visual Studio Code, open the file src > index.html.
  2. At line number 10, in the body element, add the following class:

class="container"

The final code should look like this:

1
2
3
<body class="container">
  <app-root></app-root>
</body>

Now the project is all set to develop the events page.

Developing the Events Page

  1. Run the following command in the command prompt:

ng generate component event-thumbnail

  1. In Visual Studio Code, open file src > app > event-thumbnail > event-thumbnail.component.ts, delete the contents of the file, and add the following code. Notice that the styles metadata property has been used to specify CSS styles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
    selector:'app-event-thumbnail',
    templateUrl:'./event-thumbnail.component.html',
    styles:[`
        .green { color:#003300 !important; }
        .bold { font-weight:bold; }
        .thumbnail { min-height: 210px; padding-left: 10px; background-color:#343a40; margin-bottom:10px; }
        .pad-left { margin-left: 10px; }
        .well div { color: #bbb; }
        `]
})
export class EventThumbnailComponent
{
    event:any = {
            name:'Ng-Europe',
            date:'09/10/2020',
            time:'8:00 am',
            price:'200',
            onlineUrl:'https://www.pluralsight.com/angular/ng-europe'
        };
}
  1. In Visual Studio Code, open the file src > app > event-thumbnail > event-thumbnail.component.html, delete the contents of the file, and add following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<div class="well hoverwell thumbnail">
    <h2>{{event.name | uppercase}}</h2>
    <div>Date: {{event.date | date}}</div>
    <div>Time: {{event.time}}</div>
    <div [ngSwitch]="event.time">
        <span *ngSwitchCase="'8:00 am'">Early Start</span>
        <span *ngSwitchCase="'10:00 am'">Late Start</span>
        <span *ngSwitchDefault>Normal Start</span>
    </div>
    <div>Price: {{event.price | currency}}</div>
    <div>
        <span>Online URL: {{ event.onlineUrl }}</span>
    </div>
</div>
  1. In Visual Studio Code, open the file src > app > app.component.html, delete the contents of the file, and add following code:
1
<app-event-thumbnail></app-event-thumbnail>

Launching the Development Server

  1. Run the following command in the command prompt to start Angular Development Server in watch mode: ng serve
  2. If prompted to share Angular CLI usage data, press the N key to not share it.
  3. Open a browser and type the following URL in the address bar to launch the application: http://localhost:4200.
  4. Notice that the event thumbnail is rendering fine with all the styles applied.

Using the styleUrls Property

  1. In Visual Studio Code, open the file src > event-thumbnail > event-thumbnail.component.ts, copy all the CSS code from the styles array, and paste it in the file src > event-thumbnail > event-thumbnail.component.css. The final event-thumbnail.component.css file should look like this:
1
2
3
4
5
.green { color:#003300 !important; }
.bold { font-weight:bold; }
.thumbnail { min-height: 210px; padding-left: 10px; background-color:#343a40; margin-bottom:10px; }
.pad-left { margin-left: 10px; }
.well div { color: #bbb; }
  1. In Visual Studio Code, open the file src > event-thumbnail > event-thumbnail.component.ts and replace the styles property in the component decorator with the following code:
1
styleUrls: ['./event-thumbnail.component.css']
  1. Notice that in the browser, the event thumbnail component is still rendering fine with all the styles applied.

Conclusion

Congratulations!! You have used both styles and styleUrls metadata properties to style your component's HTML template. For more information, refer to the Styles in Angular documentation.

0