1. Code
  2. JavaScript
  3. Angular

How to Build a Login UI With Angular and Material Design

Scroll to top

This tutorial will show you how to build a beautiful login and registration UI with Angular Material. We'll look at the various Material design components and how they can be used to bring about professional-looking applications. The complete code for this tutorial can be found in our GitHub repo.

Getting Started

We will start by creating an Angular application. Hopefully, you already have Node.js and Angular CLI installed in your working environment.

Use the following command to create a new Angular app.

1
# create a new Angular project under the name responsive-angular-material

2
ng new registration-login-angular-material

This command creates all the files needed to bootstrap an Angular application. The next step will be to add Angular Material in your application. 

1
cd responsive-angular-material/
2
3
#install angular material 
4
ng add @angular/material

Creating Components Using Angular CLI

Components in Angular are the building blocks of the application. Although you can manually create components, Angular CLI makes it very easy to automatically generate components that contain all the necessary starter code. To generate a component in Angular CLI, you simply run the following command:

1
# MyComponent is the name of your component

2
ng g component MyComponent

Our app will have the following components:

  • the Registration component
  • the Login Component

Let's generate those components now.

1
#registration component

2
ng g component RegistrationComponent
3
4
#login component

5
ng g component LoginComponent

Adding a Theme

The beauty of Angular Material is that it comes with pre-built themes, so you can easily bootstrap the look and feel of your application by simply plugging in some simple snippets of code to your app. To add a theme, simply import it to style.css.

1
/*style.css*/
2
@import "~@angular/material/prebuilt-themes/indigo-pink.css";

Building the UI

We want our UI to feature a navigation bar with links for registration and login. So we'll create a navigation bar with two buttons that link to the app components.

To create a toolbar and buttons, you use the <mat-toolbar> and <mat-button> components respectively.

Go ahead and add the HTML code for the UI in src/app/app.component.html. Notice that we have also added links to the routes.

1
<!--The content below is only a placeholder and can be replaced.-->
2
<mat-toolbar color="primary">
3
   <mat-toolbar-row>
4
     <!--  <span>HOME</span> -->
5
      <span><a href="/">HOME</a></span>
6
      <span class="spacer"></span>
7
      <a mat-button routerLink="/register">Register</a>
8
      <a mat-button routerLink="/login">Login</a>
9
   </mat-toolbar-row>
10
</mat-toolbar>

Next, we will add some styling to our UI. Open app.component.css and add the following CSS styles.

1
.spacer {
2
  flex: 1 1 auto;
3
}

Importing Angular Material Components

You also need to import the modules from @angular/material. You can choose to import them from the main module or create a separate module that will contain all the Material design modules. Since we will be working with many of these modules when creating the rest of the UI, we will create a separate module for all the components. Go ahead and create a new file src/app/material.module.ts.

Add the modules for the <mat-toolbar> , </mat-toolbar-row>, and <mat-button> components in the file as shown below.

1
import { NgModule } from  '@angular/core';
2
3
import {MatButtonModule,MatToolbarModule} from  '@angular/material';
4
5
6
@NgModule({
7
imports: [MatButtonModule,MatToolbarModule],
8
exports: [MatButtonModule,MatToolbarModule],
9
10
})
11
12
export  class  MyMaterialModule { }

Then, from other components in our code, we only need to include the module we just created—app/app.module.ts—as shown below.

1
// app/app.module.ts

2
import { MyMaterialModule } from  './material.module';
3
4
@NgModule({
5
  imports: [
6
    BrowserModule,
7
    BrowserAnimationsModule,
8
    MyMaterialModule,
9
10
  ],
11
})

Issue the ng serve command to test your application, and you should see a nice-looking navigation area with Register and Login links.

navigation area with Register and Login linksnavigation area with Register and Login linksnavigation area with Register and Login links

Enable Routing

Routing enables users to navigate between different pages. To enable routing in our application, we will first define the routing configuration. To do that, add the following code to app.module.ts.

1
import { RouterModule, Routes } from '@angular/router';
2
3
imports: [
4
    BrowserModule,
5
    BrowserAnimationsModule,
6
    MyMaterialModule,
7
    RouterModule.forRoot([
8
      { path: '', redirectTo: '/', pathMatch: 'full' },
9
      { path: 'register', component: RegistrationComponentComponent },
10
      { path: 'login', component: LoginComponentComponent },
11
      
12
    
13
    ]),
14
15
  ],

The next step will be to add a <router-outlet></router-outlet> element which enables the Angular Router to know where to place the routed components. The app.component.html file should now look like this:

1
<!--The content below is only a placeholder and can be replaced.-->
2
<mat-toolbar color="primary">
3
   <mat-toolbar-row>
4
     <!--  <span>HOME</span> -->
5
      <span><a href="/">HOME</a></span>
6
      <span class="spacer"></span>
7
      <a  mat-button  routerLink="/register">Register</a>
8
      <a  mat-button  routerLink="/login">Login</a>
9
   </mat-toolbar-row>
10
</mat-toolbar>
11
<router-outlet></router-outlet>

We've placed the router outlet so the components will render below the navigation bar.

Registration UI

In this section, we will mainly make use of the layout and form control components. This UI will showcase these Material Design components:

  • card component (<mat-card>)—a content container for text, photos, and actions in the context of a single subject
  • label component (<mat-label>)—used to specify a label for the form field
  • input component (<input matInput>)—specifies an input field
  • form field component (<mat-form-field>)—wraps several Angular components to make a form field
  • checkbox component (<mat-checkbox>)—a native checkbox with enhanced Material Design styling
  • date picker component (<mat-datepicker>)—an enhanced date picker

But first, let's import them in src/app/material.ts.

1
import { NgModule } from  '@angular/core';
2
import {MatNativeDateModule,MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule, MatCardModule,MatFormFieldModule,MatInputModule,MatRadioModule,MatListModule,} from  '@angular/material';
3
import {MatDatepickerModule} from  '@angular/material/datepicker';
4
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
5
6
@NgModule({
7
imports: [MatNativeDateModule,MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule,FormsModule, MatCardModule,MatFormFieldModule,MatInputModule,MatListModule,MatRadioModule,],
8
9
exports: [MatNativeDateModule,FormsModule,
10
MatDatepickerModule,MatIconModule,MatButtonModule,MatCheckboxModule, MatToolbarModule, MatCardModule,MatFormFieldModule,MatInputModule,MatListModule,MatRadioModule,],
11
12
})
13
14
export  class  MyMaterialModule { }

We will start with the <mat-card>, which will contain all the content in the registration UI. Open registration-component.component.html and add the following code.

1
<mat-toolbar>
2
   <span>Registration</span>
3
</mat-toolbar>
4
<mat-card class="my-card">
5
   <mat-card-content>
6
       
7
     <!-- CONTENT HERE -->
8
       
9
   </mat-card-content>
10
   <mat-card-actions>
11
     <!-- REGISTER BUTTON -->
12
   </mat-card-actions>
13
</mat-card>

Next, we will add an HTML form inside the content section that will contain all our form fields.

1
 <mat-toolbar>
2
  <span>Registration</span>
3
</mat-toolbar>
4
<mat-card class="my-card">
5
   <mat-card-content>
6
      <form class="my-form">
7
        <!--FORM FIELDS HERE-->
8
      </form>
9
   </mat-card-content>
10
   <mat-card-actions>
11
      <!--REGISTER BUTTON HERE-->
12
   </mat-card-actions>
13
</mat-card>

Next, we will add form fields for the first name, last name, address, email, and password. We will be using <mat-label> to create labels and <input matInput /> to create the input fields.

1
<mat-card-content>
2
  <form class="my-form">
3
     <mat-form-field class="full-width">
4
            <mat-label>First Name</mat-label>
5
            <input  matInput  placeholder="First name"  name="fname"  required>
6
         </mat-form-field>
7
         <mat-form-field class="full-width">
8
            <mat-label>Last Name</mat-label>
9
            <input  matInput  placeholder="Last Name" name="lname"  required>
10
         </mat-form-field>
11
         <mat-form-field class="full-width">
12
            <mat-label>Address</mat-label>
13
            <input  matInput  placeholder="Address" name="address"  required>
14
         </mat-form-field>
15
         <mat-form-field class="full-width">
16
            <mat-label>Email</mat-label>
17
            <input  matInput  placeholder="Email" name="email">
18
         </mat-form-field>
19
         <mat-form-field class="full-width">
20
            <mat-label>Password</mat-label>
21
            <input  matInput  placeholder="Password"  name="password">
22
         </mat-form-field>
23
  </form>
24
</mat-card-content>

Next, we will add a checkbox for gender and a date picker for date of birth. We will use <mat-checkbox> and <mat-datepicker> which have enhanced Material Design styling and animations.

1
 <section class="example-section">
2
      <label class="example-margin">Gender:</label>
3
      <mat-radio-group [(ngModel)]="gender">
4
        <mat-radio-button class="example-margin" value="after">Male</mat-radio-button>
5
        <mat-radio-button class="example-margin" value="before">Female</mat-radio-button>
6
        <mat-radio-button class="example-margin" value="before">Other</mat-radio-button>
7
      </mat-radio-group>
8
    </section>
9
10
    <mat-form-field>
11
      <mat-label>Date of Birth</mat-label>
12
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
13
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
14
  <mat-datepicker #picker></mat-datepicker>
15
</mat-form-field>

The last bit will be a button after the </mat-card-content> for submitting the user information.

1
<mat-card-content>
2
  <form class="my-form">
3
     <!--FORM FIELDS--> 
4
  </form>
5
</mat-card-content>
6
<mat-card-actions>
7
  <button mat-raised-button (click)="register()" color="primary">REGISTER</button>
8
</mat-card-actions>

Form Styling

Let's put some styling on our forms to make them more presentable. Open create-account.component.css and add the following CSS styles.

1
.my-form{
2
  min-width: 150px;
3
  max-width: 500px;
4
  width: 100%;
5
}
6
7
.full-width {
8
  width: 100%;
9
}

The Registration UI will look like this:

Registration UI layoutRegistration UI layoutRegistration UI layout

Building the UI for the Login Component

The login UI will have the following components:

  • card component (<mat-card>)—a content container for text, photos, and actions in the context of a single subject
  • input component (<input matInput>)—specifies an input field

Just like we did for the registration UI, we will have a Material card component to house the login form.

The HTML code for the Login page is shown, which contains two inputs for email and password credentials.

1
<mat-toolbar>
2
  <span>LOGIN</span>
3
</mat-toolbar>
4
<mat-card class="my-card">
5
   <mat-card-content>
6
      <form class="my-form">
7
         <mat-form-field class="full-width">
8
            <mat-label>Email</mat-label>
9
            <input  matInput  placeholder="Email" name="email">
10
         </mat-form-field>
11
         <mat-form-field class="full-width">
12
            <mat-label>Password</mat-label>
13
            <input  matInput  placeholder="Password" name="password">
14
         </mat-form-field>
15
      </form>
16
   </mat-card-content>
17
   <mat-card-actions>
18
      <button mat-raised-button (click)="login()" color="primary">LOGIN</button>
19
   </mat-card-actions>
20
</mat-card>

The finished UI will look like this:

Finished login UIFinished login UIFinished login UI

Conclusion

This tutorial has covered most of the Angular Material components needed to make a fully functioning UI. As you have seen, Angular Material is very easy to use since all the components have predefined styles. This means you can quickly build beautiful UIs. Additionally, the Angular Material documentation provides a lot of examples and is easy to follow.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.