html - 如何将 Material 图标和标题文本对齐在同一行?

标签 html css angular typescript angular-material

我正在处理 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>

完成working stackblitz available here

关于html - 如何将 Material 图标和标题文本对齐在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705672/

相关文章:

javascript - 根据 iframe 的内容动态调整其大小

html - 将 margin 应用于 flex 元素

javascript - CSS 溢出 : scroll; --scrollbar is only visible on hover/scrolling

Angular 4 内化

angular - Angular 项目中的 [number, number] 类型不存在 D3 属性 X 和属性 Y

html - iframe 显示空白

html - 工作项 HTMLFieldControl 内容

php - 两个不同的单选按钮与一种表单并使用 php 发布

html - Div 在 Chrome 和 Opera 中不可见

html - 是否可以为 <router-outlet> 中的内容定义默认包装器?