1

I have an element that contains another element, and due to other parts in the code it would be very hard to disconnect the two elements without breaking things. I have an unrelated element that the child needs to appear above, but the parent is supposed to appear below.

the code below is a recreation of what my attempts and chatgpt questions got me to, the unrelated element still appears above both the parent and the child.

.parent {
  position: relative;
  background-color: #ccc;
  height: 100px;
  width: 100px;
  z-index: 1;
}

.child {
  background-color: #f00;
  height: 50px;
  width: 50px;
  z-index: 3;
}

.unrelated {
  position: absolute;
  top: 25px;
  left: 25px;
  background-color: #00f;
  height: 25px;
  width: 50px;
  z-index: 2;
}
<div class="parent">
  <div class="child">
    Child element
  </div>
</div>

<div class="unrelated">
  Unrelated element
</div>

is there any way to make the unrelated element appear between them?

Temani Afif
  • 245,468
  • 26
  • 309
  • 415
yanzinator
  • 17
  • 7
  • what do you mean between parent and child? inside parent above child? or on z-index between parent and child? – Suraj Rao Mar 31 '23 at 04:37
  • the parent should be below the unrelated element, but the child should be above it. – yanzinator Mar 31 '23 at 04:38
  • Could you expand your example code as at the moment parent has nothing in it apart from child so it’s not clear what between would look like. Perhaps also add a sketch of the layout required. – A Haworth Mar 31 '23 at 06:10
  • i don't know why, but for some reason the stackoverflow run code snippet doesn't show properly and i can't figure out how to make it show properly. if you look at what's written in a browser, there's a parent element grey box, inside of which theres a child element red box, on top of which theres an unrelated element blue box. what i need is for the blue box to be under the red box, but still appear over the grey box. – yanzinator Mar 31 '23 at 06:39
  • what it looks like https://imgur.com/yu2Kgts (almost) what im going for https://imgur.com/O3qnAcT – yanzinator Mar 31 '23 at 06:46

1 Answers1

1

That position: relative on .parent is defining a new stacking context – as long as that is there, .parent and .child will be stacked together separately of .unrelated.

In your toy example, just removing position: relative from .parent and adding it to .child will achieve the required layout. In your final code you'll also need to remove any rules that create a new stacking context between .child and its nearest common ancestor with .unrelated. This may, unfortunately, result in a lot of messy repositioning.

<style>
.parent {
  position: static;
  background-color: #ccc;
  height: 100px;
  width: 100px;
}

.child {
  position: relative;
  background-color: #f00;
  height: 50px;
  width: 50px;
  z-index: 3;
}

.unrelated {
  position: absolute;
  top: 25px;
  left: 25px;
  background-color: #00f;
  height: 25px;
  width: 50px;
  z-index: 2;
}

</style>
<div class="parent">
  <div class="child">
    Child element
  </div>
</div>

<div class="unrelated">
  Unrelated element
</div>
motto
  • 2,888
  • 2
  • 2
  • 14
  • this worked, but now i can't use flex which completely ruined my layout . thank you though, now that i understand the problem properly i can actually figure out what to do. – yanzinator Mar 31 '23 at 06:54
  • Yeah sorry about that! Maybe the code refactoring will turn out to be less work than disrupting your layout :-O – motto Mar 31 '23 at 06:55