Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material - dynamic style for mat-menu

I would like to apply a dynamic style to the content of mat-menu. I know that I can use panelClass to assign a class, but my class is dynamic.

Angular has a [ngStyle] or just [style.attribute] binding for such cases, but that does not work on the mat-menu (or other overlays), it only works on directly rendered elements.

I am looking for something like panelStyle which would allow me to set the styles dynamically directly on the panel which holds the mat-menu.

Here is a code example, where panelClass allows me to set some css, but only static one and ngStyle is useless.

<mat-menu [ngStyle]="{'background-color': colorVariable }" panelClass="some-static-class-works">

What I am looking for:

<mat-menu [panelStyle]="{'background-color': colorVariable }">
like image 700
hoonzis Avatar asked Dec 31 '25 07:12

hoonzis


1 Answers

You can wrap your menu content in a single DIV and apply style dynamically to that. With background-color, to get it to fill the entire panel you need to adjust margins and padding. For example:

<mat-menu>
  <div [ngStyle]="{'background-color': colorVariable }" class="menu-panel">
    <button mat-menu-item>Item 1</button>
    <button mat-menu-item>Item 2</button>
  </div>
</mat-menu>

.menu-panel {
  margin: -8px 0; 
  padding: 8px 0;
}
like image 109
G. Tranter Avatar answered Jan 02 '26 21:01

G. Tranter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!