Z-index creates an obstacle that prevents items from being clicked on

I am facing an issue with my container div that has an inset drop shadow applied to it. Inside this container, I have child elements and I want these children to be clickable.

For the drop shadow to show, it is necessary to set the z-index of the child elements (which are images in my project) to -1. However, this causes the container to cover the child elements, making them unclickable.

Even setting pointer-events: none; on the container element did not solve the issue (I also need the container to be scrollable).

$('.item').on('click', function() {
  alert($(this).attr('id'));
});
    .container {
      position: absolute;
      width: 250px;
      height: 250px;
      border-radius: 50%;
      box-shadow: inset 0 0 10px black;
      overflow-y: scroll;
      overflow-x: hidden;
    }
    .item {
      width: 250px;
      height: 80px;
      margin: 3px 0;
      background-color: #cacaca;
      position: relative;
      text-align: center;
      z-index: -1;
      /*Shadow visible, JS doesn't work*/
      /*z-index: 0;*/
      /*Clickable, but shadow is covered */
    }
<div class="container">
  <div class="item" id="item1">
    <p>CLICK</p>
  </div>
  <div class="item" id="item2">
    <p>CLICK</p>
  </div>
  <div class="item" id="item3">
    <p>CLICK</p>
  </div>
  <div class="item" id="item1">
    <p>CLICK</p>
  </div>
</div>

JSFiddle

Is there a way to maintain the shadow effect while ensuring the child elements remain clickable?

Answer №1

Reason Behind the Issue

The occurrence of this issue can be attributed to the positioning of the divs with the class .item relative to the .container. Essentially, when users interact with what appears to be the .item, they are actually interacting with the underlying .container.

Simple yet Effective Solution

An easy fix for this problem is to enclose your div's within a wrapper as demonstrated below:

<div class="container">
    <div class="item" id="item1">
        <div><p>CLICK</p></div>
    </div>

    <div class="item" id="item2">
        <div><p>CLICK</p></div>
    </div>    

    <div class="item" id="item2">
        <div><p>CLICK</p></div>
    </div>

    <div class="item" id="item3">
        <div><p>CLICK</p></div>
    </div>
</div>

Furthermore, make sure to modify your CSS selectors from .item to .item > div, while keeping the JavaScript code unchanged.

By implementing this adjustment, the outer .item will remain visually above the .container, allowing the inner content to still be interactive and maintain the desired appearance!

Evidence of Success

To validate the effectiveness of this solution, refer to the following JS Fiddle link: https://jsfiddle.net/anik786/5oe841sw/6/

For visual confirmation, view the provided screenshot:

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

When submitting a form with the jQueryForm plugin, take action on the form by selecting it with `$(this)`

I have a situation where I have multiple forms on one page and am utilizing the jQuery Form plugin to manage them without having to reload the entire page. The issue arises when I need some sort of visual feedback to indicate whether the form submission wa ...

How to override the styling of a parent element in CSS

I'm encountering an issue with my website's sidebar. I've set the background color to yellow for elements with the currentPage class. This works fine for the 'Home' tab, but when I try to do the same for a nested tab like 'tag ...

Encountering an empty array in a PHP script when transmitting form data via AJAX using the POST method

I have been using the serialize() method to create a URL encoded string, but despite trying various solutions, nothing seems to work. Below is the code I am working with: <form id="idEditTaskForm"> <div class="form-group"&g ...

Transferring information from a form with multiple fieldsets to php

I am facing an issue with my form that is built using multiple fieldsets. The problem is related to passing values from each fieldset to welcome.php upon submitting the form. However, when I click submit, nothing happens and the values are not displayed. A ...

Turn on and off a group with a fading effect using the div

Recently, I was asked to replicate the functionality of a certain website. Here is the site in question: I attempted to understand their Javascript code, but it proved to be quite challenging. Can anyone assist me in duplicating this functionality as clos ...

Excessive CPU usage caused by a patch in jQuery dealing with regular expressions

Working on a project developed by an unknown individual has presented some challenges. Without any means of contact with this person, I noticed that the browser's CPU consumption spikes significantly upon loading the page. Upon further investigation, ...

What steps can be taken to avoid activating CORS while performing a POST request?

I am submitting form data and I do not want CORS to be triggered when I make the HTTP request. Currently, I am using jQuery's $.ajax method as follows: $.ajax({ method: "POST", url: url, data: e.serialize(), cache: false, dataTyp ...

What is the reason for Thickbox changing the href of links?

I'm running a WordPress setup with various plugins and a customized theme. The issue I'm facing is related to Thickbox, which is used on one of my pages to display picture pop-ups. The problem arises when only the first image clicked seems to wo ...

Using the jQuery Ternary Operator in Decision Statements

Can someone guide me on writing this code using a ternary operator in the most concise way possible? const isValuePresent = (jQuery('#product-options-wrapper select').val() || jQuery('#product-options-wrapper input').val()) ? true : fa ...

Styling your printed documents in Next.js 10

Within my Next 10 application, I am able to bring in global styles by importing them in _app.js in the following manner: import 'assets/css/app.css' I want to incorporate a print stylesheet as well. Instead of using a @media print { } query with ...

Tips for choosing all elements in a form that have a specific class assigned

I am attempting to target all fields in a form with a specific class name and then select all the remaining fields. This is my form: <form style="margin:20px 0" id="myform_2"> <p> Query Name : <input i ...

Enable drawing on a div element and synchronizing it across multiple divs using Javascript and HTML5

I am embarking on a project to create a website that allows users to doodle on an element and share it with others viewing the page using ajax - essentially a small-scale whiteboard feature. While I have a good grasp of HTML, CSS, Javascript (specificall ...

"Troubleshooting problems with jQuery click function during real-time search execution

In my code, I have an empty result div that dynamically fills up with item elements as search queries are entered (using jQuery ajax). If I use the following code: $(".result").click(function(){ alrert("click on whole result class captured") }); I r ...

Managing input jQuery with special characters such as 'ä', 'ö', 'ü' poses a unique challenge

Hey there, I'm running into a bit of trouble with my code and I can't figure out why it's not working. Here's a brief overview: I created my own auto-suggest feature similar to jQuery UI autosuggest. Unfortunately, I'm unable t ...

Tips on preventing the use of backslashes in JSON output from PHP

I'm encountering an issue with the code provided below. The console log is showing: "returningList{"view":0,"new":1,"random":1} result: "{\"view\":0,\"new\":1,\"random\":1}"" It seems that the JSON data is getting back ...

Exploring the contrast between window and document within jQuery

I'm curious about the distinction between document and window in jQuery. These two are commonly utilized, but I haven't quite grasped their differences. ...

What is the reason border property does not transition effectively?

I am trying to remove the border property after a certain period of time (1 second) and make it smooth. Here is what I have attempted: var elem = $('div').css({'border-top':'6px solid #FC9A24', 'border-le ...

Having trouble with the fancybox form

Take a look at this link for information on how to display a login form. Once you click on "Try now", follow these steps: A fancy box should open with fields for name and password. If both fields are left blank and the login button is clicked, an error ...

Looking for a jQuery plugin that helps with form alignment?

In a blog post comment, I came across an interesting jQuery form alignment plugin: jQuery.fn.autoWidth = function(options) { var settings = { limitWidth : false } if(options) { jQuery.extend(settings, options); }; ...

Vanishing border around the sticky table header

While working on creating a table in an excel style using html and css with a sticky header, I noticed that the borders on the table head appeared strange. Take a look at the code snippet below: table { border-collapse: collapse; position: relative ...