Search code examples
css

Remove CSS filter on child elements


I have problem with my css code.

As you see I have filter on li element, but it's overlays other elements. I need to make other two elements had no filter:

Is there any possibility to do so.

.main {
  width:300px;
  height:300px;
  background-color:blue;
  list-style:none;
}

.button {
    position: absolute;
    top: 35% ;
    height: 30px;
    width: 120px;
    display: none;
    z-index: 99;
    background-color:black;
    color:white;
}

.icon {
    width: 30px;
    position: absolute;
    z-index: 99;
    display: none;
    width:100px;
}

.main:hover > .button {
    display: block;
    filter: none;
}

.main:hover > .icon {
    display: block;
    filter: none;
}

.main:hover {
    filter: brightness(50%);
    transition: 0.5s;
}
<li class="main">
<img src="https://help.seesaw.me/hc/en-us/article_attachments/204081043/bear.png" class="icon" />
<a class="button" href="#">Button</a>
</li>


Solution

  • You can't do it that way. Children are affected by their parent style. That's how Cascading Style Sheets works.

    I suggest you to use a pseudo-element to make it work like you want, so that only the pseudo-element would be affected.

    See comments in the snippet:

    .main {
      position: relative;   /* Added */
      width: 300px;
      height: 300px;
      list-style: none;
    }
    
    .main::before {   /* Added */
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background-color: blue;
      transition: 0.5s;
    }
    
    .button {
      position: absolute;
      top: 35%;
      height: 30px;
      width: 120px;
      display: none;
      z-index: 99;
      background-color: black;
      color: white;
    }
    
    .icon {
      width: 30px;
      position: absolute;
      z-index: 99;
      display: none;
      width: 100px;
    }
    
    .main:hover>.button {
      display: block;
      /* filter: none; Commented */
    }
    
    .main:hover>.icon {
      display: block;
      /* filter: none; Commented */
    }
    
    .main:hover::before {   /* Modified */
      filter: brightness(50%);
    }
    <li class="main">
      <img src="https://help.seesaw.me/hc/en-us/article_attachments/204081043/bear.png" class="icon" />
      <a class="button" href="#">Button</a>
    </li>