简体   繁体   English

如何使用全局styles.css 覆盖Angular 材质样式,同时避免!important?

[英]How to override Angular material styles with global styles.css while avoiding !important?

In my angular 7 application, I am using material design.在我的 angular 7 应用程序中,我使用的是材料设计。 In my global styles.css file, I have some styling for material components, namely for mat icons.在我的全局 style.css 文件中,我有一些材质组件的样式,即垫子图标。 However, my settings are overridden by material's own style.但是,我的设置被材质自己的样式覆盖。

Using !important obviously works, but I think there is a more natural way, I just can't figure it out.使用 !important 显然有效,但我认为有一种更自然的方法,我就是想不通。 In chrome dev tools, my CSS class selector simply appears below that of material design selector, and I think this means that it was simply applied sooner, and thus material style takes precedence, since they are both class selectors and should have the same priority (that is my understanding, anyway).在 chrome 开发工具中,我的 CSS 类选择器只是出现在材料设计选择器的下方,我认为这意味着它被更早地应用,因此材料样式优先,因为它们都是类选择器并且应该具有相同的优先级(无论如何,这是我的理解)。

Relevant entry from styles.css:来自styles.css的相关条目:

.icon {
  display: inline-block;
  height: 30px;
  width: 30px;
  margin: 0 auto;
  text-align: center;
  vertical-align: middle;
}

How it appears in chrome dev tools:它在 chrome 开发工具中的显示方式:

.mat-icon {
    background-repeat: no-repeat;
    display: inline-block;
    fill: currentColor;
    height: 24px;
    width: 24px;
}

.icon {
    height: 30px;
    width: 30px;
    margin: 0 auto;
    text-align: center;
    vertical-align: middle;
}

Width and height are simply crossed out in my .icon style.宽度和高度在我的 .icon 样式中被简单地划掉了。

How can I make my style sheet be prioritized?如何让我的样式表优先? Can I somehow specify the order in which stylesheets load, or explicitly tell angular that I want my styles take over material styles?我能否以某种方式指定样式表加载的顺序,或者明确告诉 angular 我希望我的样式接管材料样式?

EDIT: added HTML where mat icons are:编辑:添加了 HTML,其中垫子图标是:

<mat-toolbar color="primary">
  <div fxFlex fxLayout>
    <mat-toolbar-row fxFlex fxLayout fxLayoutGap="20px" class="navigation-items icon-group">
      <span>
        <a routerLink="/">
          <mat-icon class="icon">home</mat-icon>
          <span class="label">Home</span>
        </a>
      </span>
      <span>
        <a routerLink="/product">
          <mat-icon class="icon">bubble_chart</mat-icon>
          <span class="label">Product</span>
        </a>
      </span>
      <span>
        <a routerLink>
          <mat-icon class="icon">widgets</mat-icon>
          <span class="label">Expedition</span>
        </a>
      </span>
      <span class="spacer"></span>
      <span>
        <a routerLink (click)="logout()">
          <mat-icon class="icon">input</mat-icon>
          <span class="label">LogOut</span>
        </a>
      </span>
    </mat-toolbar-row>
  </div>
</mat-toolbar>

It is a specificity problem.这是一个特异性问题。 You can modify the mat-icon class directly, use id which has higher specificity or use combined selector like您可以直接修改 mat-icon 类,使用具有更高特异性的 id 或使用组合选择器,例如

.icon-group > .icon

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

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