For those looking to personalize their Angular material components and add their own styles to the mat-input
placeholder, I have some recommendations for you.
1) You can override the classes in your main style.css (or style.scss, depending on which one you are using). This file should be at the same directory level as your index.html, main.ts, package.json, etc.
.mat-form-field-label {
font-size: 0.8em!important;
}
I've put together a demonstration on this link.
2) Utilize ViewEncapsulation:None
. Keep in mind that this approach is not highly recommended because it eliminates all forms of styling encapsulation within your component, allowing CSS rules to have a global impact.
In your component.ts
, make sure to import ViewEncapsulation
and set it to None
when defining encapsulation.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'input-overview-example',
styleUrls: ['input-overview-example.css'],
templateUrl: 'input-overview-example.html',
encapsulation: ViewEncapsulation.None
})
You can define your CSS styles within your component's css file without using the !important
declaration.
.mat-form-field-label {
font-size: 0.8em;
}
I've created another demo accessible through this link.
3) Apply the :host ::ng-deep
pseudo selectors within the component's css file. This allows you to disable view encapsulation for specific rules. Keep in mind that this method may become deprecated in the future, so use with caution.
In your component's css file,
:host ::ng-deep .mat-form-field-label {
font-size: 0.8em;
}
I've also created another demo available here.