简体   繁体   English

如何通过相对位置将div彼此叠加

[英]How to layer div's on top of each other with relative positioning

I need to place two transparent div's on top of each other, and then move them vertically from each other based on a percent value using only CSS. 我需要将两个透明的div放在彼此的顶部,然后仅使用CSS根据百分比值将它们彼此垂直移动。 This is what I would like it to look like if the percent value was 20%: 如果百分比值为20%,这就是我想要的样子:

例

In this example the div's are 100x100px, and the parent div simply have 10px left and right padding that results in 20% difference in vertical position. 在此示例中,div的大小为100x100px,而父div的左侧和右侧填充仅为10px,导致垂直位置的差异为20%。 The code looks like this: 代码如下:

<div class="parent" style="padding: 0 10px;">
    <div class="child left"></div>
    <div class="child right"></div>
</div>

.parent {
    position: relative;
    display: inline-block;
    border: 1px solid black;
    min-width: 100px;
    height: 100px;
}

.child {
    position: absolute;
    top: 0;
    width: 100px;
    height: 100px;
    opacity: 0.5;
}

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

.child.right {
    right: 0;
    background-color: green;
}

However I don't actually know the size of the div's in my production version of the CSS, that's why I need to be able to separate them with relative percent values instead of pixels. 但是我实际上不知道我的CSS生产版本中div的大小,这就是为什么我需要能够使用相对百分比值而不是像素来将它们分开。

Here is a fiddle of the example, and one that fails: 这是一个例子的小提琴,但失败了:

http://jsfiddle.net/rva18302/ http://jsfiddle.net/rva18302/

I can change the markup in any way, the only requirements is that the root-div resize itself dynamically to fit the child-divs, and that the child-divs keeps the same aspect ratio (square). 我可以以任何方式更改标记,唯一的要求是root-div可以动态调整自身大小以适合子div,并且子div保持相同的宽高比(正方形)。

It works, but it obviously works relative of your page, as percentage is a variable value and pixel is a fixed value. 它有效,但显然相对于您的页面有效,因为百分比是一个可变值,像素是一个固定值。 You cannot assume 10px as 10% and believe that it will work. 您不能将10px假设为10%,并认为它会起作用。

Taking your example, I have changed the percentage value and it works. 以您的示例为例,我更改了百分比值,它可以工作。

DEMO 演示

Code change: 代码更改:

<div class="parent" style="padding: 0 1.5%;">

Hope this helps. 希望这可以帮助。

I don't know if I fully understood your question but if you want to 'separate them with relative percent values instead of pixels' and keep responsiveness to parent div you could set width as percentage and it should work!! 我不知道我是否完全理解您的问题,但是如果您想“使用相对百分比值而不是像素分隔它们”并保持对父div的响应能力,则可以将宽度设置为百分比,这样就可以了!

For ex.: 例如:

.child {
  width:90%;
 }
.parent {
  width:100%;
}

fiddle 小提琴

Would you be able to use pseudo elements for this? 您可以为此使用伪元素吗?

 div { margin: 10px; height: 100px; width: 100px; position: relative; } div:before { content: ""; position: absolute; top: 0; left: -10%; height: 100%; width: 100%; background: red; opacity: 0.5; } div:after { opacity: 0.5; content: ""; position: absolute; top: 0; right: -10%; height: 100%; width: 100%; background: green; } 
 <div></div> 

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

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