我正在处理 Angular Material 网页。
我添加了一个 <mat-icon>
在 <mat-table>
中的标题文本旁边.添加垫子图标后,图标和文本没有完全对齐。该图标没有垂直居中,看起来有点太高了。标题文本未与其他列文本对齐,看起来有点低。
我得到的最接近的是当我用文本移动标签内的垫子图标时。
这是我的代码
<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>
关于html - 如何将 Material 图标和标题文本对齐在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705672/