意味着使结果表看起来不像这样:
[=== ROW ===] [=== ROW ===] [=== ROW ===] [=== ROW ===]
...像这样:
[=== ROW ===] [=== ROW ===] [=== ROW ===] [=== ROW ===]
我尝试添加
margin-bottom:1em;
到td和tr都没收到。有任何想法吗?
意味着使结果表看起来不像这样:
[=== ROW ===] [=== ROW ===] [=== ROW ===] [=== ROW ===]
...像这样:
[=== ROW ===] [=== ROW ===] [=== ROW ===] [=== ROW ===]
我尝试添加
margin-bottom:1em;
到td和tr都没收到。有任何想法吗?
Answers:
一切你需要的:
table {
border-collapse: separate;
border-spacing: 0 1em;
}
假设您想要一个1em的垂直间隙,而没有水平的间隙。如果执行此操作,则可能还应该考虑控制行高。
人们给出的一些答案有点奇怪,涉及边界崩溃:崩溃,其作用与问题所要求的完全相反。
thead, tbody {position: relative; top: -{border-spacing-value} }
。
table {
border-collapse: collapse;
}
td {
padding-top: .5em;
padding-bottom: .5em;
}
除非先设置边框折叠,否则单元格不会对任何内容做出反应。您还可以在设置边框元素后向TR元素添加边框(除此之外)。
如果这是用于布局,那么我将转向使用DIV和更多最新的布局技术,但是,如果这是表格数据,请自行淘汰。我仍然大量使用Web应用程序中的表来存储数据。
如果没有其他答案令人满意,那么您总是可以创建一个空行,并使用CSS适当地设置其样式以使其透明。
如果您没有边框,或者有边框,并且想要在单元格内保持间距,则可以使用padding
或line-height
。据我所知,边距对单元格和行没有影响。
单元格间距的CSS属性为border-spacing
,但在IE6 / 7上不起作用(因此您可以根据人群使用它)。
如果所有其他方法均失败,则可以cellspacing
在标记中使用旧属性-但这也将使您在各列之间留出间隔。一些CSS重置建议您应该进行设置以获取跨浏览器的支持:
/ *表仍然需要
cellspacing="0"
标记* /
这是这种方式(我当时认为这是不可能的):
首先,为表格仅提供垂直边框间距(例如5px),并将其水平边框间距设置为0。然后,应为每个行单元格设置适当的边框。例如,每行中最右边的单元格应在顶部,底部和右侧具有边框。最左边的单元格应在顶部,底部和左侧具有边框。并且这两个之间的其他单元格应该仅在顶部和底部具有边框。像这个例子:
<table style="border-spacing:0 5px; color:black">
<tr>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-left:thin black solid;">left-most cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-right:thin black solid;">right-most cell</td>
</tr>
<tr>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-left:thin black solid;">left-most cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-right:thin black solid;">right-most cell</td>
</tr>
<tr>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-left:thin black solid;">left-most cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid;">other cell</td>
<td style="border-bottom:thin black solid; border-top:thin black solid; border-right:thin black solid;">right-most cell</td>
</tr>
</table>
您只需在元素上使用padding-top
和。padding-bottom
td
单位可以从此列表中进行任何操作:
td
{
padding-top: 10px;
padding-bottom: 10px;
}
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr>
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>
如果您坚持使用表格进行设计,则最好的主意是在表格的各行之间给出一个空行,其中不包含任何内容,并且指定高度。
您可以使用div来避免这种复杂性。只需给每个div一个边距。
您也可以使用CSS修改每一行的高度。
<head>
<style>
tr {
height:40px;
}
</style>
</head>
<body>
<table>
<tr> <td>One</td> <td>Two</td> </tr>
<tr> <td>Three</td> <td>Four</td> </tr>
<tr> <td>Five</td> <td>Six</td> </tr>
</table>
</body>
您还可以修改<td>
元素的高度,它应该会为您提供相同的结果。
在tr中添加以下规则,它应该可以工作
float: left
样本(在IE9教学课程中打开它:)):http : //jsfiddle.net/zshmN/
编辑:这不是许多人指出的合法或正确的解决方案,但是如果您别无选择,并且需要某些东西,那么它将在IE9中起作用。
因此,所有在此投下反对票的人,也请让我们知道正确的解决方案