Is there a way to ensure that a child mimics the behavior of its parent when adjusting the opacity and visibility properties of both elements?

Apologies for the ambiguous title, as I am struggling to accurately convey the issue I am encountering. To clarify the problem, I have created a jsFiddle.

In my current layout, all four divs have opacity: 1 and visibility: visible, while divs a and b both have position:absolute with top: 0 and left: 0.

<div class="a">
  <div class="child-a"></div>
</div>
<div class="b">
  <div class="child-b"></div>
</div>

In the provided example, you can change the properties of div b to: opacity: 0 and visibility: hidden (this transition occurs over time). Once changed, only div a and its child child-a are visible. While you can click on div a, clicking on child-a actually triggers child-b.

Despite the parent of child-b being inactive to mouse events and seemingly invisible, the child itself still responds to them. Ideally, I would expect the child to be completely hidden like its parent.

How can I ensure that clicking on the area where child-a is "seen" applies to child-a as well? I want to utilize opacity transitions between showing and hiding the div, which makes using display:none impractical for transitioning purposes.

I included the display:none toggle in the example because it behaves as expected (albeit without any transition effects).

Answer №1

If you want to experiment with the pointer-events property, here is an example code snippet:

.fade-in {
    pointer-events: none;
}
.fade-in.show-me {
    pointer-events: auto;
}
.fade-in .fade-in {
    pointer-events: none;
}
.show-me .show-me {
    pointer-events: auto;
}

Check out this demo:

var x = document.getElementsByClassName("x")[0];
var y = document.getElementsByClassName("y")[0];
var z = document.getElementsByClassName("z")[0];
var p = document.getElementsByClassName("p")[0];
var q = document.getElementsByClassName("q")[0];
var childX = document.getElementsByClassName("child-x")[0];
var childY = document.getElementsByClassName("child-y")[0];

z.onclick = function () {
  if (y.classList.contains("show-me")) {
    y.classList.remove("show-me");
  } else {
    y.classList.add("show-me");
  }
};
p.onclick = function () {
  if (y.classList.contains("hide")) {
    y.classList.remove("hide");
  } else {
    y.classList.add("hide");
  }
};

x.onclick = function () { console.log("x");q.innerHTML = "x<br>"+q.innerHTML; };
y.onclick = function () { console.log("y");q.innerHTML = "y<br>"+q.innerHTML; };
childX.onclick = function (ev) {ev.stopPropagation(); console.log("child-x");q.innerHTML = "child-x<br>"+q.innerHTML; };
childY.onclick = function (ev) {ev.stopPropagation(); console.log("child-y");q.innerHTML = "child-y<br>"+q.innerHTML; };
.x,
.y {
  position: absolute;
  top: 10px;
  left: 10px;
  cursor: pointer;
}
.x:hover,.y:hover {
  box-shadow: 0 0 2px #000;
}
.x {
  background-color: #fee;
}
.y {
  background-color: #eef;
}
.child-x,.child-y {
  float: left;
  width: 80px;
  height: 80px;
  margin: 25px;
  box-shadow: 0 0 2px #000;
  cursor: pointer;
  line-height: 100px;
  text-align: center;
}
.child-x:hover,.child-y:hover {
  box-shadow: 0 0 4px #000;
}
.fade-in {
  opacity: 0;
  visibility: hidden;
  transition-delay: 0s;
  transition-duration: 200ms;
  transition-property: opacity, visibility;
  transition-timing-function: ease;
  pointer-events: none;
}
.fade-in.show-me {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}
.fade-in .fade-in {
  pointer-events: none;
}
.show-me .show-me {
  pointer-events: auto;
}
.z, .p {
  width: 240px;
  height: 40px;
  margin-top: 30px;
  cursor: pointer;
  background-color: #fff;
  margin-left: 160px;
  box-shadow: 0 0 2px #000;
  line-height: 40px;
  text-align: center;
  border-radius: 3px;
}
.z:hover, .p:hover {
  box-shadow: 0 0 4px #000; 
}
.hide {
  display: none;
}
.q {
  clear: both;
}
<div class="x fade-in show-me">
  <div class="child-x fade-in show-me">x-child</div>
</div>
<div class="y fade-in show-me">
  <div class="child-y fade-in show-me">y-child</div>
</div>

<div class="z">Toggle y opacity and visibility</div>
<div class="d">Toggle y display</div>
<div class="q"></div>

Answer №2

Implementing this with pure CSS alone may be challenging, but you can utilize JavaScript to achieve the desired outcome.

The difference between visibility: hidden and display: none is that the former hides the container while keeping the element on the page, allowing clicks to be handled by underlying elements. In contrast, the latter completely removes the element from the page.

To implement this functionality, hide the element as you are currently doing and then incorporate a setTimeout function to set the display of the parent container to none after a delay matching the transition period.

Below is the code snippet for reference:

// JavaScript code
var a = document.getElementsByClassName("a")[0];
var b = document.getElementsByClassName("b")[0];
var c = document.getElementsByClassName("c")[0];
var d = document.getElementsByClassName("d")[0];
var e = document.getElementsByClassName("e")[0];
var childA = document.getElementsByClassName("child-a")[0];
var childB = document.getElementsByClassName("child-b")[0];

c.onclick = function() {
  if (b.classList.contains("show-me")) {
    b.classList.remove("show-me");

    // Set timeout to match transition duration (200ms)
    setTimeout(function() {
      b.style.display = "none";
    }, 200);

  } else {
    b.style.display = "block";
    b.classList.add("show-me");
  }
};
d.onclick = function() {
  if (b.classList.contains("hide")) {
    b.classList.remove("hide");
  } else {
    b.classList.add("hide");
  }
};

// Additional event handling functions
a.onclick = function() {
  console.log("a");
  e.innerHTML = "a<br>" + e.innerHTML;
};
b.onclick = function() {
  console.log("b");
  e.innerHTML = "b<br>" + e.innerHTML;
};
childA.onclick = function(ev) {
  ev.stopPropagation();
  console.log("child-a");
  e.innerHTML = "child-a<br>" + e.innerHTML;
};
childB.onclick = function(ev) {
  ev.stopPropagation();
  console.log("child-b");
  e.innerHTML = "child-b<br>" + e.innerHTML;
};
.a,
.b {
  position: absolute;
  top: 10px;
  left: 10px;
  cursor: pointer;
}
.a:hover,
.b:hover {
  box-shadow: 0 0 2px #000;
}
/*Additional CSS styles*/
... (omitted for brevity) ...
.hide {
  display: none;
}
.e {
  clear: both;
}
<div class="a fade-in show-me">
  <div class="child-a fade-in show-me">a-child</div>
</div>
<div class="b fade-in show-me">
  <div class="child-b fade-in show-me">b-child</div>
</div>

<div class="c">Toggle b opacity and visibility</div>
<div class="d">Toggle b display</div>
<div class="e"></div>

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

Switch from HTML symbol to Java symbol

Is it possible to change an HTML symbol into a Java symbol? For instance, if I have &#xe000, is there a way to obtain the Java char representation like \ue000? What steps should I take to achieve this conversion? ...

Utilize jQuery to enclose nested elements within a parent element

There is a Markup presented below, where this HTML content is being generated from Joomla CMS. I am seeking a jQuery method to reorganize this HTML structure without modifying its PHP code. <div class="item column-1"> <div class="page-header"& ...

Positioning multiple images in CSS

I am facing an issue with my background images where one of them is invisible and cannot be displayed. Additionally, the padding-top for the li a element seems to not be working properly. HTML: <ul class="menu"> <li class="item-101 current a ...

Expand the range of input movement using a unique and oversized custom thumb graphic

In my project, I am creating a personalized input range feature by utilizing a 40px x 80px image as the background for the thumb. Typically, the thumb is limited to moving within the length of the track from edge to edge. However, I am aiming for the thu ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

Incorporating the fadeout technique in conjunction with the append() function

Here is some code that I am working with: $('body').on('click' , function(){ $('body').append('<div class="ajax-success"><p>Its Done !!!</p></div>').fadeOut(2000); }); The goal was to a ...

Use CSS to mimic the :hover functionality for touchscreen devices

I am struggling to get this piece of code to function correctly, $(document).ready(function() { $('.hover').bind('touchstart touchend', function(e) { e.preventDefault(); $(this).toggleClass('hover_effect'); }); ...

Display the HTML content retrieved from the SailsJS Controller

Exploring the world of SailsJS, I am on a mission to save HTML content in a database, retrieve it, and display it as rendered HTML. To achieve this goal, I have set up a sails model and a controller. This is what my model looks like: attributes: { ht ...

What is the best way to ensure the div stays in sync with scrolling?

Trying to synchronize two divs while scrolling. Essentially, I have a page scroll and want to fix the second div when it reaches the top. Please refer to the gif icon attached for clarification. You can view my progress so far here. The current issue in ...

How to Change the Appearance of Elements in an Array Using React.js

My situation involves an array receiving data from an API, and I'm looking to customize each button's appearance by giving them different background colors. In addition, my project includes Material UI... Below is the snippet of code I have - {op ...

Placing emphasis on an object that appears following a period of waiting

I'm currently working on enhancing the accessibility of a slider that will be featured on my website. The goal is to create an effect where, after clicking (or pressing enter) on the first slide, the focus shifts to the second slide element, allowing ...

How to fetch an image from a web API using JavaScript

I have recently entered the world of web development and I am facing an issue where I am trying to retrieve a Bitmap Value from a web api in order to display it on an HTML page using JavaScript. However, despite my efforts, the image is not getting display ...

What is the best way to conceal a broken image icon without sacrificing all of the stylesheet?

Can someone assist me in understanding and modifying this code? Here is the code snippet: <table> <tr> <td> <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2p ...

Transform black and white image to vibrant colors and add various hover effects

There are three images displayed on this webpage: This website is built on WordPress and utilizes the WP Bakery plugin for designing layouts. The images here are set to change from color to grayscale, and back to color upon mouseover. The following CSS c ...

Shifting a static-width table within a predetermined width container

Unable to modify the HTML code for this project, I am in need of a CSS-only solution. Dealing with an unpleasant HTML structure that is causing some issues. A side by side comparison on fiddle shows where I'm currently at in the process. The challeng ...

Drop down list not showing array values

I am attempting to populate a dropdown list with elements from an array using the Document Object Model (DOM) within a for loop. However, I keep encountering an error stating that the dropdown list is null. Here is the JavaScript code I am using: var st ...

JavaScript - Modify the proposed content prior to inserting it into the input field

In my current project, I have implemented a feature using jQuery UI - v1.11.4, where an HTML textbox utilizes a JavaScript autocomplete functionality. The suggested string for the textbox is created by concatenating several columns (patient_no, patient_nam ...

Exploring the attributes of div elements with XPath

Currently, I am in the process of familiarizing myself with xpath through the creation of a basic program that will display the list of Premier League fixtures in football from a specific webpage. The page in question can be found at the following link: A ...

How can I incorporate a vertical line divider and a legend into a curved box using HTML and CSS?

https://i.sstatic.net/dj4zb.png I have this image that I need to divide into three sections with a legend at the top, similar to the image shown. So far, this is the code I have, but I'm struggling with creating the vertical line, adding space betwe ...

Iterating through the Bootstrap grid

https://i.sstatic.net/XMIzT.jpg I'm attempting to create a layout similar to the image provided. Currently, I am utilizing WordPress and WooCommerce and aiming to showcase products in this manner. The following HTML code is what I have been working ...