【发布时间】:2012-01-30 07:10:31
【问题描述】:
我正在尝试将 3 个 div(我们称之为 div1、div2 和 div3)放入一个宽度为 700 像素的容器 div 中。我要管理的是 div1 的内容在源代码中较高,但在网页中可见较低(请参见附图)。
<div id="container">
<div id="div1">content of the div1</div>
<div id="div2">content of the div2</div>
<div id="div3">content of the div3</div>
</div>
我无法解决的问题是,所有 3 个 div 都可能具有可变高度。所以我不能使用 position:absolute;top: x px;或 padding-top: x px
请求布局的图片: http://snapshot.own.cz/divs.png
编辑:
已解决按照 cmets 中的建议使用 JS(加载后的 JS 获取 div2 的实际高度并将 top: value 设置为 div1)
$(document).ready(function() {
var newtop = $('#div2').height() ;
$('#div1').css("top",newtop);
});
<style>
#container{
width: 700px;
clear: both;
}
#div1{
width: 700px;
background: red;
float: right;
position:relative;
top:20px;
}
#div2{
width: 500px;
height: 200px;
background: blue;
float: left;
position: absolute;
}
#div3{
width: 200px;
background: green;
float: left;
position:absolute;
left: 500px;
}
</style>
<div id="container">
<div id="div1">content of the div1</div>
<div id="div2">content of the div2</div>
<div id="div3">content of the div3</div>
</div>
【问题讨论】:
-
您不想让页面的流程显示在屏幕上的原因是什么?
-
因为 div1 包含与页面相关的文本,我希望它在源代码中更高(div2 包含图片+一些通用文本,div3 是联系表格)。
-
这可能吗?我仍在努力寻找解决方案,但没有运气
-
我认为没有使用或JS是不可能的。你可以在这里试试:jsfiddle.net/QrVDC
-
好的,所以我想我会尝试以某种方式估计 php 中 div2 的高度(根据其中的元素数量)。编辑:我已经考虑过使用 JS 以某种方式获得高度,但我不喜欢这样的解决方案。
标签: html css positioning