html - 在表格中排列标题和数据(html & css)
问题描述
我正在阅读关于表格的 CSS教程,在其中一个示例中,作者创建了一个表格,该表格<td>
在<th>
.
似乎如果您设置border-collapse: collapse
表格标题不与表格主体对齐(边框延伸太远的左侧和右侧有空间)。
table {
border-collapse: collapse;
border-spacing: 0;
}
th,
td {
padding: 10px 15px;
}
thead {
/* background: #395870; */
/* color: #fff;*/
background: #f0f0f2
}
tbody tr:nth-child(even) {
background: #f0f0f2
}
td {
border-bottom: 1px solid #cecfd5;
border-right: 1px solid #cecfd5;
}
table td:first-child {
border-left: 1px solid #cecfd5;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>your page title goes here</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="test.css">
</head>
<body>
<table>
<caption>Design and Front-End Development Books</caption>
<thead>
<tr>
<th scope="col" colspan="2">Item</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Don't Make me Think by Steve Krug</td>
<td>In Stock</td>
<td>1</td>
<td>$30.02</td>
</tr>
</tbody>
<tr>
<td>A Project Guide to UX Design by Russ Unger & Carolyn Chandler</td>
<td>In Stock</td>
<td>2</td>
<td>$52.94 ($26.47 x 2)</td>
</tr>
<tr>
<td>Introducing HTML5 by Bruce Lawson & Remy Sharp</td>
<td>Out of Stock</td>
<td>1</td>
<td>$22.23</td>
</tr>
<tr>
<td>Bulletproof Web Design by Dan Cederholm</td>
<td>In Stock</td>
<td>1</td>
<td>$30.17</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Subtotal</td>
<td>$135.36</td>
</tr>
<tr>
<td colspan="3">Tax</td>
<td>$13.54</td>
</tr>
<tr>
<td colspan="3">Total</td>
<td>$148.90</td>
</tr>
</tfoot>
</table>
</body>
</html>
但是,如果您设置,border-collapse: separate
border-spacing: 0
那么它最终会正常工作,如下所示。
table {
border-collapse: separate;
border-spacing: 0;
}
th,
td {
padding: 10px 15px;
}
thead {
/* background: #395870; */
/* color: #fff;*/
background: #f0f0f2
}
tbody tr:nth-child(even) {
background: #f0f0f2
}
td {
border-bottom: 1px solid #cecfd5;
border-right:1px solid #cecfd5;
}
table td:first-child {
border-left: 1px solid #cecfd5;
}
/* The first td inside of each tr */
/* td:first-child {
border-left: 1px solid #cecfd5;
} */
<!DOCTYPE html>
<html lang="en">
<head>
<title>your page title goes here</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="test.css">
</head>
<body>
<table>
<caption>Design and Front-End Development Books</caption>
<thead>
<tr>
<th scope="col" colspan="2">Item</th>
<th scope="col">Qty</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Don't Make me Think by Steve Krug</td>
<td>In Stock</td>
<td>1</td>
<td>$30.02</td>
</tr>
</tbody>
<tr>
<td>A Project Guide to UX Design by Russ Unger & Carolyn Chandler</td>
<td>In Stock</td>
<td>2</td>
<td>$52.94 ($26.47 x 2)</td>
</tr>
<tr>
<td>Introducing HTML5 by Bruce Lawson & Remy Sharp</td>
<td>Out of Stock</td>
<td>1</td>
<td>$22.23</td>
</tr>
<tr>
<td>Bulletproof Web Design by Dan Cederholm</td>
<td>In Stock</td>
<td>1</td>
<td>$30.17</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="3">Subtotal</td>
<td>$135.36</td>
</tr>
<tr>
<td colspan="3">Tax</td>
<td>$13.54</td>
</tr>
<tr>
<td colspan="3">Total</td>
<td>$148.90</td>
</tr>
</tfoot>
</table>
</body>
</html>
我不完全确定为什么会发生这种情况。如果有人能解释这是如何工作的,那就太好了。
解决方案
我看不出有什么不同。border-color: 'black'
如果遇到任何变化,请使用。
推荐阅读
- php - 有没有 Docker PHP 镜像没有冗长的编译过程,或者如何减少编译量,加快镜像构建速度?
- c# - 将 ModelState 转换为 JSON 以进行日志记录
- c# - ORA-01008: 并非所有变量都绑定 MVC 应用程序
- c++ - 如何在此代码中包含 While 语句
- go - 在 golang 中读取二进制 .fbx 文件
- jenkins - 如何在 Jenkins 中显示本地测试报告?
- python - Python-无法访问其他方法内的类方法
- r - 编码加权平均值 (R)
- msbuild - 使用 MSBuild ProjectReference 在构建中包含其他文件?
- angular - Angular DI - useFactory - Provider:有没有办法在本地状态更新时第二次调用 Provider Factory 函数?