skip to Main Content

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>

4

Answers


  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>
    Login or Signup to reply.
  2. .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>
    Login or Signup to reply.
  3. 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>
    Login or Signup to reply.
  4. 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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search