【问题标题】:How can I align a Material icon & header text on the same line?如何在同一行对齐材质图标和标题文本?
【发布时间】:2019-11-04 10:28:55
【问题描述】:

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

我得到的最接近的是当我将带有文本的标签内的 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 >

【问题讨论】:

  • 如果涉及到一张桌子,你应该展示更多。真的,我们需要查看呈现的 HTML 和任何适用的 CSS。

标签: html css angular typescript angular-material


【解决方案1】:

你对细节的关注让我想起了我工作中的一位客户;

为了实现这一点,我们使用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>

完成working stackblitz available here

【讨论】:

    猜你喜欢
    • 2021-07-11
    • 1970-01-01
    • 2017-02-15
    • 2019-08-07
    • 2019-02-16
    • 2019-10-03
    • 1970-01-01
    • 2017-11-28
    • 2018-03-12
    相关资源
    最近更新 更多