Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Angular Material style in Angular component

I am using Material 2 <md-input-container> in my Angular component. I want to override one of the classes e.g. .mat-input-wrapper defined in Angular Material. But I want to override only in that component and the override should not effect other components in the page.

Here is the screenshot of the rendered element:Rendered md-input-container

like image 748
nullDev Avatar asked May 20 '17 18:05

nullDev


People also ask

How do you override a material style?

To override, take the selector by inspecting the HTML element in your browser, and override the property you want. If it still fails, you can use ! important to force the style to be applied.

How do you override styles in material UI?

If you want to override a component's styles using custom classes, you can use the className prop, available on each component.

Can you style angular material?

While Angular Material does not support defining custom styles or CSS overrides on components' internal elements, you might choose to do this anyway. There are three points to consider while customizing styles for Angular Material components: view encapsulation, CSS specificity, and rendering location.


1 Answers

The problem with ::ng-deep is, it will apply the style across all the pages and all the components. Meaning its a dirty approach. A better way to do this, is to wrap with a custom class and style the element in your styles.scss/styles.css

Example: As asked in the question. To override a .mat-input-wrapper, wrap your element with your custom class like:

<md-input-container class='big-input'>. Then in

styles.scss:

.big-input .mat-input-wrapper {
   height : 200px;
}

add !important if necessary.

like image 68
Deb Avatar answered Sep 18 '22 15:09

Deb