Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing Angular Material's Table Cell Padding

When I inspect the element with developer tools it shows zero padding, but when I look a it and mouse over it, it very clearly has padding within the cell. I have no idea where this is coming from, and setting td { padding: 0 !important } does nothing.

like image 502
yoursweater Avatar asked Jun 05 '18 21:06

yoursweater


People also ask

How do you apply table padding?

To set cell padding in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property padding.

What is MatColumnDef?

MatColumnDef extends CdkColumnDefDefines a set of cells available for a table column.


1 Answers

The perceived padding is being caused by display: table-cell; and vertical-align: inherit; (which usually is value middle) from the default browser/user-agent <td> styles in combination with a height being set on the tr.mat-row. The <tr> with CSS class .mat-row has a set height by default of 48px. You can adjust the height or set to height: auto; then adjust padding to the td.mat-cell as needed. This effectively removes the perceived padding that is visible when inspecting with developer tools. The green padding visualization seen in something like Chrome developer tools when inspecting the <td> is how just a middle vertically aligned element with table-cell is displayed in the tools. If you examine the Computer properties of that <td> you'll see it has zero padding on all four sides.

.mat-row {
  height: auto;
}

.mat-cell {
  padding: 8px 8px 8px 0;
}

Here is a StackBlitz showing the height: auto; on tr.mat-row as well as a custom padding value on td.mat-cell in action.

While I'd recommend to avoid changing the display property value on td.mat-cell, you can change it to something like inline-block to see the effects without any adjustments to height of mat-row.

like image 50
Alexander Staroselsky Avatar answered Sep 27 '22 06:09

Alexander Staroselsky