0

I have came to a problem when trying to change text opacity to 1 when hovering over an image. Initial opacity value of text is 0. So when I hover over profile_picture image, text opacity should be 1, but problem is it doesn't changes text opacity.

 .account_image{
        margin-top: 5px;
        margin-bottom: 5px;
    
    }
    
    .profile_picture{
            width: 60px;
            height: 60px;
            object-fit: cover;
            border-radius: 50%;
            margin-left: 65px;
            filter: brightness(100%);
            cursor: pointer;
    }
    
    
    .t_overlay{
        position: absolute;
        margin-left: 73px;
        margin-top: -43px;
        font-size: 10px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        line-height: 1.2;
        -moz-user-select: none;
      -khtml-user-select: none;
      -webkit-user-select: none;
        cursor: pointer;
      opacity: 0;
        color: white;
        font-family: 'Quicksand', sans-serif;
        pointer-events: none;
    }
    
    .profile_picture:hover{
        filter: brightness(70%);
        transition: filter 0.25s ease;
    }
    
    .profile_picture:hover .t_overlay{
        opacity: 1;
    }
<div class="account_image">
  <img src="img/profile_p.jpg" class="profile_picture">
  <div class="t_overlay">
    <div class="change_c">CHANGE</div>
    <div class="avatar_a">AVI</div>
  </div>
</div>
    
       
zerbene
  • 1,249
  • 2
  • 7
  • 21

3 Answers3

1

the issue is your css selector .profile_picture:hover .t_overlay should be .profile_picture:hover + .t_overlay or .profile_picture:hover ~ .t_overlay

the selector you wrote selects a children of profile_picture, but you want a sibling

see https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator and https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_combinator

Tiago Coelho
  • 5,023
  • 10
  • 17
0

I know this problem. You cant just hover and activate a class which is a sibling. You need a selector property for this. Like +

Here is a fiddle with the Fixed version:

.account_image{
  margin-top: 5px;
  margin-bottom: 5px;
}

.profile_picture{
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 50%;
  margin-left: 65px;
  filter: brightness(100%);
  cursor: pointer;
  position: relative;
  z-index: 1;
}

.t_overlay{
  position: absolute;
  margin-left: 73px;
  margin-top: -43px;
  font-size: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  line-height: 1.2;
  -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
  opacity: 0;
  color: white;
  font-family: 'Quicksand', sans-serif;
  pointer-events: none;
  z-index: 2;
}

.profile_picture:hover{
  filter: brightness(70%);
  transition: filter 0.25s ease;
}

.profile_picture:hover + .t_overlay{
  opacity: 1;
}
<div class="account_image">
  <img src="https://source.unsplash.com/random" class="profile_picture">
  <div class="t_overlay">
    <div class="change_c">CHANGE</div>
    <div class="avatar_a">AVI</div>
  </div>
</div>

If you want to learn more about CSS Siblings: MDN Article

koby
  • 31
  • 5
0

Your selector .profile_picture:hover .t_overlay is not correct, as the overlay is a sibling to the image, not a child. Just add a + operator between the two, like:

.profile_picture:hover + .t_overlay {

Demo:

    .account_image{
    margin-top: 5px;
    margin-bottom: 5px;

}

.profile_picture{
        width: 60px;
        height: 60px;
        object-fit: cover;
        border-radius: 50%;
        margin-left: 65px;
        filter: brightness(100%);
        cursor: pointer;
}


.t_overlay{
    position: absolute;
    margin-left: 73px;
    margin-top: -43px;
    font-size: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    line-height: 1.2;
    -moz-user-select: none;
  -khtml-user-select: none;
  -webkit-user-select: none;
    cursor: pointer;
  opacity: 0;
    color: white;
    font-family: 'Quicksand', sans-serif;
    pointer-events: none;
}

.profile_picture:hover{
    filter: brightness(70%);
    transition: filter 0.25s ease;
}

.profile_picture:hover + .t_overlay{
    opacity: 1;
}
<div class="account_image">
    <img src="https://via.placeholder.com/150" class="profile_picture">
    <div class="t_overlay">
      <div class="change_c">CHANGE</div>
      <div class="avatar_a">AVI</div>
    </div>
    </div>
Anis R.
  • 6,656
  • 2
  • 15
  • 37