将第一个 div 置于其他两个 div 下方 - 全部具有可变高度

发布于 2024-12-29 09:44:25 字数 1412 浏览 0 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2025-01-05 09:44:25

我尝试为您想要做的事情找到一些解决方法。但考虑到你正在尝试做的事情,这是不可能的。如果您愿意为元素设置固定高度,那么这种方法是可行的。

这是我的推理:

如果您要离开相对定位,这意味着页面的流程是保守的。因此,第一个项目首先显示,第二个项目随后显示,依此类推。为了将第一个 div 从页面的流程中取出并将其放置在底部,您必须以不同的方式定位它。如果您将其从页面的正常流程中取出,则无法使用其他元素的预先建立的高度,因为它不在同一流程中。

要获得您想要的显示效果,类似这样的操作是可行的:

<div id="container">
    <div id="div2">
        2
    </div>
    <div id="div3">
        3
    </div>
    <div id="div1">
        <p>Some text here!</p>
    </div>
</div>

使用此 css

* {
    border: 1px solid red;
}
#container {
    position:relative;
}
#div1 {
    clear: both;
    background-color: green;
    width: 100%;
}
#div2 {
    float: left;
    width: 49%;
    background-color: blue;
}
#div3 {
    float:left;
    background-color: gray;
    width: 49%;
}

如果您绝对需要更改页面的流程,则需要使用 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 :

<div id="container">
    <div id="div2">
        2
    </div>
    <div id="div3">
        3
    </div>
    <div id="div1">
        <p>Some text here!</p>
    </div>
</div>

Using this css

* {
    border: 1px solid red;
}
#container {
    position:relative;
}
#div1 {
    clear: both;
    background-color: green;
    width: 100%;
}
#div2 {
    float: left;
    width: 49%;
    background-color: blue;
}
#div3 {
    float:left;
    background-color: gray;
    width: 49%;
}

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.

画尸师 2025-01-05 09:44:25

使用 float 元素尝试此代码。

<div style="width: 704px; padding: 10px;">
    <div style="border: 1px solid red; height: 200px;width: 500px; float: left"></div>
    <div style="border: 1px solid blue; height: 200px;width: 200px;float: right;"></div>
    <br />
    <div style="border: 1px solid green; height: 400px;width: 702px;float: left;"></div>
</div>

看看这个 jsFiddle

Try this code using the float element.

<div style="width: 704px; padding: 10px;">
    <div style="border: 1px solid red; height: 200px;width: 500px; float: left"></div>
    <div style="border: 1px solid blue; height: 200px;width: 200px;float: right;"></div>
    <br />
    <div style="border: 1px solid green; height: 400px;width: 702px;float: left;"></div>
</div>

And check out this jsFiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文