5

I need to make responsive table even that would render in mobile view without breaking up. In order to make it work, I used standard HTML table link below

<div style="overflow-x:auto;">
  <table>
    ...
  </table>
</div>

Have a look at sample: https://www.w3schools.com/howto/howto_css_table_responsive.asp

It worked great but recently switched to Angular 5 table which does not have API for responsive design. How can I make add overflow-x auto to angular table? The sample angular 5 table:

https://stackblitz.com/edit/angular-material-table-data-source-ibmgv2 Thanks in advance

2
  • What do you mean it's not responsive? In your example, the table will smoothly shrink down to 0 width without ever needing an overflow-x. Even if you add overflow-x:auto, it will always be useless because the table will go all the way down to 0 with without ever overflowing its parent container. If that's not being responsive, I don't know what is :) Feb 1, 2018 at 13:14
  • @JeremyThille Yes I want to make it responsive without shrinking it. Need to keep the defined width such 50px. If total width is lower then show overflow-x auto. How can I acheive that in angular 5 material table? Feb 1, 2018 at 13:22

2 Answers 2

12

As per your demo, just add following CSS to your css file.

.example-container.mat-elevation-z8 {    
  overflow-x: auto;
}
.example-container .mat-table.mat-table {
  min-width: 450px;
}

Hope this will help you.

3
  • 2
    it worked like a charm. Thanks so much. Did not expect such simple solution! Feb 1, 2018 at 14:13
  • Thanks you, for such positive comment.
    – kravisingh
    Feb 2, 2018 at 5:09
  • 1
    This works but does cause issues with using a sticky header row.
    – Kishan
    Dec 7, 2020 at 22:02
1
.table-responsive {
  display: block;
  width: 100%;
  overflow-x: auto;
}

.mat-table {
  width: 100%;
  max-width: 100%;
  margin-bottom: 1rem;
  display: table;
  border-collapse: collapse;
  margin: 0px;
}

.mat-row,
.mat-header-row {
  display: table-row;
}

.mat-cell,
.mat-header-cell {
  word-wrap: initial;
  display: table-cell;
  padding: 0px 5px;
  line-break: unset;
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  vertical-align: middle;
}

check this link it will help you.

Credit Source - https://github.com/angular/components/issues/8680#issuecomment-388358839

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.