My mat-table has headers and cells that are center-aligned using specific CSS:
.center-align.mat-header-cell {
display: flex !important;
justify-content: center !important;
padding-left: 18px !important;
}
.center-align.mat-footer-cell {
display: flex !important;
justify-content: center !important;
}
.center-align.mat-cell {
display: flex !important;
justify-content: center !important;
}
.left-align.mat-header-cell {
padding-right: 18px !important;
}
You can take a look at how it looks here: My Table
If you'd like to see the code, check it out on stackblitz.
I'm facing an issue where one of the column headers ("Amount 9 Column Name") is getting wrapped. I have plenty of room to adjust other columns slightly to ensure this doesn't happen.
I've come across suggestions to manually set column widths like:
.mat-column-position {
flex: 0 0 100px;
}
However, I prefer not to go through every table and adjust multiple columns. Is there a way for cells to automatically resize so that header wrapping is avoided?
UPDATE: I still haven't found a solution to my problem. By updating the HTML and CSS as shown below, I managed to fit the content within the columns, but the centering styling no longer applies.
<div class="mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header class="left-align"> Name </th>
<td mat-cell *matCellDef="let element" class="left-align"> {{element.name}} </td>
</ng-container>
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef mat-sort-header class="center-align"> Position </th>
<td mat-cell *matCellDef="let element" class="center-align"> {{element.position}} </td>
</ng-container>
...
</table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
</div>
table{
width:100%;
}
.mat-header-row {
background-color: #3F51B5;
color: white;
font-weight: bold;
}
.mat-header-cell {
color: white;
font-weight: bold;
border-right: solid 0.5px white;
}
.mat-row:nth-child(odd) {
background-color: white;
}
...
Although the columns now fit the content, the alignment styles applied earlier are no longer effective.