使用 angular material 和 flex css 的垫子菜单样式在 angular 中不起作用

Style for mat-menu not working in angular using angular material and flex css

我正在尝试使用 angular material 和 flex css..

在 angular 2 中创建一个简单的布局

下面是我的代码...

<div  class="background" >

<mat-toolbar fxLayout="row"   fxLayoutAlign="space-between  none" >

<div  fxLayout="column" > 
<p>SPACE STUDY</p>
<h6>Rockefeller FY 2018</h6>

<div  fxShow="true" fxHide.gt-sm="true" fxLayout="column" fxLayoutAlign="start none">

 <h6 [matMenuTriggerFor]="dashboarditems">
  <mat-icon style="transform: scale(1.2);">line_weight</mat-icon> </h6>

  <mat-menu  class="mat-menu-panel"  [overlapTrigger]="true" #dashboarditems="matMenu"><br/><br/>
<a  routerLink='/home' routerLinkActive='active'>
<span matTooltip="HOME"><mat-icon style="color:lightgreen;">home</mat-icon></span></a><br/><br/><br/>

<a >
<span matTooltip="SPACE SURVEY"><mat-icon style="color:deeppink;">explore</mat-icon></span></a><br/><br/><br/>

<a  >
<span matTooltip="SPACE ADMIN"><mat-icon style="color:lightblue;">account_circle</mat-icon></span></a><br/><br/><br/>

<a >
<span matTooltip="REPORTS"><mat-icon style="color: orange;">assignment</mat-icon></span></a><br/><br/><br/>

<a >
<span matTooltip="JOINT USE"><mat-icon style="color:yellow;">supervisor_account</mat-icon></span></a><br/><br/><br/>

<a  >
<span matTooltip="HELP"><mat-icon style="color:red;">help</mat-icon></span></a><br/><br/><br/>
  </mat-menu>
</div>


</div>

<div fxLayout="row"  fxShow="true" fxHide.lt-md="true" >
<button mat-raised-button routerLink='/home' routerLinkActive='active'>
<span matTooltip="HOME"><mat-icon style="color:lightgreen;">home</mat-icon></span></button>

<button mat-raised-button >
<span matTooltip="SPACE SURVEY"><mat-icon style="color:deeppink;">explore</mat-icon></span></button>

<button mat-raised-button >
<span matTooltip="SPACE ADMIN"><mat-icon style="color:lightblue;">account_circle</mat-icon></span></button>

<button mat-raised-button >
<span matTooltip="REPORTS"><mat-icon style="color: orange;">assignment</mat-icon></span></button>

<button mat-raised-button  >
<span matTooltip="JOINT USE"><mat-icon style="color:yellow;">supervisor_account</mat-icon></span></button>

<button mat-raised-button >
<span matTooltip="HELP"><mat-icon style="color:red;">help</mat-icon></span></button>
</div>

</mat-toolbar>

</div>


<style>

.background {
/* Remember to use the other versions for IE 10 and older browsers! */
display: flex;
min-height: 100%;
font-family: 'verdana', sans-serif;
color: #fff;
height:100vh;
background: #222222;
background: #16222A; /* fallback for old browsers */
background: -webkit-linear-gradient(to top, #16222A , #3A6073); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to top, #16222A , #3A6073); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

p{
font-size:20px;
font-weight:bold;
font-family: 'verdana', sans-serif;
margin:3px;
}

h6{
    font-size:12px;
    font-weight:bold;
    font-family: 'verdana', sans-serif;
}

.mat-raised-button{
    text-align:left;
    border-radius:10px;
    font-size:11px;
    font-weight:bold;
    font-family: 'verdana', sans-serif;
    color:white;
    background:transparent;
}

.mat-icon {
    transform: scale(1.5);
}

.matTooltip{
    font-size:11px;
    font-weight:bold;
    font-family: 'verdana', sans-serif;
    }

.mat-menu-panel {
    min-width: 35px;
    max-width: 280px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 48px);
    border-radius: 2px;
    outline: 0;
    background: transparent;
    padding-left: 10px;
}
</style>

这是我的输出

这里我尝试把mat-menu容器的背景色改成透明的,还设置了一些其他的样式。

当我在控制台中更改时,它会反射回来,如下所示

但是当我在我的代码中应用相同的更改时,没有应用样式。

我还尝试在组件中显式添加 class="mat-menu-panel" 以及我在其中添加垫子菜单的 div。

但是仍然没有应用样式..

谁能帮我修改这个样式..

样式未应用,因为 Angular view encapsulation

您可以在这里看到您的 .mat-menu-panel 封装 :

解决方案 - 三个选项:

  • 关闭视图封装:

    encapsulation 组件 属性 设置为 ViewEncapsulation.None,如下所示:

    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        encapsulation: ViewEncapsulation.None, // disable ViewEncapsulation
    })
  • 或者把mat-menu-panel样式放在全局样式上:/styles.scss

  • (将被弃用 "soon")使用 ::ng-deep,如该线程中所述:

关注:

您的某些属性将被默认样式覆盖,因此您必须向每个属性添加 !important使用更具体的选择器.

例如,我将 myMenu class 添加到您的 mat-menu:

然后,将 css 选择器更新为:

.mat-menu-panel.myMenu {
    min-width: 35px;
    max-width: 280px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 48px);
    border-radius: 2px;
    outline: 0;
    background: transparent;
    padding-left: 10px !important;
}