自定义有角材料的垫菜单项,以显示尺寸较小的项目(宽度和高度)

时间:2019-03-13 06:42:17

标签: angular angular-material-7

我正在使用角材料组件进行角应用。 在网格中,我具有应支持的内联操作(可以编辑,删除以及更多操作)。

当前,当我将mat-menu组件嵌入到网格中时,项目列表显示了非常大的菜单项。我想自定义菜单项的大小(就宽度和高度而言)。

反正有这样做吗?

当前行为:

enter image description here

期望:

enter image description here

此外,我正在寻找下拉菜单,如下面的屏幕截图所示(菜单下拉项目以绿色边框突出显示。我们可以注意到,下拉菜单恰好在图标下方干净,并形成了一个清晰的框以显示菜单项。)

enter image description here

但是,在默认菜单中,下拉菜单没有明显的高亮或边框。

例如,正如我在下面实现的那样,个人资料图片下方的下拉菜单呈现如下。将此与上一个屏幕截图进行比较,上一个屏幕看起来更加清晰。

enter image description here

代码#

<button mat-icon-button [matMenuTriggerFor]="profileMenu" *ngIf="userIsAuthenticated">
    <!-- <i class="fa fa-user white-icon" aria-hidden="true"></i> -->
    <img class="avatar" src="../../assets/1.jpg">
  </button>

  <mat-menu #profileMenu="matMenu">
    <button mat-menu-item routerLink="/profile"> <i class="fa fa-user" aria-hidden="true"></i> Profile </button>
    <button mat-menu-item (click)="onLogout()"> <i class="fa fa-sign-out" aria-hidden="true"></i> Logout </button>
  </mat-menu>

添加CSS类后的更改

::ng-deep .mat-menu-content {
  width: 200px !important; 
  height: 120px !important; 
}

enter image description here

2 个答案:

答案 0 :(得分:1)

以下CSS将满足您的需求。

::ng-deep .mat-menu-content {
  padding-top: 0px !important;
  padding-bottom: 0px !important;
}

.mat-menu-item{
  line-height:35px;
  height:35px;
}

Stackblitz

https://stackblitz.com/edit/angular-uddgqx?embed=1&file=app/menu-overview-example.css

答案 1 :(得分:0)

尝试在CSS文件中添加以下内容

::ng-deep .mat-menu-content {
  width: 250px !important; // as you wish
  height: 200px !important; // as you wish 
}