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

Heena picture Heena · Apr 25, 2018 · Viewed 7.2k times · Source

I am trying to create a simple layout in angular 2 using angular material and flex css..

Below is my code...

<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>

This is my output

enter image description here

Here I am trying to change the background color for the mat-menu container to transparent and also set some other styles.

When i change in console its getting reflected back as shown below

enter image description here

But when i applied the same changes in my code , the styles are not being applied.

I also tries explicitly adding class="mat-menu-panel" in the component and the div in which i added the mat-menu.

But still the styles are not being applied..

can anybody please help me to fix this styling..

Answer

jurl picture jurl · Jun 13, 2018

The style is not applied because of Angular view encapsulation.

You can see here that your .mat-menu-panel got encapsulated:

mat-menu-panel after view encapsulation

Solutions - Three options:

  • turn off the view encapsulation:

    setting the encapsulation component property to ViewEncapsulation.None like so:

    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        encapsulation: ViewEncapsulation.None, // disable ViewEncapsulation
    })

Pay attention:

Some of your properties are getting overridden by the default style so you will have to add !important to each of them or use more specific selector.

For example I added myMenu class into your mat-menu:

.myMenu class

and then, update the css selector to:

.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;
}