【问题标题】:How to change the opacity of an child element when parent is hovered [duplicate]悬停父元素时如何更改子元素的不透明度[重复]
【发布时间】:2018-12-24 23:50:57
【问题描述】:

我想在父 div 悬停时更改子 div 图像的不透明度。如何在 CSS 中做到这一点?

<div class="parent">
    <div class="child">
        <img src="" alt="" height="" width="">
    </div>
</div>

【问题讨论】:

  • 不要告诉我你在搜索时一无所获? google.com/…
  • @TemaniAfif 没有sn-p,没有解释,并且对于这个级别的问题难度获得75分的公认答案超出了我的范围。另外,它甚至不能准确回答这个问题。 change the child div image opacity when the parent div is hovered 应该翻译成.parent:hover .child img { opacity: 0.7 }
  • @MihaiT 祝你好运,找到鼓励这种琐碎而明显的问题一次又一次被问到的支持者;)

标签: html css


【解决方案1】:

使用:hover-selector并更改img opacity

.child:hover img {
    opacity: 0.8;
}

或者如果你想改变 while 子 div 的不透明度,那么:

.parent:hover .child {
    opacity: 0.8;
}

【讨论】:

    【解决方案2】:

    小提琴链接示例 - JsFiddle

    .parent {
      width: 300px;
      height: 300px;
      background: red
    }
    .child {
      width: 100px;
      height: 100px;
      background: green
    }
    .parent:hover .child {
      opacity: .5
    }
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      .parent {
          background: red;
          padding: 30px;
      }
      
      .parent:hover .child {
         opacity: 0.3
      }
      
      .child {
          background: blue;
          padding: 30px;
      }
      <div class="parent">
          <div class="child">
              <img src="" alt="" height="" width="">
          </div>
      </div>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-07
        • 2014-12-19
        • 2018-11-28
        • 2016-07-04
        • 2011-07-01
        • 2021-12-28
        • 2017-07-03
        相关资源
        最近更新 更多