Angular course ยท Module 13: UI and Styling
Angular Material
In this lesson3
Angular Material is the official UI component library - like a ceremonial tea set.
Installation
ng add @angular/material
# Choose:
# - Pre-built theme (e.g., Indigo/Pink)
# - Global typography styles
# - BrowserAnimationsModule animationsBasic Components
import { Component } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatMenuModule } from '@angular/material/menu';
@Component({
selector: 'app-dojo-dashboard',
standalone: true,
imports: [
MatButtonModule,
MatCardModule,
MatIconModule,
MatInputModule,
MatFormFieldModule,
MatToolbarModule,
MatMenuModule
],
template: `
<!-- Toolbar -->
<mat-toolbar color="primary">
<span>Dojo Dashboard</span>
<span class="spacer"></span>
<button mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>more_vert</mat-icon>
</button>
<mat-menu #menu="matMenu">
<button mat-menu-item>Settings</button>
<button mat-menu-item>Logout</button>
</mat-menu>
</mat-toolbar>
<!-- Cards -->
<mat-card>
<mat-card-header>
<mat-card-title>Samurai Profile</mat-card-title>
<mat-card-subtitle>Clan: Tokugawa</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<mat-form-field appearance="outline">
<mat-label>Name</mat-label>
<input matInput placeholder="Enter name">
<mat-icon matPrefix>person</mat-icon>
</mat-form-field>
</mat-card-content>
<mat-card-actions align="end">
<button mat-button>Cancel</button>
<button mat-raised-button color="primary">Save</button>
</mat-card-actions>
</mat-card>
`,
styles: [`
.spacer {
flex: 1 1 auto;
}
mat-card {
max-width: 400px;
margin: 2rem auto;
}
`]
})
export class DojoDashboardComponent {}Custom Material Theme
// custom-theme.scss
@use '@angular/material' as mat;
// Define colors
$samurai-primary: mat.m2-define-palette((
50: #fce4ec,
100: #f8bbd9,
500: #c41e3a, // main
700: #ad1457,
contrast: (
50: rgba(black, 0.87),
500: white,
)
));
$samurai-accent: mat.m2-define-palette(mat.$m2-amber-palette);
// Create theme
$samurai-theme: mat.m2-define-light-theme((
color: (
primary: $samurai-primary,
accent: $samurai-accent,
),
typography: mat.m2-define-typography-config(),
density: 0,
));
// Apply theme
@include mat.all-component-themes($samurai-theme);Code for this lesson: src/app/app.component.ts
import { Component, signal, ViewEncapsulation } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule],
encapsulation: ViewEncapsulation.Emulated, // Default encapsulation
template: `
<div class="styling-guide">
<h1>Styling in Angular</h1>
<section>
<h2>ViewEncapsulation</h2>
<div class="encapsulation-types">
<div class="type emulated">
<h3>Emulated (default)</h3>
<p>Angular adds unique attributes to CSS selectors</p>
<code>[_ngcontent-xxx]</code>
</div>
<div class="type shadow">
<h3>ShadowDom</h3>
<p>Uses real Shadow DOM</p>
<code>encapsulation: ViewEncapsulation.ShadowDom</code>
</div>
<div class="type none">
<h3>None</h3>
<p>Global styles - no encapsulation</p>
<code>encapsulation: ViewEncapsulation.None</code>
</div>
</div>
</section>
<section>
<h2>Special selectors</h2>
<div class="selectors-demo">
<div class="selector">
<h3>:host</h3>
<code>:host { display: block; }</code>
<p>Styles the component host element</p>
</div>
<div class="selector">
<h3>:host-context()</h3>
<code>:host-context(.dark-theme) { ... }</code>
<p>Styles dependent on parent context</p>
</div>
<div class="selector">
<h3>::ng-deep</h3>
<code>::ng-deep .child { ... }</code>
<p>Penetrates encapsulation (deprecated)</p>
</div>
</div>
</section>
<section>
<h2>CSS Variables (Custom Properties)</h2>
<div class="css-vars-demo">
<div class="controls">
<label>
Primary color:
<input type="color" [value]="primaryColor()" (input)="onColorChange($event)">
</label>
<label>
Border radius:
<input type="range" min="0" max="30" [value]="borderRadius()" (input)="onRadiusChange($event)">
{{ borderRadius() }}px
</label>
</div>
<div class="preview-box"
[style.--primary-color]="primaryColor()"
[style.--border-radius]="borderRadius() + 'px'">
<h3>Preview</h3>
<button>Button</button>
<div class="card">Card</div>
</div>
</div>
</section>
<section>
<h2>Style binding</h2>
<pre><code>{{ stylesCode }}</code></pre>
</section>
<section>
<h2>Tailwind-like utility classes</h2>
<div class="utilities-demo">
<div class="utility-box p-4 m-2 rounded bg-primary">
p-4 m-2 rounded bg-primary
</div>
<div class="utility-box p-2 m-4 rounded-lg bg-secondary">
p-2 m-4 rounded-lg bg-secondary
</div>
<div class="utility-box p-6 m-1 rounded-full bg-accent">
p-6 m-1 rounded-full bg-accent
</div>
</div>
</section>
</div>
`,
styles: [`
:host {
display: block;
}
.styling-guide {
font-family: 'Segoe UI', sans-serif;
padding: 20px;
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
min-height: 100vh;
color: #ecf0f1;
}
h1 {
text-align: center;
color: #3498db;
}
h2 {
color: #3498db;
border-bottom: 2px solid #3498db;
padding-bottom: 10px;
}
section {
background: rgba(255,255,255,0.05);
border-radius: 10px;
padding: 20px;
margin: 20px 0;
}
.encapsulation-types {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
}
.type {
padding: 15px;
border-radius: 8px;
}
.type.emulated {
background: rgba(46, 204, 113, 0.2);
border: 2px solid #2ecc71;
}
.type.shadow {
background: rgba(52, 152, 219, 0.2);
border: 2px solid #3498db;
}
.type.none {
background: rgba(231, 76, 60, 0.2);
border: 2px solid #e74c3c;
}
.type h3 {
margin-top: 0;
}
.type code {
display: block;
background: rgba(0,0,0,0.3);
padding: 8px;
border-radius: 4px;
font-size: 0.85em;
}
.selectors-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
}
.selector {
background: rgba(0,0,0,0.3);
padding: 15px;
border-radius: 8px;
}
.selector h3 {
margin-top: 0;
color: #3498db;
}
.selector code {
display: block;
background: rgba(52, 152, 219, 0.2);
padding: 8px;
border-radius: 4px;
margin-bottom: 10px;
}
.css-vars-demo .controls {
display: flex;
gap: 20px;
flex-wrap: wrap;
margin-bottom: 20px;
}
.css-vars-demo label {
display: flex;
align-items: center;
gap: 10px;
}
.preview-box {
--primary-color: #3498db;
--border-radius: 8px;
background: rgba(0,0,0,0.3);
padding: 20px;
border-radius: 10px;
}
.preview-box h3 {
color: var(--primary-color);
}
.preview-box button {
background: var(--primary-color);
color: white;
border: none;
padding: 10px 20px;
border-radius: var(--border-radius);
cursor: pointer;
margin-right: 10px;
}
.preview-box .card {
display: inline-block;
background: var(--primary-color);
color: white;
padding: 15px 25px;
border-radius: var(--border-radius);
}
pre {
background: #1a252f;
padding: 15px;
border-radius: 8px;
overflow-x: auto;
border-left: 4px solid #3498db;
}
code {
color: #ecf0f1;
font-family: 'Fira Code', monospace;
font-size: 0.85em;
}
/* Utility classes */
.utilities-demo {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.utility-box {
color: white;
font-size: 0.85em;
}
.p-2 { padding: 8px; }
.p-4 { padding: 16px; }
.p-6 { padding: 24px; }
.m-1 { margin: 4px; }
.m-2 { margin: 8px; }
.m-4 { margin: 16px; }
.rounded { border-radius: 4px; }
.rounded-lg { border-radius: 8px; }
.rounded-full { border-radius: 50px; }
.bg-primary { background: #3498db; }
.bg-secondary { background: #2ecc71; }
.bg-accent { background: #9b59b6; }
`]
})
export class AppComponent {
primaryColor = signal('#3498db');
borderRadius = signal(8);
stylesCode = `// Single style binding
<div [style.color]="textColor">Text</div>
<div [style.font-size.px]="fontSize">Text</div>
// Multiple styles binding
<div [ngStyle]="{
'color': textColor,
'font-size.px': fontSize,
'background': isActive ? 'green' : 'red'
}">Text</div>
// Class binding
<div [class.active]="isActive">...</div>
<div [class]="dynamicClass">...</div>
// NgClass for multiple classes
<div [ngClass]="{
'active': isActive,
'disabled': isDisabled,
'highlight': isHighlighted
}">...</div>
// CSS Variables in Angular
<div [style.--primary-color]="primaryColor">
<button>Uses --primary-color</button>
</div>`;
onColorChange(event: Event): void {
const input = event.target as HTMLInputElement;
this.primaryColor.set(input.value);
}
onRadiusChange(event: Event): void {
const input = event.target as HTMLInputElement;
this.borderRadius.set(+input.value);
}
}Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the :host selector used for in an Angular component's styles?
2. Which command adds Angular Material to a project (configuring theme and animations)?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Horizontal ordering
Arrange on a single line the import of the Angular Material buttons module.
- Code editor
Complete the form using Angular Material.
- Vertical ordering
Order the Material card elements by their order of appearance in the template (top to bottom).