我有一些容器,它们的子元素只使用了绝对/相对定位。如何设置容器的高度才能让它们的子元素都在容器内?
以下是代码:
HTML
<section id="foo">
<header>Foo</header>
<article>
<div class="one"></div>
<div class="two"></div>
</article>
</section>
<div style="clear:both">Clear won't do.</div>
<!-- I want to have a gap between sections here -->
<section id="bar">
<header>bar</header>
<article>
<div class="one"></div><div></div>
<div class="two"></div>
</article>
</section>
CSS
article {
position: relative;
}
.one {
position: absolute;
top: 10px;
left: 10px;
background: red;
width: 30px;
height: 30px;
}
.two {
position: absolute;
top: 10px;
right: 10px;
background: blue;
width: 30px;
height: 30px;
}
这里有一个jsfiddle代码。我希望"bar"文本出现在4个正方形之间,而不是在它们后面。
有什么简单的解决方法吗?
请注意,我不知道这些子元素的高度,并且无法为容器设置 height: xxx。