Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to overlap one div over another with z-index

Tags:

html

css

z-index

I want to get my .dynamic-tooltip to overlay on my .static-tooltip. And my static tooltip has to be hidden.

But I am unable to do so with z-index. Please tell me where am I going wrong.

Please have a look at my code. https://jsfiddle.net/x883m3hg/

<div class = "static-tooltip">
  <div class = "tooltip-container">
    <div class = "item-key">
    Static tooltip Key
    </div>
    <div class = "item-value">
    Static tooltip Value
    </div>
  </div>
</div>


<div class = "dynamic-tooltip">
  <div class = "tooltip-container">
    <div class = "item-key">
    Dynamic tooltip Key
    </div>
    <div class = "item-value">
    Dynamic tooltip value
    </div>
  </div>
</div>

.static-tooltip{
  position:relative;
  z-index:1;
  width:100%;
  height: 30px;
}

.dynamic-tooltip{
  position:absolute;
  z-index:2;
  top:3px;
  width: 100%;
  height:30px
}

Thanks in advance.

like image 794
user3709813 Avatar asked Oct 18 '22 12:10

user3709813


1 Answers

The body element has a default margin of 8px. Start by removing that:

body { margin: 0; }

Then reset the top offset on the dynamic-tooltip to 0.

.dynamic-tooltip { top: 0; }

Here's your revised code:

  • added background colors for illustration
  • no changes to HTML
  • two changes to CSS

body {
  margin: 0;   /* new */
}
.static-tooltip {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 30px;
  background-color: aqua;
}
.dynamic-tooltip {
  position: absolute;
  z-index: 2;
  top: 0px;  /* adjusted */
  width: 100%;
  height: 30px;
  background-color: red;
}
<div class="static-tooltip">
  <div class="tooltip-container">
    <div class="item-key">
      Static tooltip Key
    </div>
    <div class="item-value">
      Static tooltip Value
    </div>
  </div>
</div>

<div class="dynamic-tooltip">
  <div class="tooltip-container">
    <div class="item-key">
      Dynamic tooltip Key
    </div>
    <div class="item-value">
      Dynamic tooltip value
    </div>
  </div>
</div>

Revised Fiddle

like image 185
Michael Benjamin Avatar answered Nov 15 '22 07:11

Michael Benjamin