代码之家  ›  专栏  ›  技术社区  ›  Kenshinh

当父元素悬停时如何更改子元素的不透明度[重复]

  •  0
  • Kenshinh  · 技术社区  · 8 年前

    这个问题已经有了答案:

    我想在父DIV悬停时更改子DIV图像不透明度。如何在CSS中执行此操作?

    <div class="parent">
        <div class="child">
            <img src="" alt="" height="" width="">
        </div>
    </div>
    
    3 回复  |  直到 8 年前
        1
  •  6
  •   Esko    8 年前

    使用 :hover-selector 并更换IMG opacity :

    .child:hover img {
        opacity: 0.8;
    }
    

    或者,如果要更改while子DIV的不透明度,则:

    .parent:hover .child {
        opacity: 0.8;
    }
    
        2
  •  0
  •   Karuppiah RK    8 年前

    小提琴链接示例- JsFiddle

    .parent {
      width: 300px;
      height: 300px;
      background: red
    }
    .child {
      width: 100px;
      height: 100px;
      background: green
    }
    .parent:hover .child {
      opacity: .5
    }
    
        3
  •  0
  •   Adeel Kirti Nariya    8 年前

    你可以这样做:

    .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>