简体   繁体   English

html / css:通过表格单元格绘制边框

[英]html/css: Draw borders through a table cell

I have a very simple table: 我有一个非常简单的表:

<table>
    <tr>
        <td>col1</td><td>col2</td><td>col3</td>
    </tr>
    <tr>
        <td colspan=2>Mybigcell</td><td>col3</td>
    </tr>
</table>

What I want to achieve is to draw a border around the first column, that means around col1 and the left part of Mybigcell. 我要实现的是在第一列周围绘制边框,这意味着在col1和Mybigcell的左侧部分周围。 The border thus has to run through the middle of Mybigcell. 因此边界必须穿过Mybigcell的中间。

Is it possible? 可能吗?

You can use absolute positioned pseudo-elements to achieve this. 您可以使用绝对定位的伪元素来实现此目的。

Just use the CSS below and add class="border" to some cell. 只需使用下面的CSS,然后将class="border"添加到某些单元格即可。 Its column will obtain a border. 其列将获得边框。

Basically, it works like this: 基本上,它是这样的:

  • We will insert some absolute positioned pseudo-elements with top: 0 and bottom: 0 . 我们将插入一些绝对定位的伪元素,它们的top: 0bottom: 0 Their containing block will be the table rectangle ( position: relative ), so the pseudo-elements will grow to cover all the column. 它们的包含块将是表格矩形( position: relative ),因此伪元素将增长以覆盖所有列。
  • These pseudo-elements will be inserted at the beginning of the cells ( ::before ). 这些伪元素将插入到单元格的开头( ::before )。 Assuming left aligning inside the cells, they will be aligned at the desired position. 假设单元格内部左对齐,它们将在所需位置对齐。
  • Note they can't be aligned using left: 0 (and we can't use ::after with right: 0 neither) because the containing block is the table, not the cell. 请注意,它们不能使用left: 0对齐(并且我们不能同时使用::afterright: 0来对齐),因为包含块是表,而不是单元格。 If the containing block was the cell this would be more reliable, but the borders wouldn't fill all the column. 如果包含块是单元格,则将更可靠,但边框不会填充所有列。
  • Therefore, if a cell has a border class, a pseudo-element will be inserted in that cell (the left border), and in the following one (the right border). 因此,如果一个单元格具有border类,则将在该单元格(左边框)和下一个单元格(右边框)中插入一个伪元素。
  • But if the cell with the border class was the last one in the row, it would have no right border, because there is no following cell. 但是,如果具有border类的单元格是该行中的最后一个单元格,则它将没有右边界,因为没有后续单元格。
  • To fix that, I use the :last-child pseudo-class to detect this case, and then I insert an ::after pseudo-element with left: 100% . 为了解决这个问题,我使用了:last-child伪类来检测这种情况,然后使用left: 100%插入::after伪元素。 As mentioned above, it will be aligned relatively to the table instead of the cell. 如上所述,它将相对于表格而不是单元格对齐。 But assuming there is no missing cell in the row, that won't matter because the right edge of the cell and the right edge of the table will coincide. 但是假设行中没有丢失的单元格,那将无关紧要,因为单元格的右边缘和表的右边缘将重合。
  • Finally, I do some small adjustments using negative margins, to make it pixel perfect. 最后,我使用负边距进行了一些小调整,以使其像素完美。

 table { position: relative; /* Containing block for the borders */ border-collapse: collapse; } td { padding: 1px; padding-left: 2px; /* Increase by borderWidth */ } .border:before, .border + :before, .border:last-child:after { content: ''; /* Enable the pseudo-element */ position: absolute; /* Take it out of flow */ top: 0; /* From the top of the table... */ bottom: 0; /* ...to the bottom of the table */ border-left: 1px solid;/* This produces the border */ margin-left: -2px; /* Same as td's paddingLeft, in negative */ } .border:last-child:after { left: 100%; /* Place it at the right */ margin-left: 0; /* Remove the margin set previously */ } 
 <table> <tr> <td class="border">col1</td> <td>col2</td> <td>col3</td> </tr> <tr> <td colspan=3>Mybigbigcell</td> </tr> </table><hr /> <table> <tr> <td>col1</td> <td class="border">col2</td> <td>col3</td> </tr> <tr> <td colspan=3>Mybigbigcell</td> </tr> </table><hr /> <table> <tr> <td>col1</td> <td>col2</td> <td class="border">col3</td> </tr> <tr> <td colspan=3>Mybigbigcell</td> </tr> </table><hr /> <table> <tr> <td>col1</td> <td>col2</td> <td class="border">col3</td> <td>col4</td> </tr> <tr> <td colspan=4>Mybigbigbigcell</td> </tr> </table> 

If you want to customize the width of the borders or the paddings, see the SCSS: 如果要自定义边框或填充的宽度,请参见SCSS:

 /* Parameters */ $borderWidth: 1px; $padding: 1px; /* Code */ $sum: $borderWidth + $padding; table { position: relative; border-collapse: collapse; } td { padding: $padding; padding-left: $sum; } .border:before, .border + :before, .border:last-child:after { content: ''; position: absolute; top: 0; bottom: 0; border-left: $borderWidth solid; margin-left: - $sum; } .border:last-child:after { left: 100%; margin-left: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM