Stretching the height of a table without altering the position of its contents

While working on a project using Angular and Angular Material, I have realized that the solution might actually lie in pure CSS.

The issue I'm facing is with a table that currently has only 2 or 3 rows. I want this table to have a fixed height of 700px. However, when I set the height of the table to 700px, the content within it stretches to fill the space. This results in top and bottom padding being added to each row to fill the table, causing each cell's total height to be 350px (assuming the table body is 700px).

Furthermore, Angular Material lacks vertical separators for columns. To work around this, I need to apply borders to my cells. But if the cells do not occupy the full height of the table, the vertical line ends at the last row creating an incomplete look.

I've attempted to use flex attributes but without success.

This is what I currently have:

<div id="main-table">
  <div id="table-container">
    <table id="master" mat-table [dataSource]="projects">

      <!-- Name Column -->
      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let project" class="py-0 px-0">
          <button mat-button (click)="getVersionsList(project);" class="cell-button"> {{project.name}} </button>
        </td>
      </ng-container>

      <!-- Description Column -->
      <ng-container matColumnDef="description">
        <th mat-header-cell *matHeaderCellDef> Description </th>
        <td mat-cell *matCellDef="let project" class="py-0"> {{project.description}} </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
  </div>

  <!-- additional content -->

</div>
  .mat-header-cell, .mat-cell{
    padding: 12px;
  }
  .py-0 { // padding on Y axis 0
    padding-top: 0 !important;
    padding-bottom: 0 !important;
  }

  .px-0, .cell-button { // padding on X axis 0
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  .cell-button{
    width: 100%;
    padding: 6px 0;
  }

  .mat-row {
    height: auto;
  }

  .mat-cell{
    //padding-top: 16px;
    //padding-bottom: 16px;
    //border-right: rgba(0,0,0,.12) 1px solid;
    border-bottom: 0;
  }

My CSS remains minimal as nothing seemed to work effectively.

Any assistance will be greatly appreciated.

Answer №1

Dealing with the unresponsiveness of Angular Material Table can be frustrating. I stumbled upon a helpful solution that might work for you as well:

If you're using CSS instead of SCSS, you can try implementing the following code to make your table responsive:

.table-responsive {
  display: block;
  width: 100%;
  overflow-x: auto;
}
.table-responsive .mat-table {
  width: 100%;
  max-width: 100%;
  margin-bottom: 1rem;
  display: table;
  border-collapse: collapse;
  margin: 0px;
}
.table-responsive .mat-row, .table-responsive .mat-header-row {
  display: table-row;
}
.table-responsive .mat-cell, .table-responsive .mat-header-cell {
  word-wrap: initial;
  display: table-cell;
  padding: 0px 5px;
  line-break: unset;
  width: auto;
  white-space: nowrap;
  overflow: hidden;
  vertical-align: middle;
}

To implement this, simply add it to your styles.css file, wrap your table inside a div, and give it the "table-responsive" class.

<div class="mat-elevation-z1 table-responsive">
  <table mat-table [dataSource]="dataSource">
     ...
  </table>
</div>

One thing to note is that if you have a mat-paginator for your table, this solution may not work smoothly. You can try placing the paginator outside the "table-responsive" div as a workaround, which worked well in my case.

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

Integrating Images Seamlessly with Text using HTML and CSS

Still getting the hang of this! I'm working on a webpage with an image and text side by side in separate divs. I've positioned them correctly, but when I resize the page, the text changes size while the image stays the same. I want the text to al ...

Is there a way to create a transparent color for the Dropdown Toggle?

As I work on expanding my understanding of HTML and Bootstrap, utilizing Bootstrap version 5.3.0 Alpha 3, I encounter an issue with the Dropdown feature. Typically, when a Dropdown is used within an element such as an icon or text, it displays an arrow sym ...

jQuery class does not apply correctly in IE9

Currently, I'm designing a menu for a WordPress theme and facing a styling issue. I aim to differentiate between menu items with submenus and those without by applying different styles on hover. Here's the jQuery code snippet I am using: $(docum ...

How to Style a Diamond Shape with Content Inside Using CSS

Can someone help me figure out how to create a diamond-shaped background in HTML? I want the diamond shape to contain text and images, similar to what's shown in this screenshot. The example image uses a background image of a diamond, but I'm loo ...

Site Having Trouble Displaying Image

I'm currently working on a Bridgetown website using Bootstrap 5, and I've encountered an issue with adding an image that won't show up on the site. This is what it currently looks like: https://i.sstatic.net/IUR7x.jpg Code <nav class=&quo ...

Tips for updating the color of carousel image slider indicators (arrows) specifically for the second slide

Is there a way to customize the carousel image slider indicator arrows for only the second image slide? For the first slide, I want the indicators to be black, but for the second slide, I would like them to be white. Can this be achieved? I attempted to ...

Avoid importing the React CSS style file at all costs

Currently, I am working on a project and facing a minor issue related to importing the style.css file. I am aware that the syntax should be import "./style.css", however, upon implementation, I encountered a Terminal Output error. The output generated by ...

Looking for ideas on what to name two PHP classes?

In my project, I have a class called user that is initialized in /update/user.php when the file update.php is executed. This particular class is responsible for handling various user-related queries on the database like edit, delete, and create functionali ...

Expandable Menu that slides up and down

Here is a link to an Accordion Menu I have created: https://jsfiddle.net/wfr57s57/ This piece of JQuery Code controls the functionality: $(".menu-item").click(function(e) { var allContents = $(".menu-content"); e.preventDefault(); if(allC ...

What is the best way to position three SVG files in the center of a webpage?

I want to combine three separate SVG files to create one single graphic. The first SVG file is a red triangle, the second is a blue circle inside the triangle, and the third is a purple rectangle beneath the triangle with a little space in between. My obje ...

How can you turn a SWFObject into a clickable link?

Is there a way to create a clickable link using a .swf flash file with swfObject? I want the entire video to be clickable, but I'm struggling to figure out how to do it. If you take a look at this example here: , you'll see that they were able ...

Pick out grouped objects without the use of Javascript

I am looking to design a single page where the display of categorized items changes based on user selection. Ideally, I want this to be chapter/hash (:target) dependent, but checkboxes influencing state would also work. As an experiment, I am trying to ac ...

Incorporate an icon into a text input field using Material UI and React

I have a form with a text input element: <FormControl className='searchOrder'> <input className='form-control' type='text' placeholder='Search order' aria-label='Search&ap ...

Drop-down menu in a table cell overflowing with lengthy text, causing the table to collapse

I'm facing an issue with my table where I have inputs and selects inside <td>. The problem arises when the text in the options of the <select> element is too long, causing the <td> to expand and disrupt the overall layout of the tabl ...

Unlocking the potential of Bootstrap 4 pop ups and modals within an Angular 6 project

Angular.json "styles": [ "./node_modules/bootstrap/dist/css/bootstrap.min.css", "./node_modules/ngx-bootstrap/datepicker/bs-datepicker.css", "src/styles.css" ], ...

Move cursor over the element with a specified attribute

Is it possible to apply a hover effect to an active element without using the disabled attribute? I attempted the code below, but unfortunately, it did not produce the desired hover effect. input.btn:not([disabled]):hover ...

Adjust the positioning of the navbar-brand to the right within bootstrap 4

Here is what I currently have: <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarS ...

Updating a text input's placeholder using Bootstrap4

Bootstrap 4 provides classes like text-left and text-right for aligning text inside an input field. Is there a Bootstrap-friendly method to only align the placeholder text in an input without affecting the alignment of the actual text? ...

Are there any CSS3 selectors currently available or planned for matching partial attribute names?

By using [foo^="bar"], you can target nodes with the attribute foo that starts with the value bar. Is there a method to select nodes based on an attribute name that begins with a specific string? This would be useful for selecting all nodes with a data-* ...

Troubleshoot: Issue with Navbar Dropdown Expansion on Bootstrap Sass 3.3.6 with JavaScript

Beginner: Bootstrap Sass 3.3.6 - Incorporating Javascript - Issue with Navbar Dropdown Not Expanding application.html.erb Head: <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> ...