Utilize z-index for overlapping dynamic divs, including those that are nested

There are two div elements nested within each other.

The first div is nested inside the second one.

To view this issue, click on the following: link to fiddle

My dilemma is that the red div (div1) shifts around within div2, and I would like div1 to disappear once it crosses the boundary of div2. Essentially, if the red div moves off the blue surface, it should vanish.

How can I accomplish this? Or where did I go wrong? I attempted enclosing both in a larger div and adjusting the z-index values so that div2 had the lowest value, div1 was in the middle, and the wrapper div had the highest. My reasoning was that the red one would disappear under the wrapper upon leaving the blue div - but this approach didn't succeed.

Currently, the red div continues moving without regard to its position within or outside the blue div.

HTML

<div id="div2">
<div id="div1"></div>
</div>

CSS

#div1{
width: 30px;
height: 30px;
background-color: red;
position: absolute;
top: 20%;
left: 20%
}
#div2{
width: 300px;
height: 300px;
background-color: blue
}

Answer №1

It appears that this is the solution you were looking for (easy to say now that I know, lol):

#div2 {
overflow: hidden;
}

Just a heads up, although #div1 may be contained within #div2, it is not actually positioned there due to the absolute property. Instead, it will adhere to the positioning of its first parent (which is not #div2) or the body element:

https://jsfiddle.net/5a1w6hd0/

Even when the parent has relative positioning (can be any type other than static):

https://jsfiddle.net/5x2bcyqe/

Additionally, the overflow property will still function regardless (unrelated issue). Setting a z-index can also be tricky - the element must have positioning in order for it to take effect.

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

The central navigation system of the Owl Carousel

I am currently using the owl carousel plugin and I am struggling to figure out how to center align the navigation vertically while also using the autoHeight option. http://codepen.io/anon/pen/jJAHL I have attempted the following code snippet, but it seem ...

The process of verifying email addresses using Angular 5

I'm having trouble validating my email with the .com domain. I've tried various methods, but so far, none have worked. Can anyone provide guidance? I'm new to Angular and struggling with this particular issue. Ideally, I want the email to be ...

Bootstrap cards that mimic a horizontal scroll but are actually static and

As I work on creating my website, I'm aiming to achieve a similar effect as shown in this example: https://codepen.io/GreenSock/pen/NWYxBKO, but using bootstrap cards instead of images. Here is the code for the bootstrap card: <div class="car ...

Looking for a way to keep the mobile ad unit fixed at the bottom of the screen

I am currently working on incorporating a 320x50 mobile ad into the mobile version of my website. The goal is to have the ad fill the entire width of a mobile device. However, the issue I'm facing is that the ad appears small and does not stretch acro ...

Arrangement of Div Box

Here is the HTML code that I am currently working with: <div class="item"> <div class="pageheader"> Header </div> <div class="info"> Created on... </div> <div class="image"> I ...

I am experiencing some issues with my AngularJS JavaScript code. The root scope is updating correctly, but the other two scopes are not working as expected. Can

My attempt at writing AngularJS JavaScript code seems to be malfunctioning. While the root scope updates properly, the other two scopes FirstCtrl and SecondCtrl do not update as expected when the root scope is updated. I would like all three scopes to upd ...

Tips for creating a Google Chart using temperature and humidity data sourced from an XML file

Below is the XML file I am working with: <data> <temp>21</temp> <humidity>17</humidity> <analog>670</analog> <analog>908</analog> <analog>628</analog> <analog>909</analog> <L ...

CakePHP allows developers to easily include images in their links using the `$this->Html->link` method. However, instead of generating HTML code for the image, it outputs ASCII characters

I can't seem to find the solution in the CakePHP 2.3.0 manual. I am trying to use $this->Html->link with $this->Html->image to create a clickable image. However, I'm encountering an issue where the ASCII rendering of quotes is being g ...

Need at least one form input field out of three to be completed

Currently, I am developing a database program where users are required to enter some of their contact information. <form action="add_action.php" method="POST"> <div class="modal-body"> Name: <br> ...

A React component that dynamically increases the padding CSS value to render its children components

Greetings! I am working on a scenario where a React component needs to pass a padding value to styled components while incrementing it based on the nested rendering level. For example, imagine a List component that must display a parent and child component ...

What is the method for adjusting the position of materialize CSS select options?

https://i.stack.imgur.com/b9p9T.png One issue arises when I open the Materialize CSS select, as the options end up covering the input. Ideally, I prefer the options to appear underneath the input. <div class="input-field "> ...

Ways to dynamically modify the CSS attributes of a webpage during the page load event

I'm looking to incorporate the background:rgba(226, 93, 141, 0.76) attribute into the body:after psuedo-element once my page has finished loading. Below is my HTML and CSS code: <style runat="server" id="myStyle" > body:after{ position:fix ...

Prevent the clustering of advertisements using jQuery

Check out my code in action here! jQuery(document).ready(function(){ var insertionTemplate = jQuery(".hiddenAdBox").eq(0).html(), insertionTarget = jQuery('ul'), insertionTargetChildren = insertionTarget.find('li'), ...

Modify the css with JQUERY when there are no rows inside the tbody section

Is it possible to change the css using jquery if there are no rows in the table body? I have attempted this but my current approach is not working. I tried adding an alert within the if statement, but the alert did not appear. My goal is to hide the table ...

function call should encompass the input content

I am currently trying to incorporate the content of a text box into a function call. Here is what I have so far: <form name="blah"> <input type="text" name="compdate" id="compdate" readonly onClick="GetDate(this);" /> <input type="button" o ...

Each time I use console.log to display a user's radio button choice, the output is consistently lagging by one step

It seems that I am experiencing an issue where the console.log output for a user's radio button selection is always one step behind. This occurs even though I have initially set the default radio button selection to "all". For example, when a user cli ...

Why doesn't jQuery UI's addClass method animate visibility like it should?

One of the advantageous features of jQuery UI is its enhancement of the jQuery addClass method, which enables animation by including a second parameter for 'duration', as shown below: $('div').addClass('someclass', 1000); Th ...

Straightening the border of the final item

In my Angular project, I'm currently implementing a progress bar with the following code: .bar { --d: 1rem; /* arrow depth */ --gap: 0.3rem; /* arrow thickness, gap */ display: flex; margin-right: var(--d); } .bar-step { flex: 1; ...

Centering a Font Awesome icon within its parent element

Below is the code I am using: <div style="width:60px; height:60px; background-color: lightgrey;"> <a class="" href="javascript:void(0)" style="color: black; width: inherit; height: inherit;"> <i class="fa fa-lg fa-play" aria-hidden="t ...

Which is better: CSS gradients or background images?

Years ago, I came across many CSS codes that used background-image: url(xxx.png) to mimic a gradient background filter, as only (please correct me if I'm mistaken) IE's proprietary filter could achieve a CSS gradient. In today's world - giv ...