gpt4 book ai didi

html - 没有按照 html 中的要求获得正确的 div 位置

转载 作者:太空宇宙 更新时间:2023-11-04 12:15:38 25 4
gpt4 key购买 nike

在我的一个元素中处理 html 代码时,我遇到了一个问题,我将一个主 div 作为容器,在其中我在左侧位置使用了一个子 div,在右侧位置使用了第二个,在右侧位置使用了第三个下一行。直到现在我的代码运行得非常完美,正如您在我的代码中看到的那样。但是我希望我的第三个 div 恰好在我的第一个 div 的底部,其高度小于我的第二个 div。所以,我只想知道它们在 html 或 css 中的任何其他属性,除了在我的第三个 div 中使用“margin-top”属性,我可以通过它来实现。为了更清楚地说明这一点,我在下面给出了一个示例。

如果有人愿意打扰我的问题并给我任何建议,我将非常高兴。在此先感谢您。

<div style="width:500px;">
<div style="width:150px;height:50px;background-color:black;float:left; color:white;">
1st div
</div>

<div style="width:100px;height:100px;background-color:#0CF;float:right;">
2nd div
</div>
<div style="clear:both"></div>
<div style="width:50px;height:50px;background-color:#F00;float:left;">
3rd div
</div>
</div>

最佳答案

您需要先删除 <div>clear:both; - 这会将您的第三个 div 推到其他两个之下。

然后添加clear:left;到第三个分区。这会导致它清除所有向左浮动的元素,同时忽略向右浮动的元素。

<div style="width:500px;">
<div style="width:150px;height:50px;background-color:black;float:left; color:white;">
1st div
</div>

<div style="width:100px;height:100px;background-color:#0CF;float:right;">
2nd div
</div>
<div style="width:50px;height:50px;background-color:#F00;float:left;clear:left;">
3rd div
</div>

参见 JSFiddle

您可能还想看看 this question on SO , 其中包括 floatclear更详细。

关于html - 没有按照 html 中的要求获得正确的 div 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28739597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com