我正在尝试创建一个表,当您将鼠标悬停在一行上时,它会更改其背景和底部边框颜色。另外,我想将同样的边框颜色应用到它的直接兄弟姐妹身上。有没有办法可以单独使用CSS来完成?
HTML
<table>
<tbody>
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
</tr>
</tbody>
</table>
CSS
table {width: 100%;}
tr {border-top: 1px solid #e6e6e6; height: 40px;}
tr:hover {border-top: 1px solid #cadaee;}
tr:first-child {border-top: none;}
答案 0 :(得分:1)
您可以使用adjacent sibling selector,+
。
tr:hover + tr {
border-top: 1px solid #cadaee;
height: 40px;
background: #ebf4ff;
}
如果您希望在:hover
上选择这两个元素,请使用以下内容:
tr:hover + tr, tr:hover {
border-top: 1px solid #cadaee;
height: 40px;
background: #ebf4ff;
}
答案 1 :(得分:0)
table {width: 100%;}
tr {border-top: 1px solid #e6e6e6; height: 40px;}
tr:hover {border-top: 1px solid #cadaee; height: 40px; background: #ebf4ff;}
tr:hover + tr {border-top: 1px solid #cadaee;} /* Property that's applied to sibling */
tr:first-child {border-top: none;}
答案 2 :(得分:0)
Check this fiddle. This code running properly and what you expected. http://jsfiddle.net/webdevkumar/3bmmc/