Reputation: 674
#MyTable tr+tr:hover {
background: #dfdfdf;
}
<table id="myTable">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>X</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
<td>X</td>
</tr>
</table>
I managed to hover row 2 and 3 but while hover on 2 and 3, how can I skip the td (X). Preferable not using jQuery selector.
Upvotes: 30
Views: 65052
Reputation: 2463
I found this SO link because I did not want my header row to highlight when I hovered over it. The solution I came up with was to use <thead>
and <tbody>
tags and apply css to the rows within <tbody>
.
<style>
.report tbody tr:hover {
background-color: khaki;
}
</style>
<table class="report">
<thead>
<tr>
<th>Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Value</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Total</td>
</tr>
</tfoot>
</table>
Upvotes: 2
Reputation: 31
:not(:first-child)
and :not(:last-child)
should do the trick, it seems those are supported by all latest browser.
If you need IE<9 support you can add classes, or replace td
's with th
's
Upvotes: 1
Reputation: 16115
Use the :not(), :first-child and :last-child.
#myTable tr:not(:first-child):hover td:not(:last-child) {
background: #dfdfdf;
}
Also see this example.
Upvotes: 59
Reputation: 22728
If you're looking to not apply something to the first and last children, you can use the :first-child
and :last-child
selectors to override the styles.
#MyTable tr, tr:hover {
background: #dfdfdf;
}
#MyTable tr:first-child, tr:last-child {
background: #000;
}
This won't work in IE until IE 9 though.
Unless you shim in some support.
Upvotes: 3