The tags <td>
and <th>
are table cells, and <tr>
are lines, besides there are elements that are used as "dividers", being <tbody>
, <tfoot>
and <thead>
-
<th>
table header cell indicates that it should contain a text or a description
-
<td>
table data cell indicates that it should contain data
Note: <th>
does not necessarily have to be inside tfoot or thead, it can be used inside tbody
A <tr>
(table-row) is one line and this row can have multiple cells, a <th>
or <td>
are cells, but can not contain other table tags directly as a child, other than the <table>
tag itself, which would generate a new table. / p>
So when you color <tr>
you are actually coloring the whole line, if the cells are colorless then they will be transparent and it will look like you 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 borders between the cells because the default of the table type is by default it is border-collapse: separate;
, but if you set 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 examples above, which one will be the most recommended?
Not necessarily, if you want to color the entire line you can use both selectors, visually they will have the same effect, but it's like said, one will color the cells and the other will color the line, ", which can have variations in the desired effects, if you want to make a zebra effect with hover
it would be easier to use directly in <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 you 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 animate the hover in the cell 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 eg with nth-child
Is there any kind of specification for this, or is it at my discretion?
Technically in CSS has no pre-defined limitations, just understand the difference of both line and cell.