代码之家  ›  专栏  ›  技术社区  ›  Sknecht

右对齐角度材质表中的一个mat标题单元格

  •  0
  • Sknecht  · 技术社区  · 7 年前

    .header-align-right {
        display: flex;
        justify-content: flex-end;
      }
    

    并将其添加到 mat-header-cell . 这使文本对齐,但也给元素添加了奇怪的间距,使其无法与其余标题对齐。也试过没有 display:flex

    <ng-container matColumnDef="Number">
            <th mat-header-cell *matHeaderCellDef mat-sort-header class="header-align-right">Number</th>
            <td mat-cell *matCellDef="let row" align="right">{{row.Number}}</td>
            <td mat-footer-cell *matFooterCellDef></td>
    </ng-conIainer>
    

    如您所见,我右对齐单元格的内容,我也希望对齐标题。

    1 回复  |  直到 7 年前
        1
  •  10
  •   umutesen    7 年前

    Live demo

    .header-align-right{
      display: flex;
      padding: 21px 0;
      justify-content: flex-end;
    }
    
        2
  •  14
  •   bts    6 年前

    使用时,默认情况下通过flexbox完成单元格对齐 <mat-header-cell> <mat-cell> ,但是 <th mat-header-cell> <td mat-cell> . 当使用table元素时,对齐是通过 text-align th td 有可能与桌子的其他部分失去垂直对齐。

    我发现 text-align: right 和设置 justify-content: flex-end 效果最好。这样,元素 display:table-cell 使用 ,和元素 display:flex 使用 justify-content

    例如:

    .header-align-right {
      text-align: right;
      justify-content: flex-end;
    }
    

    要使箭头显示在标题之前,应该使用 arrowPosition

    另外,我建议使用auto-added column类( .mat-column- +的大小写敏感值 matColumnDef )而不是添加 class="header-align-right"

    .mat-column-Number {
      text-align: right;
      justify-content: flex-end;
    }
    
    <ng-container matColumnDef="Number">
      <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before">Number</th>
      <td mat-cell *matCellDef="let row">{{row.Number}}</td>
      <td mat-footer-cell *matFooterCellDef></td>
    </ng-conIainer>
    
        3
  •  4
  •   seawave_23    6 年前

    :host ::ng-deep .mat-sort-header-container { 
        display: flex;  
        justify-content: center; 
    }
    
    th.mat-header-cell, td.mat-cell { 
        text-align: center; 
    }