【问题标题】:How to change angular material option style?如何更改角度材质选项样式?
【发布时间】:2019-12-31 18:21:53
【问题描述】:

我使用以下代码:

::ng-deep{
 .mat-select-panel{
   border-radius: 10px;
   border: 1px solid gray;
   position:absolute;
   top: 50px;
 }
}

我希望它看起来这样:

但还是这样:

而且选择

的地方很难看

【问题讨论】:

  • 你看过我分享的最新演示了吗?

标签: angular sass angular-material


【解决方案1】:

我只是在 styles.scss

中添加 panelClass 来选择标记和样式这个类

Html:

<mat-form-field>
  <mat-select disableOptionCentering panelClass="dropDown-seasons-panel">
      <mat-option *ngFor="let season of seasons" [value]="season.value">
         {{season.viewValue}}
      </mat-option>
  </mat-select>
 </mat-form-field>

styles.scss:

.dropDown-seasons-panel {
  border-radius: 10px !important;
  border: 1px solid gray !important;
  position: absolute !important;
  top: 50px !important;
}

【讨论】:

  • 我想在 mat-select 中而不是在 mat-options 中显示边框半径,那么我该如何实现呢?
  • 你可以为这个类设置样式:".mat-select-panel"
  • 不使用 !important 有更好的解决方案。请参阅此链接,#3“在单独的全局样式中覆盖 AM 样式 - 没有作用域!” betterprogramming.pub/…
【解决方案2】:

请确认,您使用的是 CSS 还是 SCSS。如果您使用 CSS,它将无法正常工作。更改为 SCSS。

我在 mat-select 组件中添加了使用 panelClass 属性的类。通过使用该类,您可以更新 css

如果您使用 SCSS,请在此处查看我的演示 - https://stackblitz.com/edit/angular-jhxdfy-piucvw

同时检查您的全局样式 CSS 文件。也许那个 CSS 可以覆盖这个 CSS。

【讨论】:

  • 我使用 scss。我完全喜欢你的演示,但没有任何反应,但如果我将此代码添加到 style.scss 一切都很好,但这不是解决方案,因为它在我的整个项目中都发生了变化
  • 你能添加你的html部分吗?那我可以建议了。
  • نامفصل: {{food.viewValue}}
  • 另外,我查看了 styles.scss ,没有覆盖的类。
  • 我刚刚更新了链接(stackblitz.com/edit/angular-jhxdfy-piucvw),我在 mat-select 组件中添加了使用 panelClass 属性的类。通过使用该类,您可以更新 css
【解决方案3】:

这可能不是最佳做法,但您可以尝试在 style.csss 中添加它们!important

.html

<mat-select id='myCustomDropdown' placeholder="Favorite food">
    <div class="custom-style">
        <mat-option *ngFor="let food of foods" [value]="food.value">
            {{ food.viewValue }}
        </mat-option>
    </div>
</mat-select>

style.css

.custom-style .mat-option{
    color:red !important
 }

Working Demo

【讨论】:

  • 是的,你可以用custom-panel类将它包装在一个div中,然后使用custom-panel.mat-select-panel这样的选择器
  • 你能帮我做一个演示吗
  • 用 class="seasons-part" 和 style.scss => .seasons-part { .mat-select-panel {border-radius: 10px !重要的;边框:1px 纯灰色!重要;位置:绝对!重要;顶部:50px!重要; } } 但它不起作用
  • 检查工作演示,在 style.css 中,我是 #myCustomDropdown .mat-select-placeholder{ color:red !important }
  • 我的选择标签没问题,但选项标签不起作用
猜你喜欢
  • 2015-12-11
  • 2019-04-05
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 2019-06-23
相关资源
最近更新 更多