简体   繁体   English

如何使用CSS重叠三个div?

[英]How to overlap three divs using CSS?

I came across a problem in which I need to make something like this: 我遇到了一个需要做以下事情的问题:

I'm stuck on CSS 我陷入CSS

Also when I click on any of the divs, it should surface all the way to the top. 同样,当我单击任何一个div时,它应该一直浮到顶部。

 .boxwrap{ margin-left: auto; margin-right: auto; top: 50px; } .box{ width: 100px; height: 100px; } #box1{ margin-left: -100px; background-color: red; } #box2{ margin-top: -50px; margin-bottom: -50px; margin-left: 0px; background-color: black; } #box3{ margin-left: 100px; background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div align="center" class="boxwrap"> <div class="box" id="box1"> </div> <div class="box" id="box2"> </div> <div class="box" id="box3"> </div> </div> 

Add a z-index like so: 像这样添加一个z-index

 .boxwrap{ margin-left: auto; margin-right: auto; top: 50px; } .box{ width: 100px; height: 100px; } #box1{ margin-left: -100px; background-color: red; position: relative; z-index: 2; } #box2{ margin-top: -50px; margin-bottom: -50px; margin-left: 0px; background-color: black; position: relative; z-index: 1; } #box3{ margin-left: 100px; background-color: green; position: relative; z-index: 2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div align="center" class="boxwrap"> <div class="box" id="box1"> </div> <div class="box" id="box2"> </div> <div class="box" id="box3"> </div> </div> 

Note: z-index only works on positioned elements 注意:z-index仅适用于定位的元素

The only way to make it work is to constantly increment the z-index value on each .click() on any .box , this way it won't come to any overlapping changes of the others: 使其起作用的唯一方法是不断增加任何.box上每个.box .click()z-index ,这样就不会对其他.box进行任何重叠的更改:

 var z = 2; /* needs to be at least 2, to be 1 above the other two if clicked on the black one for as the first click */ $('.box').click(function(){ $(this).css('z-index', z++); }); 
 .boxwrap { margin-left: auto; margin-right: auto; top: 50px; } .box { width: 100px; height: 100px; position: relative; /* or "absolute", "fixed" */ } #box1 { margin-left: -100px; background-color: red; z-index: 1; /* 1 more than the black one with the default 0 */ } #box2 { margin-top: -50px; margin-bottom: -50px; margin-left: 0px; background-color: black; /* here the "z-index" mustn't be negative, because you won't be able to click on it, better the put the other two 1 level/layer higher */ } #box3 { margin-left: 100px; background-color: green; z-index: 1; /* same here */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div align="center" class="boxwrap"> <div class="box" id="box1"> </div> <div class="box" id="box2"> </div> <div class="box" id="box3"> </div> </div> 

Set property position on .box 在.box上设置属性位置

 .box{
  width: 100px;
  height: 100px;
  position: relative;
}

and add property z-index: 1 for upper elements, and z-index:0 for lower elements 并添加属性z-index:1用于上部元素,z-index:0用于下部元素

 #box1{
  margin-left: -100px;
  background-color: red;
  z-index: 1;
}

 #box2{
  margin-top: -50px;
  margin-bottom: -50px;
  margin-left: 0px;
  background-color: black;
  z-index: 0;
}

 #box3{
  margin-left: 100px;
  background-color: green;
  z-index: 1;
}

 .boxwrap{ margin-left: auto; margin-right: auto; top: 50px; } .box{ width: 100px; height: 100px; } #box1{ margin-left: -100px; background-color: red; display:inline-block } #box2{ margin-top: -50px; margin-bottom: -50px; margin-left: 0px; background-color: black; } #box3{ margin-left: 100px; background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div align="center" class="boxwrap"> <div class="box" id="box1"> </div> <div class="box" id="box2"> </div> <div class="box" id="box3"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM