Unable to intersect sibling elements

I'm having an issue with positioning in CSS. I want to place the .sibling-child element above the .parent element without changing the current z-index values. Is there a way to accomplish this?

Check out my jsfiddle demonstrating the problem: http://jsfiddle.net/8hb6xgLj/1/

.parent {
  width: 300px;
  height: 100px;
  background: #333;
  position: relative;
  z-index: 10;
}

.child {
  top: 60px;
  position: absolute;
  width: 50px;
  height: 80px;
  background: red;
}

.sibling {
  width: 300px;
  height: 100px;
  background: #ccc;
  position: relative;
  z-index: 9;
}

.sibling-child {
  top: 10px;
  position: absolute;
  width: 70px;
  height: 80px;
  background: blue;
}
<div class="parent">
  <div class="child">

  </div>
</div>
<div class="sibling">
  <div class="sibling-child">

  </div>
</div>

Answer №1

When two div elements are positioned with different z-index values, they are stacked in the order they appear in the markup. The element that is declared first will have the highest stacking index.

As a result of this definition, it is unlikely that .sibling-child will ever be displayed above .parent.

For further details, refer to the W3C specification on stacking context. Pay special attention to point 9:

Stacking contexts are created by positioned descendants with z-indices greater than or equal to 1 in z-index order (smallest first) then tree order.

Answer №2

This is the only solution that comes to mind.

transform: translate3d(0, -100px, 20px);

.container {
  width: 300px;
  height: 100px;
  background-color: #333;
  position: relative;
  z-index: 10;
}

.box {
  top: 60px;
  position: absolute;
  width: 50px;
  height: 80px;
  background-color: red;
}

.wrapper {
  width: 300px;
  height: 100px;
  background-color: #ccc;
  position: relative;
  z-index: 9;
}

.inner-box {
  top: 10px;
  position: absolute;
  width: 70px;
  height: 80px;
  background-color: blue;
  transform: translate3d(0, -100px, 20px);
}
<div class="container">
  <div class="box">

  </div>
</div>
<div class="wrapper">
  <div class="inner-box">

  </div>
</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

Changing CSS properties in React

Currently, I am working on a dynamic table component using CSS grid. My goal is to customize the grid-template-columns CSS property. I am familiar with using repeat in CSS to achieve this. However, I am facing a challenge when it comes to dynamically chang ...

How can an Angular directive effectively serve as a front-facing interface for interacting with other elements?

This question delves into the realm of Web Components, with the examples being written in Angular for its versatility in handling certain issues (such as replace even though it's deprecated) and familiarity to many developers. Update After consideri ...

What could be causing the JSON.parse error to occur when utilizing the default code provided in "dotnet new react -o my-app"?

When executing the command 'dotnet new react -o my-app' in the terminal, it creates a C# React application. However, upon testing the initial files, the application does not function correctly. Running 'npm start' from the my-app/Client ...

The fixed navbar in Bootstrap is missing a bottom margin

I have created a basic theme using bootstrap 4. The navbar in this theme has a fixed-top class. Right below the navbar, there is a div that displays the page title as its content. <div class="aloldal_text_div"> <div class="container"> < ...

Creating a visual representation from an array of strings to produce a

My goal is to organize a list of server names into a map using a specific logic. For instance, with names like: "temp-a-name1", "temp-a-name2", "temp-b-name1", "temp-b-name2" They would be mapped as: { a: [ "temp-a-name1", "temp-a-name2" ] ...

add before the beginning and after the end

Hi there, I'm trying to figure out how to add before my initial variable and after my last one. This is the code snippet: $pagerT.find('span.page-number:first').append($previousT); $pagerT.find('span.page-number:last').append($n ...

What steps should I take to change the orientation of these items from vertical to horizontal display?

I'm struggling to get these items to appear horizontally (side by side) instead of vertically on my webpage. They are linked to a database, hence the PHP code included here. If you need more information, please feel free to ask. body { font: no ...

Stop the back button from closing the Cordova application

I've encountered a persistent issue in my cordova application where the back button consistently exits the app, regardless of any attempted solutions. I have diligently searched for answers online and tested various strategies, but none have proven su ...

The HTML code is properly linked to the JavaScript file, but for some reason, it is still not

I'm facing an issue while trying to incorporate a js file into my html document. Even though I have linked the html file to the js file, it doesn't seem to be functioning properly. In the code snippet below, everything appears to be working fine ...

Analyzing the values of two sets of key-value pairs

I am facing an issue where I have two sets of objects labeled LABELS1 and LABELS2, and my goal is to iterate through them. If any of the IDs from LABELS1 match any of the IDs from LABEL2, I need to update the simple_value of LABELS1 with the correspondin ...

What is the best way to integrate a new unique identifier into an existing MongoDB document using NodeJS?

I am looking to add up a field in a document when I input a new entry that has a replicated unique id. This is the code I have so far: MongoClient.connect(process.env.MONGODB_URI || process.env.DB_CONNECTION, { useUnifiedTopology: true, useNewUrlParser ...

Transforming Javascript code using regular expressions into C#

Currently, I am facing a challenge while trying to translate some Javascript code into .NET. Despite my efforts, I have not been able to get it right. The task at hand involves converting paths like /test/:value1/:value2 in Express for NodeJS to a regular ...

How do I test Pinia by calling one method that in turn calls another method, and checking how many times it has been called

As I embark on my journey with Vue 3 and Pinia, a particular question has been lingering in my mind without a concrete answer thus far. Let's delve into the crux of the matter... Here's an example of the store I am working with: import { ref, co ...

Replacing values between Arrays and Objects

I currently possess a lookupMap variable that contains an index of counties and their corresponding codes. Here is a snippet: { ANDERSON: { county: 'ANDERSON', code: 'us-tx-001' }, ANDREWS: { county: 'ANDREWS', code: ' ...

Tips for utilizing the <img src> tag within Google Apps Script

I am trying to incorporate an image into a google apps script using the src attribute. I have specified the folder id and path, but unfortunately the image is not displaying as expected. The folder has been shared here for reference. Any assistance with t ...

Error encountered: Attempting to upgrade Intel App Framework 2.1 resulted in TypeError: o.promise is not a function

During the process of updating Intel App Framework from version 2.0 to 2.1, I encountered a jQuery error stating 'TypeError: o.promise is not a function in jQuery'. The current setup includes jq.appframework.min.js alongside jquery-1.11.0.min.js. ...

Inheritance with AngularJS Controllers: Issues with "this" reference in child controllers

Currently, I am utilizing the controllerAs syntax in my project. To inherit from a parent, which serves as more of a base or abstract controller, I have used $controller. I came across an insightful discussion that provided the foundation for this approac ...

Regenerate main JavaScript files in Gulp whenever partials are edited

In my gulp task for javascript files, I included partial js files in the source and filtered them out to avoid building them unnecessarily. gulp.task("js", () => { return gulp .src([ src_js_folder + "*.js", src_js_f ...

Developing a Customized Filtering Mechanism in Angular 8

I have some experience working in web development, but I am relatively new to Angular. My current project involves creating a simple filter for a table's column based on user input. However, I'm facing an issue where typing in a single letter fil ...

Integrate a controller using Gulp and Angular

As a newbie to AngularJS, I've embarked on a project using a template named rdas. This template comes equipped with gulp, bower, and node (via npm). Although it's possible that I may have made errors along the way, my current dilemma is creating ...