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

Adjusting the Size and Spacing of Vuetify's Buttons

Why are buttons extending beyond the card width? https://i.stack.imgur.com/wVRoQ.png I'm facing difficulties customizing the buttons within the cards. I want to eliminate all padding so that the black border perfectly encloses the icon without any un ...

Every time I adjust the browser height on my landing page, the elements start to overlap each other

Something strange is happening - as I shorten the height of the browser, elements on the page start to overlap. I've tried using Firebug to troubleshoot, but haven't had any luck so far. Maybe someone here can help me out! EDIT: It seems that e ...

Is Ursina the right choice for web development?

Looking for a method to compile Ursina for HTML5 using WebAssembly or another technology? I am seeking to make my Ursina Game compatible with Linux & Mac (and potentially Android/iOS with webview), but the current cross-platform compilation options for Py ...

Instructions on implementing getJSON in this code

I recently set up a chained select box with JSON data to populate options, using this Fiddle. While the data is hardcoded for now, I am looking to load it using the $.getJSON() method. However, despite my efforts, I haven't been able to get the code r ...

Execute MySQL query when the content of an HTML text field changes

My goal is to check if the value entered in an input text box matches any rows in a database. I want the DB to be searched when text is typed into the input field. <input type='text' name='barcode'> I would like it to search the ...

change visibility:hidden to visible in a css class using JavaScript

I've put together a list of Game of Thrones characters who might meet their demise (no spoilers included). However, I'm struggling with removing a CSS class as part of my task. Simply deleting the CSS is not the solution I am looking for. I' ...

Using jQuery to store the last selected href/button in a cookie for easy retrieval

Recently, I've been working on a document that features a top navigation with 4 different links. Each time a link is clicked, a div right below it changes its size accordingly. My goal now is to implement the use of cookies to remember the last select ...

Tips for Changing Background Color Beyond Body Tag

When using my demo below on Safari for iOS and scrolling up and down, you'll notice that you can scroll outside of the body until removing your finger from the touchscreen. The background color outside of the body is white. Is there a way to change th ...

What is the best way to preload all videos on my website and across different pages?

I specialize in creating video websites using HTML5 with the <video> tag. On my personal computer, the website transitions (fadeIn and fadeOut) smoothly. However, on my server, each page seems to take too long to load because the videos start preloa ...

Determining in Angular 8 whether a value has been altered by a user or by a method call

Within my select element, the value is currently being assigned through an ngOnInit call. Here is an example of the HTML code: <select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> <option *ngFor="l ...

The absolute CSS dropdown isn't visible, but the relative one is displaying properly

Currently, I am in the process of creating a CSS dropdown menu. The main dropdown (the first ul) has a position set to relative, while the second dropdown has its position as absolute. However, using absolute causes this issue: On the other hand, settin ...

What is the best method to ensure that an image remains at the bottom of a div even as the div's height adjusts?

In my current project, I am facing a challenge with positioning an image in the bottom-right corner of a dynamically changing div. Initially, this is easily achieved by using CSS selectors: #div1 { position: relative; } #div1 img { position: abso ...

The process of masking a video with alpha data from another video on canvas seems to be experiencing a

I have a unique setup on my page where I'm masking one video with another. Essentially, when the Play button is pressed, a second video slowly appears over the looping video in the background. This effect is achieved by using a black/white mask transf ...

"Triggering CSS changes with the click of a

I am developing a basic calendar application in PHP and I would like to dynamically change the style of the <td> block based on user interactions. Specifically, I want to implement a behavior where once the "menuclick" class is active, the other cl ...

Adjust the dimensions of the canvas without disrupting the existing artwork

I am currently working on a pixel art application where I am facing an issue with saving images from the canvas. The saved image appears small and pixelated when I try to resize it. I would like to resize the image to larger dimensions, but I am unsure of ...

Creating a custom dynamic favicon and title in NextJS

Hello there! I am in the process of creating a web constructor. Currently, my application functions as follows: I verify the URL that the user is visiting (such as localhost:3000) I identify their project name within my web constructor (localhost:3000 -&g ...

What strategies can be implemented to enhance the performance of this jQuery slider?

I have successfully created an image carousel. Encountered Challenges: The last slide is displayed, but the counter shows the first slide. How can I resolve this issue? When clicking play, it continues to advance the count. Is there a way to make it ...

The dropdown menu is being truncated

I am having an issue with a drop-down menu being cut off by its parent div. When I increase the height of the parent div, the drop-down menu becomes visible. Can someone please assist me? Below is my code snippet: MarkUp <div id="main-navigation" clas ...

When the onLeave event of fullPage.js is activated

I am struggling to implement two CSS events when transitioning between sections on a fullPage.js application. To see my current progress, you can view the following fiddle: http://jsfiddle.net/pingo_/67oe1jvn/2/ My objectives are as follows: To modify t ...

Challenges associated with Bootstrap toggle switch

I am having some difficulties with using bootstrap switch. I have tried the examples provided on , but regardless of the classes I apply, the buttons remain the same small size and the text for ON and OFF is barely visible. I can't seem to figure out ...