What is the best way to make a mat-table resize itself to accommodate headers dynamically?

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.

Answer №1

I finally managed to solve this issue. If anyone else is facing the same problem, you can find the solution here: stackblitz

Essentially, I opted for using table, th, and td instead of mat-table, mat-header-cell, and mat-cell elements in the HTML:

<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-header" > Name </th>
         <td mat-cell *matCellDef="let element" class="left-align-cell"> {{element.name}} </td>
      </ng-container>
      <ng-container matColumnDef="position">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Position </th>
         <td mat-cell *matCellDef="let element"> {{element.position}} </td>
      </ng-container>
      <ng-container matColumnDef="amount">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount</th>
         <td mat-cell *matCellDef="let element"> {{element.amount}} </td>
      </ng-container>
      <ng-container matColumnDef="amount2">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 2</th>
         <td mat-cell *matCellDef="let element"> {{element.amount2}} </td>
      </ng-container>
      <ng-container matColumnDef="amount3">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 3</th>
         <td mat-cell *matCellDef="let element"> {{element.amount3}} </td>
      </ng-container>
      <ng-container matColumnDef="amount4">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 4</th>
         <td mat-cell *matCellDef="let element"> {{element.amount4}} </td>
      </ng-container>
      <ng-container matColumnDef="amount5">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 5</th>
         <td mat-cell *matCellDef="let element"> {{element.amount5}} </td>
      </ng-container>
      <ng-container matColumnDef="amount6">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 6</th>
         <td mat-cell *matCellDef="let element"> {{element.amount6}} </td>
      </ng-container>
      <ng-container matColumnDef="amount7">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 7</th>
         <td mat-cell *matCellDef="let element"> {{element.amount7}} </td>
      </ng-container>
      <ng-container matColumnDef="amount8">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 8</th>
         <td mat-cell *matCellDef="let element"> {{element.amount8}} </td>
      </ng-container>
      <ng-container matColumnDef="amount9">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 9 Column Name</th>
         <td mat-cell *matCellDef="let element"> {{element.amount9}} </td>
      </ng-container>
      <ng-container matColumnDef="amount10">
         <th mat-header-cell *matHeaderCellDef mat-sort-header> Amount 10</th>
         <td mat-cell *matCellDef="let element"> {{element.amount10}} </td>
      </ng-container>
      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;" ></tr>
   </table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator>
  </div>

Change the table-overview-example.css to:

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;
}

.mat-row:nth-child(even) {
  background-color: #D3D3D3;
}
.mat-cell{
  border-right: solid 0.5px black;
}

Update the styles.css with the following:

@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';

    body { 
      font-family: Roboto, Arial, sans-serif;
      margin: 0;
    }

    .basic-container {
      padding: 30px;
    }

    .version-info {
      font-size: 8pt;
      float: right;
    }

    .mat-sort-header-container{
      justify-content: center !important;
       padding-left: 18px !important; /* Allow for sort icon*/
    } 

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

     .left-align-cell.mat-cell{
       text-align: left;
     }

     .left-align-header>.mat-sort-header-container{
      justify-content: flex-start !important;
      padding-left:0px !important;
    } 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to troubleshoot the issues causing my React application to malfunction

Having some trouble with my Spotify project. Every time I search for an artist, I receive an error message saying "illegal redirect_uri." I am also facing a list of coding issues that I am struggling to resolve. Any advice or suggestions would be greatly a ...

Issues with Firefox's flexbox functionality are preventing it from working

Hey there, I've created a bubble menu with a button using code. It functions perfectly on Chrome but seems to have some issues on Mozilla Firefox. You can check it out and give it a try. $(".roundedBallOuter").click(function(e) { $(this).toggleCl ...

How can I achieve a transparent background for a text field in CSS instead of white?

I've come across several solutions, but none have been able to solve my issue so far. The text fields in question look like this: .form-wrapper .field-list .field .field-element{ background-color: rgba(0,0,0,0.5); color: rgba(255,255 ...

iOS fixed positioning and scrolling

One of the current challenges we are facing involves implementing an action button similar to the one found in GMail for adding new content. This button needs to remain fixed during scroll. Here is the HTML code: <div class="addButton actionButton" on ...

Using a JavaScript onclick function to retrieve specific elements within the document

I'm attempting to extract the elements from the source that was clicked on, but for some reason it's not functioning correctly. Check out my JSFIDDLE Here's the HTML: <span class="populate" onclick="populate();" href="?id=1">Hello&l ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

What is the best method for extracting the selected value from a Dropdown in ng-bootstrap?

I am currently utilizing ng-bootstrap and I am interested in obtaining the selected value from a dropdown. <div class="col text-right"> <div ngbDropdown placement="top-right" class="d-inline-block"> <button class="btn btn-outline-primary ...

Displaying data from multiple checkboxes in an Angular application using an array

I'm currently facing an issue with displaying checked data in an Array within my Angular application. Upon clicking the checkbox, I receive a true value, but unfortunately, the data does not display as it contains null values. For reference, here is ...

Revisiting the Issue: Jquery Hover Effect Fails to Function in Internet Explorer

Can anyone provide assistance with this issue? The code snippet works perfectly in all browsers except for IE, where the menu does not fade in and looks unattractive. html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

Do Observables serve as an extension of Promises?

Understanding the distinction between these two concepts is not the purpose of this particular question. I have encountered this inquiry twice already and I have yet to come across any online resources that adequately clarify it. Does the creation of obs ...

Update the input field's placeholder with the current date

Is it possible to dynamically set the placeholders for <input type='date' placeholder='{{ 'currentDate' }}'>? I have tried using the following variable: currentDate = this.datePipe.transform(new Date(), "yyyy-MM-dd& ...

similar to outdated compileComponentAsync for loading dynamic components

Recently, I encountered a roadblock with my app's dynamic component loading code. The function compileComponentAsync used in the code has been deprecated since RC6. I am now on the lookout for an alternative solution and have come across the suggestio ...

Displaying JSON data in a table using Angular 4

Here is a JSON data example: { "0": { "open": 93.3, "high": 96, "low": 93.3, "close": 95.04, "volume": 358172, "score": "100", "symbol": "ZBRA", "company_name": "Zebra Technologies Corp.", "company_sector": "Industrial Goods" }, "1": { "open": 19.1396, "h ...

Dropdown menu with CSS arrow

I'm attempting to create something similar to this: Here's what I have so far: Html: <div class="panel-heading" data-toggle="collapse" href="#data2"> <div class="heading"> APPLICATION </div> <span clas ...

Having trouble integrating Bootstrap with Fullpage.js

After following this solution, I attempted to create a basic full-page layout using bootstrap. I aim to have a front end that scrolls like a full page, ensuring responsiveness. Folder Setup https://i.sstatic.net/ylZF8.png Code Snippet <!DOCTYPE htm ...

How to eliminate bullets from an unordered list using Bootstrap

Struggling to get rid of the bullets displayed on an unordered list using Bootstrap. I attempted utilizing the "text-decoration-none" class without success. ...

Tips for Managing the Hardware Back Button in Ionic 3

Can someone help me enable the hardware back button in Ionic 3? I want it to redirect to specific pages and display designated content. Being new to Ionic 3, I need guidance on how to set up the hardware device buttons for redirection. ...

Adjust the background color of alternate elements

I am looking to customize the background colors of every other element within the structure provided below: <div class="dets"> <div>Simple Layout</div> <div>Few Pictures/Links</div> <div>Element Uniformity</div> ...

Issue with displaying multiple checkboxes using Materialize CSS in combination with Leaflet for web-mapping overlays

I'm currently using Materialize 0.97.7 along with Leaflet 1.0.1 (the latest version). <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet-src.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com ...

Animating Swipes with CSS3

Currently, I am in the process of developing a mobile application framework using HTML 5, CSS3, and jQuery. The layout for the 'screens' markup is structured as follows: <div class="page" id="home"> <!-- content goes here... --> ...