Skip to content
Related Articles

Related Articles

How to use matTooltip in Angular Material ?
  • Last Updated : 08 Feb, 2021

Angular Material is a UI component library developed by 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 done it you can enter the below command and can download it. matTooltip is used when certain information is to be displayed when a user hovers on a button.

Installation syntax:

ng add @angular/material

Approach:

  • First, install the angular material using the above-mentioned command.
  • After completing the installation, Import ‘MatTooltipModule’ from ‘@angular/material/tooltip’ in the app.module.ts file.
  • We need to use ‘matTooltip’ property to display the text we want to render.
  • For showing the position we need to use the ‘matTooltipPosition’ property.
  • We can also customize the position of the tooltip regarding where to display like using the following names as “above”, “below”, “left”, “right” etc.
  • Once done with the above steps then serve or start the project.

Project Structure: It will look like the following.



Code Implementation:

app.module.ts:

Javascript




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

app.component.html:

HTML




<br>
<button mat-raised-button matTooltip=
    "Hover to display information on below."
        matTooltipPosition="below">
    Hover to show information
</button>
  
<br><br><br><br><br>
  
<button mat-raised-button matTooltip=
    "Hover to display information on right."
        matTooltipPosition="right">
    Hover to show information
</button>

Output:


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :