将第一个 div 置于其他两个 div 下方 - 全部具有可变高度
我试图将 3 个 div(我们称它们为 div1、div2 和 div3)放入一个宽度为 700px 的容器 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
编辑:
已解决按照评论中的建议使用JS(加载后的JS获取div2的真实高度并将top:值设置为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>
I am trying to put 3 divs (lets call them div1, div2 and div3) into one container div with 700px width. What I want to manage is that the content of the div1 is higher in the source code, but is visible lower at the webpage (please see attached picture).
<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>
The problem I am not able to solve is, that all 3 divs may have varibale height. So I cannot use position:absolute;top: x px; or padding-top: x px
Picture of requested layout: http://snapshot.own.cz/divs.png
EDIT:
solved using JS as suggested in comments (JS after load gets real height of div2 and sets top: value to 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试为您想要做的事情找到一些解决方法。但考虑到你正在尝试做的事情,这是不可能的。如果您愿意为元素设置固定高度,那么这种方法是可行的。
这是我的推理:
如果您要离开相对定位,这意味着页面的流程是保守的。因此,第一个项目首先显示,第二个项目随后显示,依此类推。为了将第一个
div
从页面的流程中取出并将其放置在底部,您必须以不同的方式定位它。如果您将其从页面的正常流程中取出,则无法使用其他元素的预先建立的高度,因为它不在同一流程中。要获得您想要的显示效果,类似这样的操作是可行的:
使用此 css
如果您绝对需要更改页面的流程,则需要使用 JavaScript 来获取第一个 div,然后将其插入到第二个 div 的顶部页面加载时的 div。请参阅此 回答有关如何执行此操作的详细信息。
希望这有帮助。
I've tried to find a few walkarounds for what you are trying to do. But this is not possible considering what you are trying to do. Something of the sort would be doable if you were willing to set fixed heights to your elements.
This is my reasoning :
If you are going to leave the positionning relative, this means that the flow of the page is conserved. Therefore, the first item is displayed first, the second follows, etc. In order to take the first
div
out of the the flow of the page and place it at the bottom, you have to position it differently. If you are taking it out of the normal flow of the page, then you cannot use the pre-established height of the other elements as it is not in the same flow.Thefeore to obtain the display you want something like this would work :
Using this css
If you absolutely need to have the flow of the page changed, you will need to turn to JavaScript to take the first div, and then insert it ontop of the second div on page load. See this answer for details on how to do this.
Hope this helps.
使用
float
元素尝试此代码。看看这个 jsFiddle
Try this code using the
float
element.And check out this jsFiddle