给定一个模板,HTML不能修改,因为其他要求,如何可能显示(重新安排)一个div上面的另一个div时,他们不是在HTML的顺序?两个div都包含高度和宽度不同的数据。

<div id="wrapper">
    <div id="firstDiv">
        Content to be below in this situation
    </div>
    <div id="secondDiv">
        Content to be above in this situation
    </div>
</div>
Other elements

希望期望的结果是显而易见的:

Content to be above in this situation
Content to be below in this situation
Other elements

当尺寸是固定的,很容易定位他们在需要的地方,但我需要一些想法,当内容是可变的。为了实现这个场景,请将两者的宽度都考虑为100%。

我特别寻找一个css唯一的解决方案(它可能必须满足其他解决方案,如果它不成功)。

接下来还有其他因素。考虑到我演示的有限场景,我提到了一个很好的建议——考虑到它可能是最好的答案,但我也希望确保后面的元素不受影响。


当前回答

负的顶部边距可以达到这种效果,但是需要为每一页定制。例如,这个标记…

<div class="product">
<h2>Greatest Product Ever</h2>
<p class="desc">This paragraph appears in the source code directly after the heading and will appear in the search results.</p>
<p class="sidenote">Note: This information appears in HTML after the product description appearing below.</p>
</div>

...这个CSS…

.product { width: 400px; }
.desc { margin-top: 5em; }
.sidenote { margin-top: -7em; }

…你可以把第二段放在第一段上面。

当然,您必须手动调整CSS以适应不同的描述长度,以便介绍段跳跃适当的数量,但如果您对其他部分的控制有限,而对标记和CSS有完全的控制,那么这可能是一个选择。

其他回答

在CSS中,将第一个div向左或向右浮动。第二个div与第一个div一样向左或向右浮动。应用clear:向左或向右,与上面两个div的第二个div相同。

例如:

#firstDiv {
    float: left;
}

#secondDiv {
    float: left;
    clear: left;
}

使用css很容易,只需使用display:block和z-index属性

这里有一个例子:

HTML:

<body>
    <div class="wrapper">

        <div class="header">
            header
        </div>

        <div class="content">
            content
        </div>
    </div>
</body>

CSS:

.wrapper
{
    [...]
}

.header
{
    [...]
    z-index:9001;
    display:block;
    [...]
}

.content
{
    [...]
    z-index:9000;
    [...]
}

编辑:为div设置一些背景颜色是很好的,这样可以正确地看到东西。

负的顶部边距可以达到这种效果,但是需要为每一页定制。例如,这个标记…

<div class="product">
<h2>Greatest Product Ever</h2>
<p class="desc">This paragraph appears in the source code directly after the heading and will appear in the search results.</p>
<p class="sidenote">Note: This information appears in HTML after the product description appearing below.</p>
</div>

...这个CSS…

.product { width: 400px; }
.desc { margin-top: 5em; }
.sidenote { margin-top: -7em; }

…你可以把第二段放在第一段上面。

当然,您必须手动调整CSS以适应不同的描述长度,以便介绍段跳跃适当的数量,但如果您对其他部分的控制有限,而对标记和CSS有完全的控制,那么这可能是一个选择。

你可以使用Flex

#包装{ 显示:flex; flex-direction:列; } # firstDiv { 顺序:1; } # secondDiv { 秩序:0; } < div id = "包装" > < div id = " firstDiv " > 在这种情况下,内容在下面 < / div > < div id = " secondDiv " > 在这种情况下满足于上面 < / div > < / div >

如果你只使用css,你可以使用flex。

.price { 显示:flex; 对齐项目:中心; justify-content:中心; flex-direction: row-reverse;/ /恢复水平 / / flex-direction: column-reverse;恢复垂直 } < div class = "价格" > < div >第一块< / div > 第二块< div > < / div > < / div >