繁体   English   中英

如何在同一行上对齐“材质”图标和标题文本?

[英]How can I align a Material icon & header text on the same line?

我正在制作Angular Material网页。 我在<mat-table>的标题文本旁边添加了<mat-icon> <mat-table> 添加了垫图标后,图标和文本未完全对齐。 图标没有垂直居中,看起来有点太高。 标题文本与其他列文本不对齐,看起来有点低。

我得到的最接近的是我在带有文本的标签内移动了mat-icon的情况。

这是我的代码

  <table mat-table [dataSource]="dataSource"> <ng-container matColumnDef="column1"> <th mat-header-cell *matHeaderCellDef style="text-align: center !important" [attr.colspan]="2"><h2>Column 1</h2></th> </ng-container> <ng-container matColumnDef="column2"> <th *matHeaderCellDef style="text-align: center !important" [attr.colspan]="2"> <h2><mat-icon class="pointer" style="padding-bottom: 0px; padding-right: 1px; cursor: pointer;" >arrow_left</mat-icon>Column 2</h2> </th> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedGroupColumns; sticky: true"></tr> <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr> <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr> </table > 

您对细节的关注让人想起我在工作的客户;

为了做到这一点,我们使用position:absolute ,但将其包含在带有position:relative的div内,这样我们就不会脱离网格

相关的CSS

.myCustomHeading{position:relative;padding-top:4px;}
.myIcon{position:relative;}
.mySpan{position:absolute;padding-top:2px;}

相关的HTML

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

  <!-- Position Column -->
  <ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> <h2>No.</h2> </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} </td>
  </ng-container>

  <!-- Name Column -->
  <ng-container matColumnDef="name">
    <th mat-header-cell *matHeaderCellDef>
      <div class='myCustomHeading'>  
        <h2><mat-icon class="pointer myIcon" >arrow_left</mat-icon>
        <span class="mySpan">Column 2</span></h2>
      </div>
    </th>
    <td mat-cell *matCellDef="let element"> {{element.name}} </td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
  <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>

完整的工作可在这里进行闪电战

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM