Expandable rows within an Angular Material table (mat-table) feature a distinct gap, even when rows are collapsed

I recently integrated an Angular Material table into my website, following the examples provided on the official Angular page (link to the examples). I wanted the table rows to be expandable similar to the "Table with expandable rows" example on the Angular site. However, I noticed that there were gaps in the collapsed rows which revealed a small portion of the expandable part of the row (picture showing the issue).

In addition, I also implemented the filter function and remove/add column functions from the Angular Material examples. Could this have affected the styling of the table?

Upon inspecting the browser console, I found that the problem was due to a padding of the table rows set to 0.75rem in the bootstrap.css file. Adjusting the padding to 0 made the normal rows look unappealing. Is there a way to eliminate these unsightly gaps without compromising the overall design?

The Angular component contains animations for the expandable rows:

@Component({
  selector: 'app-compare-page',
  templateUrl: './compare-page.component.html',
  styleUrls: ['./compare-page.component.css'],
  animations: [
    trigger('detailExpand', [
      state('collapsed', style({height: '0px', minHeight: '0'})),
      state('expanded', style({height: '*'})),
      transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
    ]),
  ]
})

To maintain clarity, the HTML elements within the div with the class example-element-diagram have been excluded.

<table mat-table [dataSource]="tableData" multiTemplateDataRows class="mat-elevation-z8">
    <div *ngFor="let column of displayedColumns; track column">
      <ng-container [matColumnDef]="column">
        <th mat-header-cell *matHeaderCellDef> {{column}} </th>
        <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
      </ng-container>
    </div>
    <ng-container matColumnDef="expand">
      <th mat-header-cell *matHeaderCellDef aria-label="row actions">&nbsp;</th>
      <td mat-cell *matCellDef="let element">
        <div *nfIf="expandedElement === element; else elseBlock "> 
        <button mat-icon-button aria-label="expand row" (click)="(expandedElement = expandedElement === element ? null : element); $event.stopPropagation()">
            <mat-icon>keyboard_arrow_up</mat-icon>
          </button>
          </div>
          <ng-template #elseBlock>
            <button mat-icon-button aria-label="expand row" (click)="(expandedElement = expandedElement === element ? null : element); $event.stopPropagation()"> 
            <mat-icon>keyboard_arrow_down</mat-icon>
          </button>
          </ng-template>
         
      </td>
    </ng-container>

    <ng-container matColumnDef="expandedDetail">
      <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplayWithExpand.length">
        <div class="example-element-detail"
           [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
            <div class="example-element-diagram">


            </div>
          </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="columnsToDisplayWithExpand"></tr>
    <tr mat-row *matRowDef="let element; columns: columnsToDisplayWithExpand;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? undefined : element">
  </tr>
    <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
  </table>

I attempted various adjustments to the padding in CSS without achieving success. The stylesheet remains consistent with the one provided in the Angular online example.

Answer №1

I recently discovered a solution that, while not considered best practice, worked for me. I simply added the "padding = 0px" to the necessary rows in the HTML file using the style keyword, ensuring only the designated rows were affected.

Below is the same code snippet as above, now with the additional style applied where needed:

<table mat-table [dataSource]="tableData" multiTemplateDataRows class="mat-elevation-z8">
    <div *ngFor="let column of displayedColumns; track column">
      <ng-container [matColumnDef]="column">
        <th mat-header-cell *matHeaderCellDef> {{column}} </th>
        <td mat-cell *matCellDef="let element"> {{element[column]}} </td>
      </ng-container>
    </div>
    <ng-container matColumnDef="expand">
      <th mat-header-cell *matHeaderCellDef aria-label="row actions">&nbsp;</th>
      <td mat-cell *matCellDef="let element">
        <div *nfIf="expandedElement === element; else elseBlock "> 
        <button mat-icon-button aria-label="expand row" (click)="(expandedElement = expandedElement === element ? null : element); $event.stopPropagation()">
            <mat-icon>keyboard_arrow_up</mat-icon>
          </button>
          </div>
          <ng-template #elseBlock>
            <button mat-icon-button aria-label="expand row" (click)="(expandedElement = expandedElement === element ? null : element); $event.stopPropagation()"> 
            <mat-icon>keyboard_arrow_down</mat-icon>
          </button>
          </ng-template>
         
      </td>
    </ng-container>

    <ng-container matColumnDef="expandedDetail">
      <td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplayWithExpand.length" style="padding: 0px;">
        <div class="example-element-detail"
           [@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
            <div class="example-element-diagram">
             
            </div>
          </div>
      </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="columnsToDisplayWithExpand"></tr>
    <tr mat-row *matRowDef="let element; columns: columnsToDisplayWithExpand;"
      class="example-element-row"
      [class.example-expanded-row]="expandedElement === element"
      (click)="expandedElement = expandedElement === element ? undefined : element">
  </tr>
    <tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
  </table>

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

Is it possible to modify the concealed global attribute using CSS?

Imagine this scenario: <div hidden>blah blah blah</div> If I hide the <div> element like that, can CSS be used to make it visible again? Alternatively, is there an HTML-only method to hide a <div> that can later be reversed with ...

What are some ways to ensure my web application appears correctly on Internet Explorer versions 8 and 9?

When I compare how my application looks in IE versus Chrome and Firefox, it seems like there are styling issues. I want to address this problem. I attempted to use the IE developer tool but couldn't find any options to customize styles and CSS. How d ...

Ensure that CSS is integrated correctly

I have a main Django template that incorporates another template for the navigation bar. I currently have separate CSS files for both the main page and the navbar. My query is: what is the correct method to include the CSS from the navbar in the main p ...

Differences in Changing Div Background Between Firefox and Chrome

My webpage has a div element that I want to be able to change the background color of by clicking on it. The background should either be transparent with no color, or a reddish/pinkish shade. I've written a javascript function to handle this function ...

Best practices for integrating text and images within a website header using CSS and HTML

I need help figuring out the most efficient method for creating a page header that showcases an image while keeping the text visible for search engines and in case the image fails to load. According to Google, it is recommended to use a <div> for th ...

Retrieve the paragraph located directly under the specified heading from any location

I'm having trouble figuring out how to specifically target a paragraph after the heading. Here is an example of my HTML file script: <!doctype html> <html> <head><title>SAM.com</title> </head> <body><h1&g ...

Sending an object between two components without a direct parent-child connection

Hello, I have a question similar to the one asked on Stack Overflow regarding passing a value from one Angular 2 component to another without a parent-child relationship. In my scenario, Component1 subscribes to a service that makes a GET request to the ...

"Enhance Your Website with Foundation 6.2's Spacious Gutter

The Foundation 6.2 documentation indicates that the default grid gutter size is 20px for small screens and 30px for medium screens. However, it fails to specify the default gutter size for large screens. Can you please provide information on the default ...

Perfecting the Radio Button Selection (because why not?)

Struggling to round the outer corners of three inline squared radio buttons with unique IDs. Need help fixing my CSS syntax - I can get each button to individually round its corners, but not just radio1 and radio3's outer corners. Check out the issue ...

Is it feasible to transition a mobile webpage seamlessly without using jqm?

I'm currently developing a Phonegap app and I am trying to steer clear of jqm in my project. I find that the css is causing some issues with the html code. As a workaround, I can transition between pages using: window.location = "profiles.html"; How ...

Hovering over overlapping spans without moving them

For the text formatting, I attempted to use the following code: <style> #items { width:400px; padding: 10px; } .esp { float: left; background-color: yellow; position:relative; z-index:0; } .le { float: left; position:rela ...

I am attempting to swap values within table cells using AngularJS. Would it be recommended to utilize ngBind or ngModel, or is there another approach that would

How can I make a table cell clickable in AngularJS to switch the contents from one cell to another, creating a basic chess game? I want to use angular.element to access the clicked elements and set the second clicked square equal to the first clicked using ...

"Embracing the power of dynamic CSS customization within Umbraco

I recently inherited an Umbraco system without much prior knowledge of the platform, and I am currently investigating the site's performance. The branding and styling of the site is currently managed through a "Brand" document type, allowing the site ...

Making adjustments to the Bootstrap CSS design by removing certain elements

I have inserted a styling link from Bootstrap into the header section of my page, but it is applying additional styles to my cards which I don't want. Is there a way to eliminate these unwanted styles without removing the Bootstrap CSS link? Below, y ...

What is the best way to link to this list of options?

#episode-list { padding: 1em; margin: 1em auto; border-top: 5px solid #69c773; box-shadow: 0 2px 10px rgba(0,0,0,.8) } input { width: 100%; padding: .5em; font-size: 1.2em; border-radius: 3px; border: 1px solid #d9d9d9 } <div id="epis ...

Steps to resolve the error "The value for '--target' option should be one of the following: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es201', 'esnext'."

I recently cloned an Angular application and encountered an error related to the "target" property in the tsconfig.json file within Visual Studio 2019. My Angular version is v16.1.4, with Typescript v5.1.6. I've attempted to resolve this issue by upda ...

Organized grid design where every element occupies its own required space

Here's the current image I have displayed: https://i.sstatic.net/qKH8x.jpg This image is being styled by the following code: .gallery-grid { display: grid; grid-template-columns: repeat(4, 1fr); } I've been searching for a solution ...

Placing a pseudoelement amidst the track and thumb of a range input type

I'm trying to position a pseudo element with a specific z index that is lower than the thumb of an input type range but higher than its track. This is what I have so far: HTML: <div class="range"> <input type="range" name="" class="progre ...

Issue with the overall layout in Bootstrap 4 involving the main content area and sidebar

I am encountering challenges with the overall design of my website, particularly with how the main content area and sidebar are positioned. The layout I have in mind is: https://i.sstatic.net/PYphE.jpg Below is the code snippet I am using to implement th ...

What is the best way to show HTML code from an HTML file in a Vue template?

I need help showcasing the HTML code from an external file in a Vue component template. <template> <div class="content"> <pre> <code> {{fetchCode('./code/code.html')}} & ...