html - 将鼠标悬停在父元素上时如何设置子元素的样式

标签 html css hover

我正在尝试使用 .box_1 img:hover ~ .m_12 将鼠标悬停在 .box_1 img 上时设置 span .m_12 样式。但是这似乎不起作用。

<div class="col-md-4 box_1"> 
  <a href="#">
    <img src="images/pic1.jpg" class="img-responsive" alt=""/>
  </a>
  <div class="box_2">
    <div class="special-wrap">
       <div class="forclosure2">
         <span class="m_12">$140</span>
       </div>
    </div>
  </div>
</div>

最佳答案

这个

.box_1 img:hover ~ .m_12

将不起作用,因为它假定 .m_12img 的兄弟,但事实并非如此。

悬停只会影响被悬停的元素,它是后代或兄弟。

所以,你需要。

.box_1 a:hover + .box_2 .m_12

.box_1 a:hover ~ .box_2 .m_12

换句话说,.m_12 元素是 .box2 的子元素/后代,而 .box2 是链接 兄弟 a.box_1

的子项

关于html - 将鼠标悬停在父元素上时如何设置子元素的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267754/

相关文章:

javascript - 如果单击“确定”按钮,则在模式关闭后执行代码

html - 如何在两种颜色的 Bootstrap 中拆分屏幕

jquery - 当 ko 验证消息很长时如何对齐

CSS3 :hover wont work after animation

jquery 'hover mouseover' 函数跳跃

css - 带有 css 和 html 的图像上的图像描述框

html - Angular 5 如何等待 styles.css 加载

css - Firebug/Console 风格悬停效果

php - 将变量传递到 url

html - css img tint 仅在图像不可见时有效(图像覆盖色调?)