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>
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>
If you don't want all header cells to align-center you can combine two classes:
Then on the html:
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 :
which opens some properties for its parent to style
mat-header-cell
which is in your html code so whatever style you provide inmat-header-cell
and in theng-deep
which is inheriting from its parent then only those styles and none other than that would go down that hierarchy .The accepted answer is correct. However,
::ng-deep
is depreciated and maybe dropped in future (official documentation).The proper way is to use ViewEncapsulation. In your component.ts, add the following:
and override the class in your component.css file:
Update for Angular Material 5.x.x, no need for ng-deep:
DEMO
md-header-cell
get 'translated' to a container with class="mat-sort-header-container". Using that, you set its style withng-deep
. Use flexbox to center its content. Put the following in the components stylesheet:DEMO