How can I apply css to an img that is not inside a disabled parent?

Viewed 76

I haven't found an effective way to apply a hover effect on an image, but only if it is not inside a disabled element. I would think this css selector would work, but it doesn't appear to. In this example, I'd want the hover effect on examples #2 and #3, but not #1, because it is in a disabled button.

*:not(*:disabled) img.action:hover {
    border-radius: 35%; 
    border: 1px;
    background: red;
}
<button name="example#1" class="action" type="button" 
 value="Edit"  disabled>
   <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</button>

<span name="example#2">
   <a href="index.php">
      <img class="action" src="https://i.stack.imgur.com/3ATqJ.png" >
   </a>
   </span>

<div name="Example#3">
   <img class="action" src="https://i.stack.imgur.com/3ATqJ.png" >
</div> 

2 Answers

Use the disabled attribute selector => [disabled] without the everything selector inside. *:not([disabled])

Also add the child selector > to indicate that the img.action.hover is a child of *:not([disabled]) ==> *:not([disabled]) > img.action:hover

*:not([disabled]) > img.action:hover {
  border-radius: 35%;
  border: 1px;
  background: red;
}
<button name="example#1" class="action" type="button" value="Edit" disabled>
   <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</button>

<span name="example#2">
   <a href="index.php">
      <img class="action" src="https://i.stack.imgur.com/3ATqJ.png" >
   </a>
   </span>

<div name="Example#3">
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</div>

<span name="Example#4">
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</span>

You could do the opposite: set the hover for all images and then remove it when inside a disabled element. This allows for the most generic selectors.

img.action {
  width: 60px;
  height: 60px;
}

img.action:hover {
  border-radius: 35%;
  border: 1px;
  background: red;
}

[disabled] img.action:hover {
  border-radius: initial;
  border: initial;
  background: initial;
}
<button name="example#1" class="action" type="button" value="Edit" disabled>
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</button>

<span name="example#2">
  <a href="index.php">
    <img class="action" src="https://i.stack.imgur.com/3ATqJ.png" >
  </a>
</span>

<div name="Example#3">
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</div>

<button name="example#4" class="action" type="button" value="Edit" disabled>
  <div>
    <span>
      <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
    </span>
  </div>
</button>

 

However, in reality you probably don't have to account for all the possibilities under the sun, so you could narrow your selector slightly. For instance, since all your examples have a name attribute, you could do something like:

img.action {
  width: 60px;
  height: 60px;
}

[name]:not([disabled]) img.action:hover {
  border-radius: 35%;
  border: 1px;
  background: red;
}
<button name="example#1" class="action" type="button" value="Edit" disabled>
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</button>

<span name="example#2">
  <a href="index.php">
    <img class="action" src="https://i.stack.imgur.com/3ATqJ.png" >
  </a>
</span>

<div name="Example#3">
  <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
</div>

<button name="example#4" class="action" type="button" value="Edit" disabled>
  <div>
    <span>
      <img class="action" src="https://i.stack.imgur.com/3ATqJ.png">
    </span>
  </div>
</button>

Related