Open In App

Angular Material mat-tab-group

Last Updated : 16 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Angular Material is a UI component library that is developed by the Angular team to build design components for desktop and mobile web applications. In order to install it, we need to have angular installed in our project, once you have it you can enter the below command and can download it. mat-tab-group is used mainly for navbar requirements when we have multiple tabs to display.

Installation:

ng add @angular/material

Approach:

  • First, install the angular material using the above-mentioned command.
  • After completing the installation, Import ‘MatTabsModule’ from ‘@angular/material/tabs’ in the app.module.ts file.
  • Then use the <mat-tab-group> tag to group all the items inside this group tag.
  • Inside the <mat-tab-group> tag we need to use <mat-tab> tag for every item.
  • We can also style the tabs basing upon the position like start, center, and end. For this, we need to use a property called mat-align-tabs.
  • If we want to change the theme then we can change it by using the color property. In angular we have 3 themes, they are primary, accent, and warn.
  • Once done with the above steps then serve or start the project.

Example:

app.module.ts




import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule } from '@angular/forms'
import { MatTabsModule } from '@angular/material/tabs'
      
import { AppComponent } from './app.component'
import { BrowserAnimationsModule } from 
'@angular/platform-browser/animations';
  
  
@NgModule({ 
  imports: 
  [ BrowserModule, 
    FormsModule, 
    MatTabsModule,
    BrowserAnimationsModule], 
  declarations: [ AppComponent ], 
  bootstrap: [ AppComponent ] 
}) 
  
export class AppModule { }


app.component.html




<p>
   Implementation of mat-tab-group for 
   center style and for default theme
</p>
  
<mat-tab-group mat-align-tabs="center">
  <mat-tab label="Home">Home Content</mat-tab>
  <mat-tab label="Overview">Overview Content</mat-tab>
</mat-tab-group>
<br>
  
  
<p>
  Implementation of mat-tab-group for center 
  style and for primary theme
</p>
  
<mat-tab-group mat-align-tabs="center" backgroundColor="primary">
  <mat-tab label="Home">Home Content</mat-tab>
  <mat-tab label="Overview">Overview Content</mat-tab>
</mat-tab-group>
<br>
  
  
<p>
  Implementation of mat-tab-group for center 
  style and for accent theme
</p>
  
<mat-tab-group mat-align-tabs="center" backgroundColor="accent">
  <mat-tab label="Home">Home Content</mat-tab>
  <mat-tab label="Overview">Overview Content</mat-tab>
</mat-tab-group>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads