如何在父元素和子元素上设置 CSS 悬停效果

2022-01-22 00:00:00 hover css

假设我有以下代码:

<div class="wrapper">
  <div class="title"></div>
  <div></div>
</div>

<div class="wrapper">
  <div class="title"></div>
  <div></div>
</div>

<div class="wrapper">
  <div class="title"></div>
  <div></div>
</div>

悬停 wrapper 我希望它是子 title div 来更改它的背景颜色.我可以在纯 CSS 中做到这一点吗?在 js 中,我知道该怎么做.我有兴趣是否存在纯 CSS 方法?

On hover for a wrapper I want it's child title div to change it's background color. Can I do this in pure css. In js I know how to do it. I'm interested if a pure CSS method exists for this?

谢谢

推荐答案

是的,你可以这样做.只是:

Yes, you can do this. It's just:

.wrapper:hover .title {
  background-color: #f00;
}


请注意,IE6 仅在 a 元素上识别 :hover,所以这不起作用 - 但我希望你不必弄乱那个蹩脚的旧东西.


Please note that IE6 recognizes :hover only on a-elements, so this won't work - but i hope you don't have to mess with that crappy old thing.

相关文章