这是一个简单而优雅的解决方案,但有一些注意事项:
- 您实际上无法使间隙透明,您需要为它们赋予特定的颜色。
- 您不能将间隙上方和下方的边框转角
- 您需要知道表格单元格的内边距和边框
考虑到这一点,试试这个:
td {padding:5px 8px;border:2px solid blue;background:#E0E0E0} /* lets say the cells all have this padding and border, and the gaps should be white */
tr.gapbefore td {overflow:visible}
tr.gapbefore td::before,
tr.gapbefore th::before
{
content:"";
display:block;
position:relative;
z-index:1;
width:auto;
height:0;
padding:0;
margin:-5px -10px 5px; /* 5px = cell top padding, 10px = (cell side padding)+(cell side border width)+(table side border width) */
border-top:16px solid white; /* the size & color of the gap you want */
border-bottom:2px solid blue; /* this replaces the cell's top border, so should be the same as that. DOUBLE IT if using border-collapse:separate */
}
您实际上在做的是将一个矩形::before 块粘贴到您想要的行中所有单元格的顶部,前面有一个间隙。这些块从单元格中伸出一点,与现有边界重叠,将它们隐藏起来。这些块只是夹在一起的顶部和底部边框:顶部边框形成间隙,底部边框重新创建单元格原始顶部边框的外观。
请注意,如果表格本身以及单元格周围都有边框,则需要进一步增加“块”的水平 -ve 边距。因此,对于 4px 表格边框,您应该使用:
margin:-5px -12px 5px; /* 14px = original 10px + 2px for 'uncollapsed' part of table border */
如果您的表格使用border-collapse:separate 而不是border-collapse:collapse,那么您需要 (a) 使用完整的表格边框宽度:
margin:-5px -14px 5px; /* 14px = original 10px + 4px full width of table border */
...以及 (b) 替换现在需要出现在间隙下方的双倍宽度边框:
border-bottom:4px solid blue; /* i.e. 4px = cell top border + previous row's bottom border */
如果您愿意,该技术很容易适应 .gapafter 版本,或者创建垂直(列)间隙而不是行间隙。
这是一个代码笔,您可以在其中看到它的实际效果:https://codepen.io/anon/pen/agqPpW