Search code examples
csssassz-index

How to hide an item under another item without using z-index


I use this code on my website: https://codepen.io/dustindowell/pen/GgeWep?editors=1100

I need the tail hidden under the body of the message, like here: https://codepen.io/haygt/pen/KKPMBOL?editors=1100

But I can't use z-index: -1;

Because, in that case, the tail is completely gone (I'm using vuetify, and most likely it overrides the z-index and I can't use negative values).

So I tried to do it this way: https://codepen.io/haygt/pen/eYOzyLv?editors=1100

But as you can see it's not working, so how can I do to green tail was hidden under the body of the message?


Solution

  • UPDATE:

    To hold the position, wrap it with a container as I used bubble-container.

    *,
    *::before,
    *::after {
      margin: 0;
      border: 0;
      padding: 0;
      word-wrap: break-word;
      box-sizing: border-box;
    }
    
    body {
      font-size: 1.5em;
      margin: 1em;
      background-color: rgba(8, 36, 64, 0.0625);
    }
    
    
    /* MIXIN STARTS HERE */
    
    
    /* Requires LifeSaver Sass mixin linked externally */
    
    .bubble-center {
      position: relative;
      display: flex;
      justify-content: center;
    }
    
    .bubble-right {
      position: relative;
      display: flex;
      justify-content: flex-end;
    }
    
    .bubble-left {
      position: relative;
      display: flex;
      justify-content: flex-start;
    }
    
    .bubble {
      margin: 0.25em;
      min-height: 1em;
      padding: 0.25em 0.75em;
      position: relative;
      border-radius: 0.5em;
      line-height: 1.5;
      color: white;
      background-color: dodgerblue;
      z-index: 10;
    }
    
    .tail {
      display: block;
      width: 0.75em;
      height: 0.5em;
      position: absolute;
      right: 0.1em;
      bottom: 0;
      border-left: 0.5em solid green;
      border-bottom-left-radius: 100%;
      z-index: 1;
    }
    
    .bubble-container {
      position: relative;
    }
    <div class="bubble-right">
      <div class="bubble-container">
        <div class="bubble">
          <p>The default bubble.</p>
        </div>
        <div class="tail"></div>
      </div>
    </div>
    
    <div class="bubble-left">
      <div class="bubble-container">
        <div class="bubble">
          <p>The default bubble.</p>
        </div>
        <div class="tail"></div>
      </div>
    </div>
    
    <div class="bubble-center">
      <div class="bubble-container">
        <div class="bubble">
          <p>The default bubble.</p>
        </div>
        <div class="tail"></div>
      </div>
    </div>


    You can achieve by using a regular div, instead of ::before as you can't use z-index:-1.

    *,
    *::before,
    *::after {
      margin: 0;
      border: 0;
      padding: 0;
      word-wrap: break-word;
      box-sizing: border-box;
    }
    
    body {
      font-size: 1.5em;
      margin: 1em;
      background-color: rgba(8, 36, 64, 0.0625);
    }
    
    
    /* MIXIN STARTS HERE */
    
    
    /* Requires LifeSaver Sass mixin linked externally */
    
    .bubble-center {
      text-align: right;
      position: relative;
    }
    
    .bubble {
      display: inline-block;
      margin: 0.25em;
      min-height: 1em;
      padding: 0.25em 0.75em;
      position: relative;
      border-radius: 0.5em;
      line-height: 1.5;
      color: white;
      background-color: dodgerblue;
      z-index: 10;
    }
    
    .tail {
      display: block;
      width: 0.75em;
      height: 0.5em;
      position: absolute;
      right: 0.1em;
      bottom: 0;
      border-left: 0.5em solid green;
      border-bottom-left-radius: 100%;
      z-index: 1;
    }
    <div class="bubble-center">
      <div class="bubble">
        <p>The default bubble.</p>
      </div>
      <div class="tail"></div>
    </div>

    pug

    .bubble-center
      .bubble
        p The default bubble.
      .tail
    

    You might need to convert this CSS to SCSS