Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular material 2 table header center alignment

If md-sort-header is added into md-header-cell in md-table, it is always left-alignment. How to center-align header cells, such "name"?

<md-header-cell *cdkHeaderCellDef md-sort-header style="text-align:center"> 
      Name 
</md-header-cell>

plnkr

like image 369
Yong Avatar asked Sep 11 '17 11:09

Yong


2 Answers

Update for Angular Material 5.x.x, no need for ng-deep:

  mat-header-cell {
   display:flex;
   justify-content:flex-end;
  }

DEMO


md-header-cell get 'translated' to a container with class="mat-sort-header-container". Using that, you set its style with ng-deep. Use flexbox to center its content. Put the following in the components stylesheet:

::ng-deep .mat-sort-header-container {
  display:flex;
  justify-content:center;
}

DEMO

like image 152
Vega Avatar answered Oct 17 '22 13:10

Vega


The accepted answer is correct. However, ::ng-deep is depreciated and maybe dropped in future (official documentation).

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools. As such we plan to drop support in Angular (for all 3 of /deep/, >>> and ::ng-deep). Until then ::ng-deep should be preferred for a broader compatibility with the tools.

The proper way is to use ViewEncapsulation. In your component.ts, add the following:

import { ViewEncapsulation } from '@angular/core';

@Component({
    ....
    encapsulation: ViewEncapsulation.None
})

and override the class in your component.css file:

.mat-sort-header-container {
  display:flex;
  justify-content:center;
}
like image 13
Faisal Avatar answered Oct 17 '22 14:10

Faisal