Components: Angular Material Table cannot center columns

Created on 12 Jan 2019  ·  7Comments  ·  Source: angular/components

Description

Bug, feature request, or proposal:

I am using Angular Material Table and I am trying to center all the columns
By these images I mean what I am trying to achieve and that is merely a centering of the columns. I have given a link to a StackBlitz demo, which is the one from the Offical Docs.
It would be more than enough if you manage to center the columns.

I have tried

display: flex;
justify-content: center;
, but it worked ifff I used the `` tags, but as I want a **better horizontal scrolling**, but I had to move to ``, which I like more btw and `` doesn't support horizontal scrolling well. When I apply the aforementioned CSS everything breaks. If I try to do it the old school way with `text-align: center` The headers are misaligned and the columns are as well due to paddings inside the cells. If you have any ideas, please share them. #### What is the expected behavior? All the columns are centered. #### What is the current behavior? Nothing is centered ## 🔬 Minimal Reproduction StackBlitz starter: https://stackblitz.com/angular/ndjvyykgoje?file=app%2Ftable-basic-example.ts ## 🌍 Your Environment **Angular Version:**


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 7.1.3
Node: 10.14.2
OS: linux x64
Angular: 7.1.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.3
@angular-devkit/build-angular     0.11.3
@angular-devkit/build-optimizer   0.11.3
@angular-devkit/build-webpack     0.11.3
@angular-devkit/core              7.1.3
@angular-devkit/schematics        7.1.3
@angular/cdk                      7.2.0
@angular/cli                      7.1.3
@angular/flex-layout              7.0.0-beta.22
@angular/material                 7.2.0
@ngtools/webpack                  7.1.3
@schematics/angular               7.1.3
@schematics/update                0.11.3
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.23.1

Most helpful comment

Seems like you may not be centering the sorted headers. One way to do this would be to add this styling:

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

If you need more help, I suggest putting together a full reproduction in Stackblitz that others can help you debug and style. I'll close this since its not an issue, but more of a troubleshooting question

All 7 comments

I got it working in StackBlitz using the example in the Material Table Overview using this CSS:

table {
text-align: center !important;
width: 100%;
}

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


But I still can't make it work in my project.

It only centers the first header cell. Even in chrome dev tools, when I turn off every style having to do with text alignment - it still doesn't center my headers.

Also when I remove the sort header - it starts working.
The sort header hinders the alignment of text

You should omit td and tr, and use 'mat-table' instead of 'table mat-table'. I've provided a Stackblitz where the columns are centered: https://stackblitz.com/edit/mat-table-centered-columns

I edited my question (The tags didn't show up before). I went on and used <table mat-table>, because it provides support for horisontal scrolling. I couldn't find anything about centering <table mat-table>, nor did I find about horisontal scrolling on <mat-table>.
I cannot center the columns, because of the mat-sort-header.

I edited the stackblitz to include horizontal scrolling. The thing is, when using flex, it will use the size available. That means, if you want it to 'overflow', you will have to set a specific size for the columns, either in % or px. For the sort headers, you can just offset the table header with the same size that the sort-button and padding occupies.

I really appreciate your StackBlitz example, but why does every example in the documentation use <table mat-table> as opposed to <mat-table>. Also, you are breaking some styles for the borders and then fixing them manually with CSS.
For example this answer from StackOverflow also says that you should instead use <mat-table>. I personally find it really frustrating and confusing. I kind of got it working using the css from your example (the overflow sort of broke my toolbar when the table was , but when I wanted to add sticky headers for instance - I had to do some manual css (which couldn't work out for me) instead of just sticky: true and sticky, which are available for <table mat-table>.
In the end I think that they didn't bother to make it easy, because maybe centering columns is a bad practice and should be avoided.
Thanks for your time buddy!

Seems like you may not be centering the sorted headers. One way to do this would be to add this styling:

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

If you need more help, I suggest putting together a full reproduction in Stackblitz that others can help you debug and style. I'll close this since its not an issue, but more of a troubleshooting question

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings

Related issues

constantinlucian picture constantinlucian  ·  3Comments

LoganDupont picture LoganDupont  ·  3Comments

shlomiassaf picture shlomiassaf  ·  3Comments

MurhafSousli picture MurhafSousli  ·  3Comments

jelbourn picture jelbourn  ·  3Comments