The tags <td>
and <th>
are table cells, and the <tr>
are lines, in addition to them there are elements that are used as "dividers", being the <tbody>
, <tfoot>
and <thead>
Note: <th>
does not necessarily need to be inside tfoot or thead, can be used inside tbody
A <tr>
(table-Row) is a line and this line can have several cells, a <th>
or <td>
are cells, but cannot contain other table tags directly as daughter, other than the tag itself <table>
which would generate a new table.
So when you color the <tr>
in fact you are coloring the whole line, if the cells do not have color then they will be transparent and it will seem that colored everything:
.table {
width: 100%;
}
.table tr {
background: #fc0;
}
<table class="table">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
In case you only notice the edges (spacing) between the cells because the pattern of the table type is by default border-collapse: separate;
, but adjust to border-collapse: collapse;
, see the difference:
.table {
width: 100%;
border-collapse: collapse;
}
.table tr {
background: #fc0;
}
<table class="table">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
In the above examples, which will be the most recommended?
Not necessarily, if you want to color the whole line you can use both selectors, visually they will have the same effect, but as I said, one will color the cells and the other will color the line, ie it will be the "parent" element, which can have variations in the desired effects, if you want to do a zebra effect with hover
it would be easier to use directly on <tr>
.table {
width: 100%;
}
.table tr {
background: #c0c0c0;
}
.table tr:hover {
background: #fc0;
}
<table class="table">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
Of course it can achieve the same effect as tr
doing so:
.table {
width: 100%;
}
.table tr td {
background: #c0c0c0;
}
.table tr:hover td {
background: #fc0;
}
<table class="table">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
If you want to cheer up the cell phone do so:
.table {
width: 100%;
}
.table tr {
background: #c0c0c0;
}
.table td:hover {
background: #fc0;
}
<table class="table">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
This would only be more interesting if you want an effect for example with nth-child
Is there some sort of specification on that, or is it up to me?
Technically in CSS there are no predefined limitations, just understand the difference of both line and cell.
On TR if you want it to stay on the line, on TD or TH if you want it to stay on the cell ;)
– Guilherme Nascimento