Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize Angular Material 2 Tooltip styles

In AngularJS is possible to style tooltips in CSS using the selector .md-tooltip

What is the way to have custom format tooltips in Angular 4?


EDIT:

I am using Angular 4 & Material2.

An example of how I am using it is:

<span mdTooltip='TEST' mdTooltipPosition='right'>TEST</span>

It shows the tooltip pretty fine, except the fact that I don´t know how to style it.

like image 876
Nizam Avatar asked Aug 19 '17 23:08

Nizam


People also ask

How do you style a material tooltip?

To style React Material UI tooltip, we can use the makeStyles function. We call makeStyles with a function that returns an object that has some classes with some styles. We set the background color and the color in the classes.

What is matTooltip?

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.

How do I change font size on mat tooltip?

You can fix this by adding a . mat-tooltip css declaration in you main styles file and change the font size there. You need to set ! important on the font size otherwise it won't show up.


3 Answers

If you want to customize the css of the tooltip, then you can use ::ng-deep. Add the following styles in your component's styles:

::ng-deep .mat-tooltip {
    /* your own custom styles here */ 
    /* e.g. */
    color: yellow;
}

Another option is to set the View Encapsulation to None in your component:

@Component({ 
    templateUrl: './my.component.html', 
    styleUrls: ['./my.component.css'], 
    encapsulation: ViewEncapsulation.None
})

Then in your component css you dont have to use ::ng-deep.

.mat-tooltip {
    /* your own custom styles here */ 
    /* e.g. */
    color: yellow;
}
like image 191
Faisal Avatar answered Oct 22 '22 03:10

Faisal


You could take a look into the following example angular/material2 Tooltip Demo

Basically you could set up the tooltip as follows (you could define not only the css but also the position, the hide delay, the show delay and if it is disable or not):

<button #tooltip="mdTooltip"
            md-raised-button
            color="primary"
            [mdTooltip]="message"
            [mdTooltipPosition]="position"
            [mdTooltipDisabled]="disabled"
            [mdTooltipShowDelay]="showDelay"
            [mdTooltipHideDelay]="hideDelay"
            [mdTooltipClass]="{'red-tooltip': showExtraClass}">

In your component then

position: TooltipPosition = 'below';
message: string = 'Here is the tooltip';
disabled = false;
showDelay = 0;
hideDelay = 1000;
showExtraClass = true;

And the css as example:

/deep/ .red-tooltip {
  background-color: rgb(255, 128, 128);
  color: black;
}
like image 32
Axel Freiria Avatar answered Oct 22 '22 01:10

Axel Freiria


Simply add a matTooltipClass="red-tooltip" in your input tag may be. And then in styles.css add the definition for this class

<input type="number" matTooltipClass='red-tooltip'/>

.red-tooltip{
       background-color:red;
}
like image 16
sumit Avatar answered Oct 22 '22 02:10

sumit