简体   繁体   English

防止角材料 css 样式传播到其他组件

[英]Prevent angular material css styles from propagating to other components

so I have an override to angular material style in my global scss style like this:所以我在我的全局 scss 样式中覆盖了 angular material 样式,如下所示:

.mat-form-field-infix{
   padding: 0.5em 0 0.5em 0 !important;
}

then I want to apply a different padding to the same element in my component since I need a bit of more space to view more data;然后我想对组件中的同一元素应用不同的填充,因为我需要更多空间来查看更多数据; here is what I do in my component scss style:这是我在组件 scss 样式中所做的:

:host ::ng-deep .mat-form-field-infix{
   padding: 0.2em 0 0.2em 0 !important;
}

but the last style gets propagated to all components once its load.但是最后一个样式在加载后会传播到所有组件。 so the question is how can I prevent this behavior?所以问题是如何防止这种行为?

Note that ::ng-deep is destined to be deprecated: see Special selectors .请注意::ng-deep注定会被弃用:请参阅特殊选择器

You can, however, follow a strategy like Yuriy describes: If you want extra padding for all descendants of my-roomy-component , you can target CSS from your global styles like但是,您可以遵循 Yuriy 描述的策略:如果您想要为my-roomy-component所有后代额外填充,您可以从全局样式中定位 CSS,例如

my-roomy-component .mat-form-field-infix {
  padding: 0.2em 0 0.2em 0 !important;
}

And, again as Yuriy suggests, add more specificity to the selector to help the padding take effect without the !important .并且,正如 Yuriy 建议的那样,为选择器添加更多特异性,以帮助填充在没有!important情况下生效。 Alternatively, the outer component can have its view encapsulation set to None so its styles become global, but scope with CSS selectors again.或者,外部组件可以将其视图封装设置为 None 使其样式变为全局,但再次使用 CSS 选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM