Is it possible to apply the angular material built-in theme to a specific component only?
I did:
@import "~@angular/material/prebuilt-themes/indigo-pink.css";
In one of my component.scss file and referenced this as the styleUrl inside of component.ts file. But the styles did not apply to my angular material paginator.
Here is what the paginator looks like
As shown, the styles do not apply.
Is it something to do with the fact that I'm importing them in a component specific scss file instead of importing it in angular.json?
it's very easy to create a custom angular material theme. This will allow you to specify primary, accent and warning colors that will be used on Angular Material components. Your custom theme will be a Sass file and in our case, we'll call it theme. scss and place it in our app's /src folder.
You can find the pre-built theme files in the "prebuilt-themes" directory of Angular Material's npm package ( @angular/material/prebuilt-themes ). To include the pre-built theme in your application, add your chosen CSS file to the styles array of your project's angular. json file.
Defining a theme in Angular Material is extremely simple, all we need to do is to create a theme file, select three color palettes for the main theme colors — primary, accent and warn — and we are all set! Feel free to explore all the palettes that are available out of the box.
To do so, open the Settings and navigate to Appearance & Behavior → Material Theme UI → Custom Theme. Once you're done customizing your colors, you'll need to select Custom Theme or Light Custom Theme from the Theme Switcher to see your colors in action. Enjoy!
You have to use the Mixins @angular/material provides instead of using a prebuilt theme.
@import '~@angular/material/theming';
@include mat-core();
// Use the desired palette
$palette: mat-palette($mat-indigo);
// Create the theme
$theme: mat-light-theme($palette, $palette);
// Include component specific mixin
@include mat-dialog-theme($theme);
// Or wrap inside another selector to scope the styles to only one specific component
my-component {
@include mat-dialog-theme($theme);
}
Edit: This code should be in your styles.scss (the global one, not the component specific scss)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With