1
votes

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

please access my sample code here.. https://stackblitz.com/edit/angular-mat-formfield-flex-layout-vc3fsj?file=app%2Fapp.component.html

3
still your code is broken please fix thatChellappan வ
Your stackblitz snippet does not runbugs
can you please access this link ...stackblitz.com/edit/…Heena
@Heena not sure how to phrase this differently, that link does not workbugs
Any custom styling to mat-menu is best done in the theme .scss file. In your style.scss file you already put rules for body, try adding the mat-menu styles there. A good resource to check out is this one blog.thoughtram.io/angular/2017/05/23/…arsanyf

3 Answers

3
votes

You can't have styles in your html template. You need to put them in separate stylesheet. Once you've done that, it may not work for styling material components, because of angular's default style encapsulation.

You can

set the styles to global styles.css file

Style.css

.mat-menu-panel {
min-width: 35px;

use the ng-deep combinator

Put your styles in component specific stylesheet

Component.css

:host ::ng-deep  .mat-menu-panel {
min-width: 35px;
/* ...*/

Change the component' s encapsulation

Put your styles in component specific stylesheet and change the component' s code

Component.ts

@Component({
//...
 encapsulation: ViewEncapsulation.None 
 })

With that last one, styles will leak to entire app (like in global style sheet)

1
votes
1
votes

Just use

::ng-deep.mat-menu-panel {}

note that you can handle the following classes .mat-menu-item, .mat.menu-wrapper etc directly Note that you shouldn't use them inside another css class in the componenet.scss