How to add border radius to angular mat table?

I am trying to make the mat-table border as round. But it is not working. Tried this-

table { width: 100%; border-collapse: collapse; border-radius: 1em; } 

How to achieve this?

2

8 Answers

The most simple solution that worked for me was to hide the overflow of certain elements that gave the unfinished look when we set the border-radius. So, we just handle that

.mat-table{ border-radius: 8px; overflow:hidden !important; } 

Feel free to point out cases where this might cause other issues.

2

the problem is that you need over-ride the background-color of mat-table:

.mat-table { background-color:transparent!important; } table { width: 100%; border-collapse: collapse; border-radius: 5em; } table tr:last-child td /*To remove the last border*/ { border-bottom:0 solid } 

stackblitz

3

Update:

@Eliseo answer works if you add border to mat-table: border: solid 1px grey;

 .mat-table { background-color:transparent!important; border: solid 1px grey; } table { width: 100%; border-collapse: collapse; border-radius: 5em; } table tr:last-child td /*To remove the last border*/ { border-bottom:0 solid } 

This CSS code working in Mat table border Radius

CSS code for Mat table border radius enter image description here

th.mat-header-cell:first-of-type, td.mat-cell:first-of-type, td.mat-footer-cell:first-of-type { padding-left: 24px; border-top-left-radius: 9px; } th.mat-header-cell:last-of-type, td.mat-cell:last-of-type, td.mat-footer-cell:last-of-type { padding-right: 24px; border-top-right-radius: 9px; } 
1

If you want it in whole application just add it in you main css with !important like following

.mat-table { width: 100% !important; border-collapse: collapse !important; border-radius: 1em !important; } 

or if this doesnt works there is another way to use /deep/ Or ::deep

although it is not best practice and is also deprecated but solves the purpose

1

Your styling problems don't have to do with Angular Material tables but with HTML tables in general. If you search for how to style a table e.g. add borders to rows or margins you'll find various answers and suggestions.

You basically can't add margin or padding to a table row directly.

margin applies to all elements except elements with table display types other than table-caption, table and inline-table.

padding applies to all elements except table-row-group, table-header-group, table-footer-group, table-row, table-column-group and table-column.

Solutions

How you set a border for table rows and specify a margin and padding depends on the border model (collapse or separate) you use.

The separated borders model: border-collapse: seperate

In the separated borders model, the edges coincide with the border edges of cells. (And thus, in this model, there may be gaps between the rows, columns, row groups or column groups, corresponding to the 'border-spacing' property.)

In this model, each cell has an individual border. The 'border-spacing' property specifies the distance between the borders of adjoining cells. (...) Rows, columns, row groups, and column groups cannot have borders (i.e., user agents must ignore the border properties for those elements).

  1. Solution: If you want a border, margin and padding you could do something like this:

    td.mat-cell { /* row padding / padding: 16px 0; / row border */ border-bottom: 1px solid #ffa600; border-top: 1px solid #ffa600; }

    td.mat-cell:first-child { /* row border */ border-left: 1px solid #ffa600; }

    td.mat-cell:last-child { /* row border */ border-right: 1px solid #ffa600; }

    table { /* row spacing / margin */ border-spacing: 0 8px !important; }

The collapsing border model: border-collapse: collapse The edges of the rows, columns, row groups and column groups in the collapsing borders model coincide with the hypothetical grid lines on which the borders of the cells are centered. (And thus, in this model, the rows together exactly cover the table, leaving no gaps; ditto for the columns.)

In the collapsing border model, it is possible to specify borders that surround all or part of a cell, row, row group, column, and column group. (...) Also, in this model, a table does not have padding (but does have margins).

  1. Solution: If you only want a row border and padding but no spacing / margin between the rows you could do:

    table { border-collapse: collapse; }

    th { /* row border */ border-bottom: 1px solid #ffa600; }

    td.mat-cell { /* row padding */ padding: 20px 0; border: none; }

    tr.mat-row { /* row border */ border: 1px solid #ffa600; }

simply add this to your css file: table, th, td { border-radius: 10px; }

simply add this to your css file:

table, th, td { border-radius: 10px; }

1
thead, tr, th { border-radius: 8px; } tbody { tr:last-child { td { border-radius: 8px; } } } 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like