这是否可以通过CSS实现?

我在努力

tr.classname {
  border-spacing: 5em;
}

但没有用。也许我做错了什么?


当前回答

来自Mozilla开发者网络:

border-spacing CSS属性指定相邻单元格边界之间的距离(仅适用于分隔边界模型)。这相当于表示HTML中的cellspacing属性,但可以使用可选的第二个值来设置不同的水平和垂直间距。

这最后一部分经常受到监督。例子:

.your-table {
    border-collapse: separate; /* allow spacing between cell borders */
    border-spacing: 0 5px; /* NOTE: syntax is <horizontal value> <vertical value> */

更新

我现在明白了OP希望特定的、单独的行具有增加的间距。我添加了一个带有tbody元素的设置,它可以在不破坏语义的情况下实现这一点。然而,我不确定是否所有浏览器都支持它。我是用Chrome制作的。

下面的例子是为了展示如何让它看起来像是一个单独的表格,充满了css的甜蜜。也给了第一排更多的空间与车身设置。请随意使用!

支持通知:IE8+、Chrome、Firefox、Safari、Opera 4+

.间距表{字体系列:'Helvetica','Arial',无衬线;字体大小:15px;边界塌陷:分离;表格布局:固定;宽度:80%;边框间距:0 5px;/*这是最终的解决方案*/}.间距表th{文本对齐:左侧;填充:5px 15px;}.间距表td{边框宽度:3px 0;宽度:50%;边框颜色:深红色;边框样式:实心;背景色:红色;颜色:白色;填充:5px 15px;}.间距表td:第一个子项{边框左侧宽度:3px;边界半径:5px 0 0 5px;}.space table td:最后一个子级{边框右侧宽度:3px;边界半径:0 5px 5px 0;}.间距表thead{显示:表格;表格布局:固定;宽度:100%;}.间距表tbody{显示:表格;表格布局:固定;宽度:100%;边框间距:0 10px;}<table class=“spacing table”><thead><tr>主唱</th><th>波段</th></tr></thead><tbody><tr><td>博诺牌手表</td><td>U2型</td></tr></tbody><tbody><tr><td>克里斯·马丁</td><td>酷玩</td></tr><tr><td>米克·贾格尔</td><td>滚石乐队</td></tr><tr><td>约翰·列侬</td><td>披头士乐队</td></tr></tbody></table>

其他回答

您需要在td元素上使用填充。像这样的东西应该会奏效。当然,您可以使用顶部填充而不是底部填充获得相同的结果。

在下面的CSS代码中,大于号表示填充仅应用于td元素,这些元素是带有spaceUnder类的tr元素的直接子元素。这将使使用嵌套表成为可能。(示例代码中的单元格C和D)我不太确定浏览器是否支持直接子选择器(想想IE 6),但它不应该破坏任何现代浏览器中的代码。

/*对类spaceUnder的tr元素的直接子级td元素应用填充*/tr.spaceUnder>td{衬垫底部:1毫米;}<表><tbody><tr><td>一个</td><td>B级</td></tr><tr class=“spaceUnder”><td>C类</td><td>D)</td></tr><tr><td>电子</td><td>F级</td></tr></tbody></table>

这应该有点像这样:

+---+---+
| A | B |
+---+---+
| C | D |
|   |   |
+---+---+
| E | F |
+---+---+

因为我在桌子后面有一个背景图像,所以用白色填充来伪装是行不通的。我选择在每行内容之间放置一个空行:

<tr class="spacer"><td></td></tr>

然后使用css为间隔行提供一定的高度和透明背景。

只需将div放在td中,并设置以下div样式:

margin-bottom: 20px;
height: 40px;
float: left;
width: 100%;

行间隙的出现可以通过在应该存在下一个间隙的单元格上使用底部边框来实现,即边框底部:纯白色5px;

下面是创建屏幕截图的代码:

<style>
table.class1 {
    text-align:center;
    border-spacing:0 0px;
    font-family:Calibri, sans-serif;
}

table.class1 tr:first-child {
    background-color:#F8F8F8; /* header row color */
}

table.class1 tr > td {
    /* firefox has a problem rounding the bottom corners if the entire row is colored */
    /* hence the color is applied to each cell */
    background-color:#BDE5F8;
}

table.class1 th {
    border:solid #A6A6A6 1px;
    border-bottom-width:0px; /* otherwise borders are doubled-up */
    border-right-width:0px;
    padding:5px;
}

table.class1 th:first-child {
    border-radius: 5px 0 0 0;
}

table.class1 th.last {
    border-right-width:1px;
    border-radius: 0 5px 0 0;
}

/* round the bottom corners */
table.class1 tr:last-child > td:first-child {
    border-radius: 0 0 0 5px;
}

table.class1 tr:last-child > td:last-child {
    border-radius: 0 0 5px 0;
}

 /* put a line at the start of each new group */
td.newgroup {
    border-top:solid #AAA 1px;
}

/* this has to match the parent element background-color */
/* increase or decrease the amount of space by changing 5px */
td.endgroup {
    border-bottom:solid white 5px;
}

</style>

<table class="class1">
<tr><th>Group</th><th>Item</th><th class="last">Row</th></tr>
<tr><td class="newgroup endgroup">G-1</td><td class="newgroup endgroup">a1</td><td class="newgroup endgroup">1</td></tr>
<tr><td class="newgroup">G-2</td><td class="newgroup">b1</td><td class="newgroup">2</td></tr>
<tr><td>G-2</td><td>b2</td><td>3</td></tr>
<tr><td class="endgroup">G-2</td><td class="endgroup">b3</td><td class="endgroup">4</td></tr>
<tr><td class="newgroup">G-3</td><td class="newgroup">c1</td><td class="newgroup">5</td></tr>
<tr><td>G-3</td><td>c2</td><td>6</td></tr>
</table>

要创建行间距的错觉,请将背景色应用于行,然后使用白色创建厚边框,以便创建“空间”:)

tr 
{
   background-color: #FFD700;
   border: 10px solid white;
}