在我的HTML文档中,有一个包含两列和多行的表格。如何使用CSS增加第一列和第二列之间的间距?我尝试将“margin-right: 10px;”应用于左侧的每个单元格,但没有效果。
padding-right
可以解决你的特定(视觉)问题,但这不是添加表格单元格间距的正确方法。对于单元格,padding-right
的作用类似于它对大多数其他元素的作用:它增加了单元格内部的空间。如果单元格没有边框、背景颜色或其他显示出差异的东西,那么这可以模仿设置单元格间距的效果,否则不行。内部表格元素生成带有内容和边框的矩形框。单元格也有填充。内部表格元素没有外边距。
那么什么是“正确”的方法?如果您想替换表格的 cellspacing 属性,则border-spacing
(使用 border-collapse
禁用)是一种替代方法。但是,如果需要每个单元格的“margins”,我不确定如何使用 CSS 正确实现。我能想到的唯一的hack 是像上面那样使用 padding
,避免对单元格进行任何样式设置(背景色、边框等),而是在单元格内使用容器 DIV 实现这样的样式。将此应用于您的第一个 <td>
:
padding-right:10px;
HTML示例:
<table>
<tr>
<td style="padding-right:10px">data</td>
<td>more data</td>
</tr>
</table>
border-spacing: 5px;
。 - Sølve T.border-spacing
还会在行之间添加空间。不幸的是,没有单独控制垂直和水平间距的属性。 - Lee Blakeborder-spacing
需要两个参数 - 一个用于水平方向,另一个用于垂直方向。 - OldTinfoil如果您无法使用填充(例如您在td中有边框),请尝试以下方法
table {
border-collapse: separate;
border-spacing: 2px;
}
我知道这有点晚了,但是为了记录,您也可以使用CSS选择器来实现这一点(消除了内联样式的需要)。 这个CSS将填充应用于每行的第一列:
table > tr > td:first-child { padding-right:10px }
这将是您的HTML,没有CSS!:
<table><tr><td>data</td><td>more data</td></tr></table>
这样可以实现更加优雅的标记,特别是在需要使用 CSS 进行大量具体格式化的情况下。
很不幸,margin在单独的单元格上不起作用,但是您可以在想要之间添加额外的列以间隔两个单元格之间...另一个选项是使用与背景相同颜色的边框...
您可以简单地这样做:
<html>
<table>
<tr>
<td>one</td>
<td width="10px"></td>
<td>two</td>
</tr>
</table>
</html>
不需要 CSS :) 这个 10px 是您的空间。
<table>
<tr>
<td style="padding-right: 10px;">one</td>
<td>two</td>
</tr>
</table>
CSS:
table {border-collapse: separate; border-spacing:0; } /* separate needed */
td { display: inline-block; width: 33% } /* Firefox need inline-block + width */
td { position: relative } /* needed to make td move */
td { left: 10px; } /* push all 10px */
td:first-child { left: 0px; } /* move back first 10px */
td:nth-child(3) { left: 20px; } /* push 3:rd another extra 10px */
/* to support older browsers we need a class on the td's we want to push
td.col1 { left: 0px; }
td.col2 { left: 10px; }
td.col3 { left: 20px; }
*/
HTML:
<table>
<tr>
<td class='col1'>Player</td>
<td class='col2'>Result</td>
<td class='col3'>Average</td>
</tr>
</table>
2016年更新
Firefox现在支持它而无需使用inline-block
和固定的width
属性。
table {border-collapse: separate; border-spacing:0; }
td { position: relative; padding: 5px; }
td { left: 10px; }
td:first-child { left: 0px; }
td:nth-child(3) { left: 20px; }
td { border: 1px solid gray; }
/* CSS table */
.table {display: table; }
.tr { display: table-row; }
.td { display: table-cell; }
.table { border-collapse: separate; border-spacing:0; }
.td { position: relative; padding: 5px; }
.td { left: 10px; }
.td:first-child { left: 0px; }
.td:nth-child(3) { left: 20px; }
.td { border: 1px solid gray; }
<table>
<tr>
<td>Player</td>
<td>Result</td>
<td>Average</td>
</tr>
</table>
<div class="table">
<div class="tr">
<div class="td">Player</div>
<div class="td">Result</div>
<div class="td">Average</div>
</div>
</div>
使用 border-collapse: separate; 对我没有起作用,因为我只需要在表格单元格之间有一个边距而不是在表格的边缘上有一个边距。
我想出了下一个解决方案:
-CSS
.tableDiv{
display: table;
}
.cellSeperator {
display: table-cell;
width: 20px;
}
.cell1 {
display: table-cell;
width: 200px;
}
.cell2 {
display: table-cell;
width: 50px;
}
-HTML
<div class="tableDiv">
<div class="cell1"></div>
<div class="cellSeperator"></div>
<div class="cell2"></div>
</div>
以这种方式无法单独选定单元格中的各列。在我看来,最好的选择是在第二列上添加style='padding-left:10px'
,并在内部div或元素上应用样式。这样,您可以实现更大空间的幻象。