有没有办法使用CSS在表格中仅在列之间(而不是在外边缘)显示边框?
Answers:
编辑2
伊拉斯mus(Erasmus)在下面有更好的单线
并非没有棘手的CSS选择器和额外的标记等。
可能会这样(使用CSS选择器):
table {
border:none;
border-collapse: collapse;
}
table td {
border-left: 1px solid #000;
border-right: 1px solid #000;
}
table td:first-child {
border-left: none;
}
table td:last-child {
border-right: none;
}
编辑
为了澄清@jeroen的评论打击,您真正需要的是:
table { border: none; border-collapse: collapse; }
table td { border-left: 1px solid #000; }
table td:first-child { border-left: none; }
:last-child
。
我知道这是一个古老的问题,但是有一个简单的单行解决方案可以在Chrome,Firefox等以及IE8及更高版本上始终如一地工作(并且大部分情况下也可以在IE7上工作-请参见http: //www.quirksmode.org/css/selectors/了解详情):
table td + td { border-left:2px solid red; }
输出是这样的:
Col1 | Col2 | Col3
使这项工作的原因是,您仅在与另一个表单元格相邻的表单元格上定义了边框。换句话说,您要将CSS应用于行中除第一个单元格之外的所有单元格。
通过将左边框应用到第二个到最后一个孩子,它使线条看起来像在单元格之间。
桌子上的边框总是有点片状。一种可能性是在每个表单元格中添加最右边的声明,最右边一列中的声明除外。如果您使用任何形式的表间距,则此方法将无法很好地工作。
另一种选择是使用内部带有边框的1px高背景图像,但这仅在您可以始终保证每个单元格的宽度的情况下才有效。
另一种可能性是尝试使用colgroup / col。上次查看此功能时,它对跨浏览器的支持相当糟糕,但此后可能会有所改善:http : //www.webmasterworld.com/forum83/6826.htm
我可能是在简化问题,但是td {border-right:1px solid red;}是否可用于您的表格设置?
请参阅表格的rules
属性-https: //www.w3.org/TR/html401/struct/tables.html#h-11.3.1
取得一个具有类名的表,column-bordered-table
然后将其添加到CSS之下。这也适用于bootstrap
表
.column-bordered-table thead td {
border-left: 1px solid #c3c3c3;
border-right: 1px solid #c3c3c3;
}
.column-bordered-table td {
border-left: 1px solid #c3c3c3;
border-right: 1px solid #c3c3c3;
}
.column-bordered-table tfoot tr {
border-top: 1px solid #c3c3c3;
border-bottom: 1px solid #c3c3c3;
}
参见下面的输出
N:B您必须根据需要添加表标题backgorund颜色
:last-child
(:first-child
根据quirksmode.org/css/contents.html的要求