我遇到过同样的问题。当鼠标悬停在整行上时,我试图突出显示整行。下面是它的css代码:
tr:hover {
outline: none;
-webkit-box-shadow: inset 0 0 10px #337AB7;
box-shadow: inset 0 0 10px #337AB7;
}
它在 Windows 7 上的 Mozilla Firefox (38.0.1) 和 Internet Explorer (11.0.9600.17801) 上运行良好。但是,在 Chrome (43.0.2357.81) 上无法运行。
因此,我不得不变通,我混合了 Sviatoslav Zalishchuk 和 David Winiecki 的答案。结果我得到:
@media screen and (-webkit-min-device-pixel-ratio:0) {
tr:hover td:first-child {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7,
inset 11px 0px 8px -10px #337AB7;
}
tr:hover td {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7;
}
tr:hover td:last-child {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7,
inset -11px 0px 8px -10px #337AB7;
}
}
tbody > tr:hover {
outline: none;
-webkit-box-shadow: inset 0 0 10px #337AB7;
box-shadow: inset 0 0 10px #337AB7;
}
这很好用,它不会破坏表格的列宽,并且仍然可以在 Mozilla 和 Explorer 上工作。
下面是一个完整的例子:
table {
box-sizing: border-box;
border-collapse: collapse;
}
td,
th {
padding-left: 10px;
padding-right: 10px;
border: 1px solid #dddddd;
font: 14px;
text-align: left;
}
/*To work only on Chrome and Safari*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
tr:hover td:first-child {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7,
inset 11px 0px 8px -10px #337AB7;
}
tr:hover td {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7;
}
tr:hover td:last-child {
box-shadow: inset 0px 11px 8px -10px #337AB7,
inset 0px -11px 8px -10px #337AB7,
inset -11px 0px 8px -10px #337AB7;
}
}
/*To work on the others browsers*/
tbody > tr:hover {
outline: none;
-webkit-box-shadow: inset 0 0 10px #337AB7;
box-shadow: inset 0 0 10px #337AB7;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Born</th>
<th>City</th>
</tr>
</thead>
<tbody>
<tr>
<td>David Gilmour</td>
<td>6 March 1946</td>
<td>Cambridge, England</td>
</tr>
<tr>
<td>Roger Waters</td>
<td>6 September 1943</td>
<td>Surrey, England</td>
</tr>
<tr>
<td>Nick Mason</td>
<td>27 January 1944</td>
<td>Birmingham, England</td>
</tr>
<tr>
<td>Richard Wright</td>
<td>28 July 1943</td>
<td>London, England</td>
</tr>
</tbody>
</table>