What is the best way to style an image with CSS when it is not contained within a disabled parent element?

Struggling to find a way to add a hover effect to an image that is not inside a disabled element? Although it seems like the css selector below should do the trick, it doesn't quite work as expected. In this scenario, I only want the hover effect on examples #2 and #3, excluding #1 because it is within 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.sstatic.net/3ATqJ.png">
</button>

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

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

Answer №1

Try utilizing the disabled attribute selector => [disabled] minus the everything selector within it. *:not([disabled])

Additionally, incorporate the child selector > to specify that 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.sstatic.net/3ATqJ.png">
</button>

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

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

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

Answer №2

One approach is to reverse the process: apply hover effects to all images and then remove them when inside a disabled element. This allows for more versatile 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.sstatic.net/3ATqJ.png">
</button>

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

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

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

However, you may not need to cover every possibility, so you could refine your selector. For example, if all examples have a name attribute, you could use:

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.sstatic.net/3ATqJ.png">
</button>

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

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

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery will envelop the HTML elements in an inconsequential div

Imagine a website that is visually complex, with various styles and images positioned in different ways. What if we wanted to add a small overlay icon above each image? At first, the solution might seem simple - just use absolute positioning for a span el ...

Tips for activating just a single event, whether it's 'change' or 'click'

I am facing an issue with a number input tag that has two event listeners 'on-click' and 'on-change'. Whenever I click on the arrow, both event listeners get triggered. I want only one event to trigger first without removing any of the ...

Passing JSON information from a website to a Node.js server

<script type="text/javascript" src="data.json"></script> var mydata = JSON.parse(data); data = '[{"yer" : "Besiktas", "lat" : "41.044161", "lng" : "29.001056"},{"yer" : "Eminönü", "lat" : "41.017513", "lng" : "28.970939"},{"yer" : "Zeyt ...

The separator falls short of spanning the entire width of the page

For some reason, I can't seem to make the divider extend to the full length of the page. <TableRow> <TableCell className={classes.tableCell} colSpan={6}> <Box display="grid" gridTemplateColumn ...

<Select> Placeholder customization

For my react project, I am utilizing material-Ui and I am looking to have a placeholder that stands out by having grey text instead of black when compared to the selected item. <Select name="answer" value={values.answer} onChange={handleChange} onBlur= ...

Access an image URL that is saved in a MySQL database

Currently, I am facing a challenge attempting to populate an img tag src attribute with a filename sourced from a mysql database. As of now, I retrieve two images through a single MySQL query (selected randomly). However, in the future, I anticipate needi ...

Reveal each element individually upon clicking the button

I am trying to display 5 div elements one by one when clicking a button, but the current code is not working. I am open to alternative solutions for achieving this. Additionally, I want to change the display property from none to flex in my div element. ...

Cut out a passage by using a polyline shape

I am looking to create a unique effect by clipping a text like a heading1 using an svg polyline. The concept is to place the H1 text behind the polyline background and make it appear frosted or blurred. I have successfully achieved this in the past but see ...

Selecting options from a pop-up menu

I've created a dropdown menu with what seems to be correct code. However, there are no errors showing up, but the selected item from the menu is not alerting as expected. Here is the jsfiddle link $(document).ready(function () { // This function ...

Deactivating a form field depending on a selected radio button in Angular 2

If I have two radio buttons, with a click function called localClick for the first button to give value 1 and the second button to give value 2. <div class="ui-g-12"><p-radioButton name="group1" value="Local" (click)=localClick(1) label="Local"&g ...

Interactive Thumbnail Selection for HTML5 Video

Having trouble with creating thumbnails? I managed to solve the cross-domain issue using an html2canvas PHP proxy. No error messages in the Console, but the thumbnails are unfortunately not showing up - they appear transparent or white. Here is a snippet ...

Why is the camera access permission dialog not showing up in the WeChat app?

Why is it that when I attempt to open the following links for the first time, Chrome asks for camera permission, but when I access them through WeChat's in-app browser, the camera permission dialog does not appear? Can someone shed some light on this ...

Divs are being obstructed by the navigation bar

I am seeking a solution to prevent div elements from being positioned below my navigation/information bar when the screen width is minimized. The information bar has a height of 1200px and I would like it to stay on the left side without any overlapping fr ...

Having trouble understanding the differences between Bootstrap 4's .list-inline class and .list-inline-item class?

I am currently utilizing Bootstrap 4 within Wordpress. Strangely, I am facing an issue where I am unable to have list items appear inline (horizontally) solely by using the class .list-inline on the list itself, as shown below: <ul id="dances" class="l ...

Unable to adjust the width of a datatable using the jQuery Datatable plugin version 10.1

I am facing a challenge with multiple datatables using the jQuery plugin on my page. Some of the datatables fit perfectly within the page width, while others extend beyond the page boundaries. Despite trying various methods, including setting styles in HTM ...

Having difficulty handling text overflow in Angular4 and HTML

I'm facing an issue with displaying a very large text in a table. Despite trying various attributes such as - { text-overflow: clip; } { text-overflow: ellipsis; } { text-overflow: ellipsis-word; } { text-overflow: "---"; } { text-overflow: ellip ...

Turn off the extra space inserted by DataTables

Help needed with centering table header text. <table class="table table-bordered table-hover center-all" id="dataTable"> <thead> <tr> <th scope="col" style="text-align: center">Nam ...

Modal - sending data through URL

I am trying to create a modal that will open another view using a URL in script, but instead it just opens a blank pop-up and doesn't redirect to the URL. I have attempted various approaches, but none seem to be working. Adding <div class="modal-b ...

I'm having trouble grasping the concept of margin collapse in this particular example

It's clear why the margin collapses to 20px in the first example when there's a border on the outer div. However, it is puzzling how the margin collapses to zero in the second example just by removing the border from the outer div. /*First exam ...

Discover the best method for transferring MySQL data between pages

Can anyone help guide me in the right direction on how to allow a user to click a link within a PHP While Loop generated MySQL table and pass that data to another page? I've searched through similar questions but haven't found a solution that fit ...