Overriding Material2 Style with Custom CSS

元气小坏坏 提交于 2019-12-09 21:44:49

问题


I have Datepicker element in Angular. It is standard component from Material Design.

When I try to set own styles to this element, it does not work with property !important.

How to fix it, and how to change the default component's styles?

Example of code:

.mat-datepicker-toggle {
  margin-right: 10px !important;
}

回答1:


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

@Component({
    templateUrl: './my.component.html' ,
    styleUrls: ['./my.component.scss'], //or .css, depending what you use
    encapsulation: ViewEncapsulation.None,
})

Then in your component css you can do exactly what you tried:

.mat-datepicker-toggle {
  margin-right: 10px !important;
}



回答2:


You need to use ::ng-deep. In your component css, set your style to the following:

::ng-deep .mat-datepicker-toggle {
  margin-right: 10px !important;
}


来源:https://stackoverflow.com/questions/45614200/overriding-material2-style-with-custom-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!