2012-11-26 57 views
2

简介:我需要显示的表像下面这样:不同的边框间距不同行

table with different spacing between head and body

用品:

  • 语义HTML编码
  • 没有脚本

HTML:

<table> 
<thead> 
    <tr> 
     <th colspan=2> 
      One 
     </th> 
     <th colspan=2> 
      Two 
     </th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td> 
      One 
     </td> 
     <td> 
      Two 
     </td> 
     <td> 
      Three 
     </td> 
     <td> 
      Four 
     </td> 
    </tr> 
    <tr> 
     <td> 
      One 
     </td> 
     <td> 
      Two 
     </td> 
     <td> 
      Three 
     </td> 
     <td> 
      Four 
     </td> 
    </tr> 
<tbody> 
</table> 

第一次尝试:

“边界崩溃属性”

我对thead试图border-collapse: separate;border-collapse: collapse;tbody但SIMP没有工作。

table { 
    border-collapse: collapse; 
    border-spacing: 1em; 
} 

table thead { 
    border-collapse: separate; 
} 

table tbody tr { 
    border-bottom: 1px solid black; 
} 

table thead tr th{ 
    border-bottom: 1px solid black; 
    padding: 10px; 
    text-align: center; 
} 

table tbody tr td { 
    border-bottom: 1px solid black; 
    padding: 10px; 
}​ 

On JSFIDDLE


第二次尝试:

“添加空白单元格”

我可以在HTML中添加空白单元格拿到表的首选外观码。但是这种方法缺乏语义结构。

table { 
    border-collapse: collapse; 
    border-spacing: 1em; 
} 

table tbody tr { 
    border-bottom: 1px solid black; 
} 

table thead tr th[colspan="2"]{ 
    border-bottom: 1px solid black; 
    padding: 10px; 
    text-align: center; 
} 

table tbody tr td { 
    border-bottom: 1px solid black; 
    padding: 10px; 
}​ 

On JSFIDDLE


其他各种attemps

我也试过在头边框-webkit-border-image: -webkit-linear-gradient(left, black 95%, white 5%);,但不能设法得到它的工作。


毕竟我接受新的建议。

注: 这将是一个电子书文件。所以一般背景颜色在不同的阅读器应用中可能有所不同

回答

2

根据您的兼容性要求,您必须使用CSS生成内容的选项:

th { 
    /* other CSS */ 
    position: relative; 
} 

thead th::before, 
thead th::after { 
    content: ''; 
    position: absolute; 
    bottom: -1px; 
    width: 0.5em; 
    border-bottom: 1px solid #fff; 
} 

thead th::before { 
    left: 0; 
} 

thead th::after { 
    right: 0; 
} 

JS Fiddle demo

为了简单起见,我已经给th元素相同::before::after,但如果总是只有两个th元素的选择可以改变:

th { 
    /* other CSS */ 
    position: relative; 
} 

thead th:first-child::after, 
thead th:last-child::before { 
    content: ''; 
    position: absolute; 
    bottom: -1px; 
    width: 0.5em; 
    border-bottom: 1px solid #fff; 
} 

thead th:last-child::before { 
    left: 0; 
} 

thead th:first-child::after { 
    right: 0; 
} 

JS Fiddle demo

+0

为何增加绝对定位生成的内容? –

+0

你需要注意定位元素直接放在表格单元格内,因为firefox没有正确定位相对于单元格的东西 –

+1

@Rémi:因为,据我所知,'padding'不会影响'border-底部的长度。这似乎是要求。 –

5

Here's my try

基本上我只是做:

table thead tr th[colspan="2"]:first-child { 
    border-right: 20px solid white; 
} 
table thead tr th[colspan="2"]:nth-child(2) { 
    border-left: 20px solid white; 
} 

注:我个人不会用这样复杂的选择,但是这应该给你的想法。

+0

谢谢你的回答。这肯定是一个好方法,但我忘了提及它应该在电子书(ePub3)中。因此,读者应用程序首选项的背景颜色可能会有所不同,并将边框颜色设置为透明可以消除魔法。 – username

+0

一般而言,我建议在旧版Internet Explorer上仔细测试这种“魔法”,您可能会希望这些版本支持。 – Paramaeleon

1

您可以通过添加白色边框来做到这一点。然后您需要从第一个和最后一个单元格关闭它们。

table thead tr th{ 
    border-left: solid 10px white; 
    border-right: solid 10px white; 
    border-bottom: 1px solid black; 
    padding: 10px; 
    text-align: center; 
} 
table thead tr th:first-child { 
    border-left: none; 
} 
table thead tr th:last-child { 
    border-right: none; 
} 

这里的时候可以垫`th`元素,而不是更新的爵士小提琴http://jsfiddle.net/davetayls/Tw5Vb/9/

+0

请不要发布重复的答案。 – Chris

+0

当我发布它时,它并不重复 –

+0

@DaveTaylor检查时间戳。 – Chris