简体   繁体   English

角度材质中垫菜单的自定义样式背景颜色

[英]Custom Styling Background Color for Mat-Menu in Angular Material

I am trying to override some Material2 styling in my Angular 2 app and so far I'm not able to get it to work.我试图在我的 Angular 2 应用程序中覆盖一些 Material2 样式,但到目前为止我无法让它工作。 Specifically, I want to override the background color for the md-menu.具体来说,我想覆盖 md 菜单的背景颜色。 This is what my html looks like:这是我的 html 的样子:

<md-menu #menu="mdMenu" [overlapTrigger]="false" class="sub-drop-bg">
  <button md-menu-item routerLink="option-A" class="sub-drop-item">Option A</button>
...
</md-menu>

And this is what I've tried adding in my LESS/CSS:这就是我尝试在 LESS/CSS 中添加的内容:

md-menu.sub-drop-bg /deep/ {
  background-color: #555 !important;
}

So far this seems to have no effect.到目前为止,这似乎没有效果。 The default #fff background-color still shows up for the md-menu background.默认的#fff背景色仍然显示在 md 菜单背景中。

As additional info, when I hover over and inspect element, I see the default mat-menu-content class as being styled with the white background, like this:作为附加信息,当我将鼠标悬停在元素上并检查元素时,我看到默认的mat-menu-content类被设置为白色背景,如下所示:

.mat-menu-content {
    background: #fff;
}

I've tried adding that class to my component CSS, and using it to override the background-color, also to no avail.我已经尝试将该类添加到我的组件 CSS 中,并使用它来覆盖背景颜色,但也无济于事。 If I "de-select" that color for that class in my browser console, the background color goes away (becomes transparent).如果我在浏览器控制台中为该类“取消选择”该颜色,背景颜色就会消失(变为透明)。 But, as I say, adding that class and overriding the color in my CSS doesn't override the white color when I reload.但是,正如我所说,在我重新加载时,添加该类并覆盖 CSS 中的颜色不会覆盖白色。

How can I accomplish this with CSS alone?如何仅使用 CSS 来完成此操作?

You can also use this:你也可以使用这个:

::ng-deep .mat-menu-content{
  background-color:red;
}

demo演示

If I am not wrong, the following css should work for you:如果我没有错,下面的 css 应该适合你:

/deep/ .mat-menu-content {
  background-color: #555 !important;
}

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

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