Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve div overlapped to another div?

How can I achieve a <div> overlap so that the div #inner-block us in the foreground?

#block-1 {
  position: absolute;
  width: 200px;
  height: 200px;
  top: 10px;
  left: 10px;
  background-color: #999;
  z-index: 1;
}

#inner-block {
  position: relative;
  width: 100px;
  height: 100px;
  margin: 20px;
  background-color: #777;
  z-index: 100;
}

#block-2 {
  position: absolute;
  width: 200px;
  height: 200px;
  top: 60px;
  left: 60px;
  background-color: #666;
  z-index: 2;
}
<div id="block-1">
  <div id="inner-block"></div>
</div>
<div id="block-2"></div>
like image 992
Mano World Avatar asked Oct 28 '25 23:10

Mano World


1 Answers

A simple solution would be to update your HTML like so:

<div id="block-1">
  <div id="inner-block"></div></div>
  <div id="block-2">
</div>

This works because it ensures that the ordering of block-2 and inner-block is relative to a common parent; block-1:

#block-1
{
  position: absolute;
  width: 200px;
  height: 200px;
  top: 10px;
  left: 10px;
  background-color: #999;
  z-index: 1;
}
#inner-block
{
  position: relative;
  width: 100px;
  height: 100px;
  margin: 20px;
  background-color: #777;
  z-index: 100;
}
#block-2
{
  position: absolute;
  width: 200px;
  height: 200px;
  top: 60px;
  left: 60px;
  background-color: #666;
  z-index: 2;
}
<div id="block-1">
  <div id="inner-block"></div>
  <div id="block-2"></div>
</div>

Hope this helps!

like image 173
Dacre Denny Avatar answered Oct 30 '25 13:10

Dacre Denny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!