我使用Angular Material Select具有这种html结构
<mat-form-field class="year-drpdwn-vacay-stock">
<mat-select placeholder="Year">
<mat-option>None</mat-option>
<mat-option *ngFor="let yr of year" [value]="yr">{{yr}}</mat-option>
</mat-select>
</mat-form-field>
我放置了一个类,因为我也在其他组件中使用了 Select。我尝试添加
::ng-deep .mat-select-panel {
margin-top: 20%;
}
在 CSS 中自定义,但问题是,包含另一个选择框的其他组件也会受到影响(继承 margin-top CSS)
目前我有这个 CSS
.year-drpdwn-vacay-stock mat-select.mat-select :host ::ng-deep .mat-select-panel {
margin-top: 20%;
}
但它仍然不起作用。
更新
目标:我希望选项面板在打开选择框时稍微向下移动,这就是为什么我希望 .mat-select-panel
的上边距为 20%
原文由 Char 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用
cdk-overlay-container
作为基础并像这样转到选项窗格,mat-select 使用绝对位置,其中没有任何子项。所以你需要将你的风格应用到cdk-overlay-container
这是 演示
编辑:
好的,所以您与可以添加的其他元素存在冲突问题
和
像这样改变你的班级
演示