Can anyone provide a concise explanation of how to center (or move to the right) Angular Material table header and cell texts? There seems to be a lot about needing /deep/ and other versions, which may or may not be deprecated.
Asked
Active
Viewed 8.7k times
12 Answers
20
If no answer is working above then try this :
table {
width: 100%;
}
mat-header-cell, mat-cell {
justify-content: center;
}
or

Shabbir Ismail
- 278
- 2
- 14
14
use this style with !important
th.mat-header-cell {
text-align: center !important;
}

Amir Azizkhani
- 1,662
- 17
- 30
10
This exactly works for mat-table.
td, th {
text-align: center !important;
vertical-align: middle !important;
}
.mat-sort-header-container {
justify-content: center !important;
}
6
you can achieve that easily if you use this layout of table
<table mat-table [dataSource]="dataSource" style="width: 100%;">
<!-- identification Column -->
<ng-container matColumnDef="identification">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let element"> {{element.identification}} </td>
</ng-container>
<!-- value Column -->
<ng-container matColumnDef="total">
<th mat-header-cell *matHeaderCellDef> Value </th>
<td mat-cell *matCellDef="let element"> {{ element.value | currency: 'ILS' }} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
and put this in where-the-table-is.component.css
td.mat-cell, th.mat-header-cell {
text-align: center;
justify-content:center;
}

user12163165
- 555
- 8
- 12
5
Try this, It's work for me.
:host ::ng-deep .mat-sort-header-container {
display: flex;
justify-content: center;
}

lahiru dilshan
- 690
- 9
- 13
3
You can use text-align: center
Here is an example StackBlitz tweaked from the Material docs

nick
- 1,880
- 17
- 27
3
If you are using a sorted header you need to provide
table {
width: 100%;
text-align: center !important;
}
.mat-sort-header-container{
justify-content: center;
}
.mat-header-cell {
text-align: center !important;
}

Aswin KV
- 1,710
- 2
- 12
- 23
-
It appears only the CSS rule on .mat-sort-header-container is absolutely necessary. FYI, define in the top level CSS, not a component's CSS file. – schmiddy98 Aug 10 '22 at 19:18
2
This works for me
:host ::ng-deep .mat-sort-header-container {
display: flex;
justify-content: center;
}
table {
text-align: center !important;
width: 100%;
}

San Jaisy
- 15,327
- 34
- 171
- 290
-
This is the only one that worked for me - I dont understand it - not come across :host ::ng-deep – Richard Turner Jul 20 '22 at 09:55
2
Try using class text-center
<td mat-cell *matCellDef="let element" class="text-center"> {{element.multiplicador}} </td>

Lucas Borinato
- 21
- 1
1
You can create css class
.centre {
text-align: center
}
and add it to your html
<th mat-header-cell *matHeaderCellDef class="centre"> Symbol </th>
just like here: https://stackblitz.com/edit/angular-qfav5c?file=app%2Ftable-basic-example.css

sylwester
- 16,498
- 1
- 25
- 33
-
1Please mention the angular version? The stackblitz works nicely with a "td" element that has a mat-cell attribute. In Angular 8 I'm using a "mat-cell" element directly, and I cannot make this solution work. Tried putting text-align:center into one of the dynamically created classes that the material table recognizes, also no luck. – chrisinmtown Oct 22 '19 at 21:01
-
@chrisinmtown as you can see this question is from 2018 so it was angular 1 – sylwester Mar 20 '20 at 18:50
-
@chrisinmtown : for recent Angular (8/9), you have to tell your component to use the "standard DOM" instead of the "shadow DOM" using `encapsulation: ViewEncapsulation.None` in the `@Component` properties. – Julien Kronegg Apr 17 '20 at 13:29
0
This works for me:
td, th {
text-align: center !important;
vertical-align: middle !important;
}
:host ::ng-deep .mat-sort-header-container {
display: flex;
justify-content: center;
}

Bhargavi
- 233
- 3
- 11
0
This worked for me:
.mat-header-cell {
flex-direction: column;
justify-content: center;
}
.mat-cell {
text-align: center;
justify-content:center;
}