0

I'm trying to achieve a layout where I have a div with position: relative, and inside it, I want to place another div with position: absolute. However, I want only the part of the absolute div that is outside the relative div to be visible, while the part inside the relative div should be hidden. How can I accomplish this effect using CSS? Any help would be appreciated!

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
  z-index: 2;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  z-index: 1;
  width: 100px;
  height: 100px;
  background-color: blue;
}
<div class="relative-container">
  <div class="absolute-element"></div>
</div>
Stairss
  • 176
  • 1
  • 14
  • 2
    Like, remove `z-index` from the container, and set it to `-1` for the child? That will of course only work if the container actually has a solid background. If you need an _actual_ "cut" effect, then you will need to go look into clip-path or something like that. – CBroe Aug 08 '23 at 08:30

4 Answers4

1

Just remove z-index declaration from the relative container and put a negative z-index to the absolute container.

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  z-index: -1;
  width: 100px;
  height: 100px;
  background-color: blue;
}
<div class="relative-container">
  <div class="absolute-element"></div>
</div>
msrumon
  • 1,250
  • 1
  • 10
  • 27
1

You could for instance just put the absolute-element behind the relative div like you tried with z-index but the relative div itself can't have z-index otherwise it becomes the z-index ancestor of the absolute div and can't be put behind anymore

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  width: 100px;
  height: 100px;
  background-color: blue;
  z-index: -1;
}
<div class="relative-container">
  <div class="absolute-element"></div>
</div>

If you need the red div to have z-index then you can root both to another (possibly invisible) parent div and set them both to absolute.

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
}

.red-element{
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: red;
  z-index: 2;
}

.blue-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  width: 100px;
  height: 100px;
  background-color: blue;
  z-index: 1;
}
<div class="relative-container">
  <div class="red-element"></div>
  <div class="blue-element"></div>
</div>
remix23
  • 2,632
  • 2
  • 11
  • 21
1

You can use mix-blend-mode:

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  width: 100px;
  height: 100px;
  background-color: blue;
  mix-blend-mode: overlay;

}
<div class="relative-container">
  <div class="absolute-element">hello</div>
</div>

Or 3D transform: (Credit: How to get a child element to show behind (lower z-index) than its parent?)

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
  transform-style: preserve-3d;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  transform: translateZ(-10px);
  width: 100px;
  height: 100px;
  background-color: blue;
}
<div class="relative-container">
  <div class="absolute-element"></div>
</div>

The other provided solutions won't work if you create a stacking context in the parent (see CSS negative z-index: what does it mean?):

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
  z-index: 0; /* creates a stacking context, (or any other stacking context creating property https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context )*/
  
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  width: 100px;
  height: 100px;
  background-color: blue;
  z-index: -1;
}
<div class="relative-container">
  <div class="absolute-element">hello</div>
</div>
ATP
  • 2,939
  • 4
  • 13
  • 34
-1

.relative-container {
  position: relative;
  width: 200px;
  height: 200px;
  background-color: red;
  z-index: 2;
}

.absolute-element {
  position: absolute;
  bottom: -10px;
  right: -10px;
  z-index: 1;
  width: 100px;
  height: 100px;
  background-color: blue;
  clip-path:polygon(90px 0 , 100px 0, 100px 100px , 0 100px , 0 90px, 90px 90px);
}
<div class="relative-container">
  <div class="absolute-element"></div>
</div>