How to Create a Responsive Header in Your Angular Site with Angular Material’s Toolbar

Jacob Naryan - Full-Stack Developer

Posted: Mon Jul 24 2023

Last updated: Wed Nov 22 2023

Angular Material is a UI component library for Angular apps. One of the components it offers is the toolbar component, represented by the tag <mat-toolbar>. This blog post will guide you through how to use this component in your Angular applications.

1. Set Up Your Angular Environment

Before getting started with Angular Material, you need to have an Angular project set up. If you haven’t done this yet, you can follow the official Angular guide on setting up your environment here: Setting up the Local Environment and Workspace.

2. Install Angular Material

Once you have your Angular project, the next step is to install Angular Material. Run the following command in your terminal:

ng add @angular/material

This command will not only install Angular Material, but it will also ask you some questions to customize your installation, such as choosing a prebuilt theme.

3. Import MatToolbarModule

In order to use the toolbar component, you need to import MatToolbarModule in your AppModule (or the specific module where you want to use it). Add the following code to your app.module.ts file:

import { MatToolbarModule } from '@angular/material/toolbar';

@NgModule({
...
imports: [
...
MatToolbarModule,
...
],
...
})
export class AppModule { }

4. Use in Your Component

After importing MatToolbarModule, you can now use in your component’s HTML file. Here is an example of how to do it:

<mat-toolbar color="primary">
My App
</mat-toolbar>

In this example, ‘My App’ is the title of the toolbar and ‘primary’ is the color of the toolbar, which corresponds to the primary color of the theme you chose when installing Angular Material.

5. Customize Your Toolbar

You can customize your toolbar by adding more elements inside it. For example, you can add buttons and icons like this:

<mat-toolbar color="primary">
<button mat-icon-button>
<mat-icon>menu</mat-icon>
</button>
My App
</mat-toolbar>

In this example, a button with a menu icon is added before the title.

And that’s it! Now you know how to use and customize the toolbar component in Angular Material. Have fun coding!

Thank you for reading. If you liked this blog check out more tutorials about software engineering here.

Thanks for reading, please consider tipping some BTC if you liked the blog!
Click or scan the QR code below.

bitcoin address for tips if you are so inclined.

Need a developer?

Hire me for all your Angular needs.