2015-10-16 56 views
6

当表有空tbody时,Firefox不会正确呈现表格单元格边框。为什么Firefox不会使用空tbody渲染表的边界?

但是,如果您使用伪选择器tbody:empty {display:none;}来隐藏tbody元素,则按预期呈现所有内容。

jsfiddle

table { 
 
    border-collapse: collapse; 
 
} 
 
th, 
 
td { 
 
    border: 1px solid #000; 
 
} 
 

 
.fixed tbody:empty { 
 
    display: none; 
 
}
<table class="broken"> 
 
    <thead> 
 
     <tr> 
 
      <th>1</th> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
    <tfoot> 
 
     <tr> 
 
      <th>1</th> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </tfoot> 
 
</table> 
 

 
<hr /> 
 

 
<table class="fixed"> 
 
    <thead> 
 
     <tr> 
 
      <th>1</th> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </thead> 
 
    <tbody></tbody> 
 
    <tfoot> 
 
     <tr> 
 
      <th>1</th> 
 
      <td>2</td> 
 
      <td>3</td> 
 
     </tr> 
 
    </tfoot> 
 
</table>

回答

6

它最有可能属于Bug 409254Bug 217769在Firefox。注意:虽然在HTML 5中有一个空的tbody有效,但每个行组中的单元格数量应该匹配(除了使用colspan)在一个表中。

解决方法是在表格和单元格元素上分别绘制边框。

table { 
    border-collapse: separate; /*changed from collapse*/ 
    border-spacing: 0; 
    border: 1px solid; 
    border-width: 0 0 1px 1px; /*draw bottom and left borders*/ 
} 
th, 
td { 
    border: 1px solid; 
    border-width: 1px 1px 0 0; /*draw top and right borders*/ 
} 

jsfiddle

+0

好吧,谢谢你的提示。我有一个空白的tbody(它仍然是一个标签)似乎是合法的。因为,是的,它可能会发生(如果你在用户交互后用javascript填充tbody)。 – zeropaper

+0

不客气。我编辑了答案,提出了一个解决方法。 – Stickers