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.
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.
To complete this guide, you should have:
ng new styles-demo
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.
cd styles-demo
npm i jquery
npm i bootstrap
angular.json
.styles
array before src/style.css
."node_modules/bootstrap/dist/css/bootstrap.min.css",
scripts
array:"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
src > index.html
.class="container"
The final code should look like this:
1<body class="container">
2 <app-root></app-root>
3</body>
Now the project is all set to develop the events page.
ng generate component event-thumbnail
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.1import { Component, Input, Output, EventEmitter } from '@angular/core';
2
3@Component({
4 selector:'app-event-thumbnail',
5 templateUrl:'./event-thumbnail.component.html',
6 styles:[`
7 .green { color:#003300 !important; }
8 .bold { font-weight:bold; }
9 .thumbnail { min-height: 210px; padding-left: 10px; background-color:#343a40; margin-bottom:10px; }
10 .pad-left { margin-left: 10px; }
11 .well div { color: #bbb; }
12 `]
13})
14export class EventThumbnailComponent
15{
16 event:any = {
17 name:'Ng-Europe',
18 date:'09/10/2020',
19 time:'8:00 am',
20 price:'200',
21 onlineUrl:'https://www.pluralsight.com/angular/ng-europe'
22 };
23}
src > app > event-thumbnail > event-thumbnail.component.html
, delete the contents of the file, and add following code:1<div class="well hoverwell thumbnail">
2 <h2>{{event.name | uppercase}}</h2>
3 <div>Date: {{event.date | date}}</div>
4 <div>Time: {{event.time}}</div>
5 <div [ngSwitch]="event.time">
6 <span *ngSwitchCase="'8:00 am'">Early Start</span>
7 <span *ngSwitchCase="'10:00 am'">Late Start</span>
8 <span *ngSwitchDefault>Normal Start</span>
9 </div>
10 <div>Price: {{event.price | currency}}</div>
11 <div>
12 <span>Online URL: {{ event.onlineUrl }}</span>
13 </div>
14</div>
src > app > app.component.html
, delete the contents of the file, and add following code:1<app-event-thumbnail></app-event-thumbnail>
ng serve
http://localhost:4200
. 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.green { color:#003300 !important; }
2.bold { font-weight:bold; }
3.thumbnail { min-height: 210px; padding-left: 10px; background-color:#343a40; margin-bottom:10px; }
4.pad-left { margin-left: 10px; }
5.well div { color: #bbb; }
src > event-thumbnail > event-thumbnail.component.ts
and replace the styles
property in the component decorator with the following code:1styleUrls: ['./event-thumbnail.component.css']
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.