Smi*_*ith 3 html css html-table rounded-corners
我有圆角的CSS规则:
th, td { padding: 8px;
background: #E8ECE0;
text-align: center;
border: 1px solid #444;
border-bottom-width: 0px;
}
thead { background-color: #446bb3 ; color :#fff; padding:4px; line-height:30px }
tbody tr:nth-child(even) {background: #F6F6EC;}
tbody tr:nth-child(odd) {background: #FFF}
tr:first-child td, tr:first-child th {
border-top-left-radius: 12px; border-top-right-radius: 12px;
}
tr:last-child td {
border-bottom: 1px solid #444;
border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;
}
table { border-spacing: 0; border: 0; margin:0px; width:100%; padding:5px}
td.pd {border-bottom-left-radius: 12px; border-bottom-right-radius: 12px;}
td.pu {border-top-left-radius: 12px; border-top-right-radius: 12px;}
Run Code Online (Sandbox Code Playgroud)
我的html表是:
<table >
<tbody>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
<tr >
<td >Hello</td><td >Hello</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
这给了我这个

我如何解决这个问题,因为表格和表格中间的td元素也有圆角?我只需要第一行和最后一行有圆角.
假设你table的html类似于以下内容:
<table>
<thead>
<tr>
<th>First column</th>
<th>Second column</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row one, cell one</td>
<td>Row one, cell two</td>
</tr>
<tr>
<td>Row two, cell one</td>
<td>Row two, cell two</td>
</tr>
<tr>
<td>Row three, cell one</td>
<td>Row four, cell two</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
以下CSS应该有效:
table {
border-spacing: 0;
}
th, td {
border: 1px solid #000;
padding: 0.5em 1em;
}
/* the first 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:first-child {
border-radius: 0.6em 0 0 0;
}
/* the last 'th' within the first 'tr' of the 'thead': */
thead tr:first-child th:last-child {
border-radius: 0 0.6em 0 0;
}
/* the first 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:first-child {
border-radius: 0 0 0 0.6em;
}
/* the last 'td' within the last 'tr' of the 'tbody': */
tbody tr:last-child td:last-child {
border-radius: 0 0 0.6em 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑回应OP提出的问题:
表中的边界有点想,我怎么做它1px
单元格之间的边界有点厚,因为一个单元格的左边框与前一个单元格的右边框相对(顶部/底部边框相同).
删除该效果的一种方法是border-collapse: collapse;在table元素上指定.不幸的是这样做的效果是也删除/复位/重载border-radius声明:演示.
更复杂的方法是手动删除具有前一行的行的顶部边框,以及跟随单元格的单元格的左边框,将以下内容添加到先前的CSS:
thead + tbody tr td,
tr + tr td {
border-top: 0;
}
tr th + th,
tr td + td {
border-left: 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑减少使CSS更耐用(对于单细胞行,添加tfoot或删除thead):
table {
border-spacing: 0;
}
th, td {
border: 1px solid #000;
padding: 0.5em 1em;
}
thead tr:first-child th:first-child,
tbody:first-child tr:first-child td:first-child {
border-top-left-radius: 0.6em;
}
thead tr:first-child th:last-child,
tbody:first-child tr:first-child td:last-child {
border-top-right-radius: 0.6em;
}
thead + tbody tr:last-child td:first-child,
tfoot tr:last-child td:first-child {
border-bottom-left-radius: 0.6em;
}
thead + tbody tr:last-child td:last-child,
tfoot tr:last-child td:last-child {
border-bottom-right-radius: 0.6em;
}
thead + tbody tr td,
tfoot + tbody tr td,
tfoot tr td,
tbody + tbody tr td,
tr + tr td {
border-top: 0;
}
tr th + th,
tr td + td {
border-left: 0;
}
Run Code Online (Sandbox Code Playgroud)
tbody在没有元素的情况下,存在多个元素的问题,tfoot其中第一个元素tbody将在其下边界处保留边界半径.
小智 6
你可以把表放入div.div的样式(示例):
div {
border-radius: 4px;
-moz-border-radius: 4px
-webkit-border-radius: 4px;
overflow: hidden; /*notice*/
}
Run Code Online (Sandbox Code Playgroud)
因此桌角将被隐藏.