简体   繁体   English

你如何将左div移到背景?

[英]How do you move left div to the background?

I am trying to complete a style similar to the picture below me, whereby you have a left div partially in the background of a right div.我正在尝试完成类似于下图的样式,即您的左 div 部分位于右 div 的背景中。 Essentially, I want two side by side divs to be able to overlap each other, one in the background, and one in the foreground.本质上,我希望两个并排的 div 能够相互重叠,一个在后台,一个在前台。

This is what I'm trying to achieve:这就是我想要实现的目标:

在此处输入图像描述

This is what I have so far:这是我到目前为止所拥有的:

 .wrapper{ display: grid; width: fit-content; align-items: center; grid-template-columns: 1fr auto; border: solid 2px black; }.left{ width: 150px; height: 115px; background-color: red; border: 2px red solid; z-index:-999; }.right{ width: 120px; height: 150px; background-color: blue; border: 2px blue solid; z-index: 999; }
 <div class="wrapper"> <div class="left"> </div> <div class="right"> </div> </div>

I've tried using the z-index field to no avail.我尝试使用z-index字段无济于事。 Also, in addition to being able to control which element is in the background;此外,除了能够控制背景中的元素之外; on mouse hover, I would like the left div in the background to "float" to the foreground and increase in size partially, and the right div to "float" to the back and partially decrease in size.在鼠标 hover 上,我希望背景中的左侧 div “浮动”到前景并部分增加大小,而右侧 div “浮动”到后面并部分减小大小。

I hope my problem was adequately described, thank you.我希望我的问题得到了充分的描述,谢谢。

I think the display: grid is not helping you here as it's trying to confirm the child divs into a grid (ie, not overlapping).我认为display: grid在这里没有帮助你,因为它试图将子 div 确认为网格(即不重叠)。

I would just set the two sides to have whatever width you want (eg 50%) and use z-index to overlay them.我只需将两侧设置为您想要的任何宽度(例如 50%)并使用 z-index 覆盖它们。

.wrapper {
  max-width: 400px;
}

.left, .right {
  width: 50%;
  height: 200px;
  position: absolute;
}

.left {
  background-color: red;
  left: 0;
}

.right {
  background-color: blue;
  right: 0;
  z-index: 99;
  -webkit-box-shadow: -4px 0px 15px -1px #000000; 
   box-shadow: -4px 0px 15px -1px #000000;
}

See https://jsfiddle.net/47bh3eqk/1/https://jsfiddle.net/47bh3eqk/1/

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

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