40

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

Vega
  • 27,856
  • 27
  • 95
  • 103
Yong
  • 1,107
  • 1
  • 12
  • 21

6 Answers6

77

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

Vega
  • 27,856
  • 27
  • 95
  • 103
13

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;
}
FAISAL
  • 33,618
  • 10
  • 97
  • 105
  • 1
    :ng-deep is not depricated! it's /deep/ only. encapsulation may affect other classes – Vega Sep 11 '17 at 12:56
  • 3
    Please read the 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).` – FAISAL Sep 11 '17 at 12:58
  • @Vega , I am not saying that your answer doesn't work, the OP may face an issue in future once `::ng-deep` is dropped completely. – FAISAL Sep 11 '17 at 13:00
  • 2
    Thanks for the solution! I wanted to add that if you want to center only one table header, then you can create your own custom class with name like `mat-sort-header-container-centered` with the outlined above css and assign it to your cell like this `` – Deniss M. Oct 07 '17 at 09:10
  • 1
    @DenissM. yes exactly :) I do the same and avoid using `::ng-deep`. – FAISAL Oct 07 '17 at 09:12
  • 1
    ::ng-deep is easier to use, and is it really depreciated? – Debadatta Jun 04 '19 at 12:35
  • I _think_ ::ng-deep is going to stay deprecated forever, and when it will be dropped an alternative will be provided. Until then, it's going to stay. It's been deprecated 3 versions ago and still going strong. – Alberto Chiesa Nov 22 '19 at 11:58
  • Avoid using `ViewEncapsulation.None` at all costs unless you know exactly what you're doing. – Dane Brouwer Apr 23 '21 at 13:52
8
.mat-header-cell {    text-align: center;    }
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
dayderluv
  • 81
  • 1
  • 2
4

I think the given solutions to the problem are too strict in their approach, i had a similar issue where i needed to change some css properties of mat-sort-header-container , but dynamically.

I did something like Vega's answer but minutely different on how styles are taken :

::ng-deep .mat-sort-header-container{
    justify-content: inherit;
    /* other inheritable properties */
}

which opens some properties for its parent to style mat-header-cell which is in your html code so whatever style you provide in mat-header-cell and in the ng-deep which is inheriting from its parent then only those styles and none other than that would go down that hierarchy .

Aakash Uniyal
  • 1,519
  • 4
  • 17
  • 32
4

If you don't want all header cells to align-center you can combine two classes:

.mat-header-cell.text-center { text-align: center; }

Then on the html:

<th mat-header-cell *matHeaderCellDef class="text-center">Actions</th>
guizo
  • 2,594
  • 19
  • 26
0

If you wanted to align both the header and row cell content, on a column-by-column basis (allowing for differing alignments between columns), you could do something like this:

<mat-table>
    <ng-container matColumnDef="myColumn">
      <mat-header-cell *matHeaderCellDef> My Column </mat-header-cell>
      <mat-cell *matCellDef="let element"> {{element.myField}} </mat-cell>
    </ng-container>
...
.mat-column-myColumn{
  display: flex !important;
  justify-content: flex-start !important; /* Left aligned*/
}

The alignment is applied by the name specified in matColumnDef.

Chris Peacock
  • 4,107
  • 3
  • 26
  • 24