[英]Fill entire table row in CSS
How can I fill (with background-color
) an entire table row when I have a table with different numbers of columns in each row? 当我的表中每一行的列数不同时,如何填充整个表行(
background-color
)?
I have a table like this: 我有一张这样的桌子:
But I want to color the entire line to the end, like this (with the same color, not yellow): 但是我想像这样将整行着色到最后(用相同的颜色,而不是黄色):
Now, I am trying to do it like this: 现在,我正在尝试这样做:
tr:nth-child(2n+1) {
background-color: rgb(240, 240, 240);
}
You need to set colspan
on the last td
for row 1 and 2, or else it won't stretch to full table width. 您需要在第1行和第2行的最后一个
td
上设置colspan
,否则它将不会延伸到整个表格宽度。
table, body { margin-top: 0px; padding-top: 0px; text-align: top; border-collapse: collapse; } td#naglowek { font-family: verdana; font-size: 14px; font-weight: bold; text-align: left; background-color: black; color: white; } td:first-child { font-family: verdana; font-size: 10px; font-weight: bold; text-align: left; } tr:nth-child(2n+1) { background-color: Red; } td { font-family: verdana; font-size: 10px; text-align: left; padding-top: 2px; padding-bottom: 2px; } th { padding-top: 10px; font-family: verdana; font-size: 9px; font-weight: bold; border-bottom: solid 0px black; text-align: left; }
<table> <tr> <td>Imie</td> <td colspan='3'>Wartosc</td> </tr> <tr> <td>Nazwisko</td> <td colspan='3'>Wartosc</td> </tr> <tr> <td>Adres</td> <td><b>Kraj</b></td> <td><b>Województwo</b></td> <td><b>Miejscowość</b></td> </tr> <tr> <td></td> <td>Wartość</td> <td>Wartość</td> <td>Wartość</td> </tr> </table>
Update 更新
Based on your comments where a solution without the need of empty td
and colspan
and after some thinking, I came up with this trick, which could be an alternative, though you need to test it in all major browsers to make sure it works (I tested it on Windows using Chrome, FF, Edge, IE11 with success). 根据您的意见,即无需空
td
和colspan
的解决方案,并经过一番思考,我想出了这个技巧,尽管您需要在所有主流浏览器中进行测试以确保其有效,但我还是可以选择这种技巧(我已成功在Windows上使用Chrome,FF,Edge,IE11对它进行了测试)。
table, body { margin-top: 0px; padding-top: 0px; text-align: top; border-collapse: collapse; } td#naglowek { font-family: verdana; font-size: 14px; font-weight: bold; text-align: left; background-color: black; color: white; } td:first-child { font-family: verdana; font-size: 10px; font-weight: bold; text-align: left; } td { font-family: verdana; font-size: 10px; text-align: left; padding-top: 2px; padding-bottom: 2px; } th { padding-top: 10px; font-family: verdana; font-size: 9px; font-weight: bold; border-bottom: solid 0px black; text-align: left; } /* begin - fix for full width background color */ table { overflow: hidden; } tr:nth-child(2n+1) td:first-child { position: relative; } tr:nth-child(2n+1) td:first-child:after { content: ' '; position: absolute; left: 0; top: 0; bottom: 0; width: 100vw; background-color: red; z-index: -1; } /* end - fix for full width background color */
<table> <tr> <td>Imie</td> <td>Wartosc</td> </tr> <tr> <td>Nazwisko</td> <td>Wartosc</td> </tr> <tr> <td>Adres</td> <td><b>Kraj</b></td> <td><b>Województwo</b></td> <td><b>Miejscowość</b></td> </tr> <tr> <td></td> <td>Wartość</td> <td>Wartość</td> <td>Wartość</td> </tr> </table>
You can simply add the colspan
attribute to your table rows where the rows have less than the full number of cells. 您可以简单地将
colspan
属性添加到表行,其中行数少于单元格的总数。 For example <td colspan="3">Wartosc</td>
: 例如
<td colspan="3">Wartosc</td>
:
table, body { margin-top: 0px; padding-top: 0px; text-align: top; border-collapse: collapse; } td#naglowek { font-family: verdana; font-size: 14px; font-weight: bold; text-align: left; background-color: black; color: white; } td:first-child { font-family: verdana; font-size: 10px; font-weight: bold; text-align: left; } tr:nth-child(2n+1) { background-color: Red; } td { font-family: verdana; font-size: 10px; text-align: left; padding-top: 2px; padding-bottom: 2px; } th { padding-top: 10px; font-family: verdana; font-size: 9px; font-weight: bold; border-bottom: solid 0px black; text-align: left; }
<table> <tr> <td>Imie</td> <td colspan="3">Wartosc</td> </tr> <tr> <td>Nazwisko</td> <td>Wartosc</td> </tr> <tr> <td>Adres</td> <td><b>Kraj</b> </td> <td><b>Województwo</b> </td> <td><b>Miejscowość</b> </td> </tr> <tr> <td></td> <td>Wartość</td> <td>Wartość</td> <td>Wartość</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.