为什么我无法对有角度的垫子桌子行应用边框?

Rob*_*Rob 4 css angular-material angular angular-material-table

我有一个简单的角度材质表:

<table mat-table [dataSource]="scoreboardData">
    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>
    <ng-container matColumnDef="totalScore">
      <th mat-header-cell *matHeaderCellDef> Total Score </th>
      <td mat-cell *matCellDef="let element"> {{element.totalScore}}</td>
    </ng-container>
    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我想在行之间以及边框之间添加一些额外的边距,甚至可能添加一些填充。我发现我可以更改行的背景颜色,但不能对行应用边距,填充或边框。

tr.mat-row {
    background: #2f5b98; /* Old browsers */
    background: -moz-linear-gradient(top, rgba(74,144,239,1) 20%, rgba(0,0,0,1) 100%); /* FF3.6-15 */
    background: -webkit-linear-gradient(top, rgba(74,144,239,1) 20%,rgba(0,0,0,1) 100%); /* Chrome10-25,Safari5.1-6 */
    background: linear-gradient(to bottom, rgba(74,144,239,1) 20%,rgba(0,0,0,1) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4a90ef', endColorstr='#000000',GradientType=0 ); /* IE6-9 */

    margin-top: 16px;
    padding: 8px;
    border: 1px solid #FAFF00;
}
Run Code Online (Sandbox Code Playgroud)

我敢肯定这很简单,但是我无法弄清楚是什么导致我无法将这些样式应用于行。同样,我可以更改背景,其中的字体和其他几种样式,而不必更改这三种特定样式(填充,边距,边框)。

编辑:我已经确定,在任何html表上都不允许填充和边距。边界仍然使我困惑。

fri*_*doo 5

您的样式问题与Angular Material表无关,但通常与HTML表有关。如果您搜索如何设置样式,table例如在行或边距上添加边框,则会找到各种答案和建议。

您基本上不能直接添加margin或添加padding到表行<tr>

margin适用于除表显示类型为table-captiontableinline-table之外的其他元素。

padding适用于除table-row-grouptable-header-grouptable-footer-grouptable-rowtable-column-group 和table-column 之外的所有元素。

解决方案

如何border为表格行设置a 并指定a marginpadding取决于您使用的边框模型collapseseparate)。

分隔边框模型: border-collapse: seperate

分离的边界模型中,边缘与单元的边界边缘重合。(因此,在此模型中,对应于“边框间距”属性,行,列,行组或列组之间可能存在间隙。)

在此模型中,每个单元格都有一个单独的边界。'border-spacing'属性指定相邻单元格的边界之间的距离。(...)行,列,行组和列组不能有边框(即,用户代理必须忽略这些元素的边框属性)。

1.解决方案:如果需要bordermarginpadding,可以执行以下操作:

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;
} 
Run Code Online (Sandbox Code Playgroud)

https://stackblitz.com/edit/angular-cjxcgt-wtkfg4

崩溃的边界模型: border-collapse: collapse

折叠边界模型中行,列,行组和列组的 边缘与单元格边界居中的假想网格线重合。(因此,在此模型中,行一起精确地覆盖了表格,没有任何空隙;各列也一样。)

在折叠边框模型中,可以指定包围单元格,行,行组,列和列组的全部或部分的边框。(...)此外,在此模型中,表没有填充(但是有边距)。

2.解决方案:如果只需要行边框填充,但行之间没有间距/边距,则可以执行以下操作:

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;
}
Run Code Online (Sandbox Code Playgroud)

https://stackblitz.com/edit/angular-cjxcgt-xphbue