我的css布局的一部分设置div高度与宽度相关,因此纵横比对于所有屏幕尺寸都是相同的。这种方式我从来没有专门将div的高度设置为px值 - 高度仅仅是宽度的高宽比。如果你之前还没有碰到过这种技术,here is a good explanation。多个垂直div,高度与宽度相关
到我的特殊问题...
考虑到这一点我已经创建了下面这个简单的div是两倍宽,因为它是高:(to see it in action please see this fiddle)
<html>
<head>
<style type="text/css">
.container
{
position:relative;
width:50%;/*half the width of the whole page*/
margin:auto;/*center the whole thing*/
}
.relative_container
{
background-color:blue;
position:absolute;
width:100%;
top:0;
left:0;
}
.set_height
{
width:100%;
height:100%;
margin-top:50%;/*aspect ratio 2:1*/
}
</style>
</head>
<body>
<div class='container'>
<div class='relative_container'>
<div class='set_height'></div>
</div>
</div>
</body>
</html>
都好到目前为止。然而,我然后想在第一个下添加另一个相同的div。正如你在this fiddle中看到的那样,我无法让它们以任何方式显示,除了彼此之外。毫无疑问,答案在于清除div as demonstrated here,但我无法弄清楚。
'.container's只有1个元件:绝对定位的元素。绝对定位的元素从父母的角度来看,其行为类似于“display:none”元素。此外,'clear'ed元素仅受'float'ed元素影响,但您的示例中没有浮点数。 – biziclop
谢谢。即使我把'margin:auto'换成'float:left',问题仍然存在 – mulllhausen
你应该以某种方式摆脱'pos:abs'。 – biziclop