我正在尝试覆盖“Angular 材质”中snackbar组件的默认最大宽度。
角材料应用的CSS如下所示:
.mat-snack-bar-container {
border-radius: 2px;
box-sizing: border-box;
display: block;
margin: 24px;
max-width: 568px;
min-width: 288px;
padding: 14px 24px;
transform: translateY(100%) translateY(24px);
}
我尝试过在style.css文件中使用相同的样式进行覆盖,但此样式被默认样式覆盖。
.mat-snack-bar-container {
max-width: 800px;
}
我已经找到了similar question的答案,但我知道这个问题的答案现在已经过时了(/deep/ is deprecated)。
对此是否有最佳做法解决方案?
9条答案
按热度按时间qvk1mo1f1#
要正确执行此操作,需要在组件上将“视图封装”设置为“无”:
然后在你的组件css中,你可以这样做:
来自官方文件:
View Encapsulation = None意味着Angular不封装视图。Angular将CSS添加到全局样式中。前面讨论的作用域规则、隔离和保护不适用。这本质上与将组件的样式粘贴到HTML中相同。
ax6ht2ek2#
把css放到你的
styles.scss
或styles.css
里注意:确保您已为每个样式设置了
!important
,如果没有它,样式将无法工作。以
component.ts
为单位9nvpjoqh3#
验证**@Angular /材料v7.0.x**:
CSS**!important修饰符**就可以完成这个任务。
输入src/styles.scss(应用程序的全局css):
我们还调整了它的字体:
yqhsw0fo4#
截至2019年6月30日,使用Angular Material 8.0.1和Angular 8.0.3,以下SCSS和打字脚本似乎可以覆盖Angular Material snackbar中操作按钮的颜色***,而无需使用!important***:
styles.scss(不是持续时间过长,这让我可以在样式消失之前检查它):
app.module.ts:
s3fp2yjn5#
我记得在一个项目中与网页设计者一起工作,他们有一个钱罐,如果开发者使用!important语句,他们就必须在里面放一个硬币。)
其他解决方案对我不起作用,除非我设置.cdk-overlay-pane(使用材质11):
46scxncf6#
冤家路窄。
这里有一个stackblick
g2ieeal77#
Angular 10且无特殊技巧:
1.在打开小吃店时使用panelClass,例如:
持续时间:0仅用于调试。
1.将此内容写入您的style.css
由于css的特殊性,这将是最特殊的规则。
0mkxixxg8#
使用
vw
对我来说很有效,无论是在大屏幕上还是在小屏幕上gev0vcfq9#
不确定Material是什么时候引入的(从这个线程的所有答案来看,这一定是一个新事物),但是现在可以通过在_snackBar.open()中传递一个参数来覆盖mat-snack-bar-container的样式,如下所示:
component.ts
component.scss
恐怕你还是得用ng-deep和!important;但至少不再需要执行ViewEncapsulationNone。