Update: See pistol-pete's answer above. It's the same, essentially.
For those coming in later, There's another way to do this. Do not use ng-deep, in its various forms, as it's deprecated. Breaking encapsulation is not recommended and goes against Angular's design practice. The solution is global CSS, but with a specific selector.
mat-form-field
is part of Angular Material. It has its own encapsulation. Global styles, not component styles, can override this. However, you may not want to override all of these elements, maybe just one or two. I solved this by using a unique class to isolate the elements I wanted to change, then adding .mat-form-field-wrapper
to target the element with the padding. Here's an example:
<mat-form-field class="pb-0">
<mat-label>Not so hacky</mat-label>
<imput matInput formControlName="notHacky" />
</mat-form-field>
Then with the pb-0
class, or whatever you want to call it, target in a global CSS space. In Angular, that's styles.css
. With Sass, you'll likely have other global files you're importing into the styles file. Add this:
/* sass */
mat-form-field {
&.pb-0 {
.mat-form-field-wrapper {
padding-bottom: 0;
}
}
}
/* plain css */
mat-form-field.pb-0 .mat-form-field-wrapper
{
padding-bottom: 0;
}
You can repeat this for any component from any framework since global styles will be applied everywhere. I would recommend creating a unique file to hold all of these overrides.