gpt4 book ai didi

html - 为什么 'clear:left' 也正确清除?

转载 作者:太空狗 更新时间:2023-10-29 15:39:31 25 4
gpt4 key购买 nike

我一直在努力让 CSS float 工作(在我的脑海中)。

注意下面的例子:

<style type="text/css">
div.container {
width:500px;
}
div.left {
float:left;
clear:left;
}
div.right {
float:right;
}
</style>
<div class="container">
<div class="left">leftdata 1</div>
<div class="left">leftdata 2</div>
<div class="right">rightdata 1</div>
<div class="right">rightdata 2</div>
<div class="right">rightdata 3</div>
<div class="right">rightdata 4</div>
</div>

这将给出以下输出:

+--------------------------------------------------------------------+| leftdata 1                                                         || leftdata 2         rightdata 1 rightdata 2 rightdata 3 rightdata 4 ||                                                                    |+--------------------------------------------------------------------+

However I was expecting this:

+--------------------------------------------------------------------+| leftdata 1         rightdata 1 rightdata 2 rightdata 3 rightdata 4 || leftdata 2                                                         ||                                                                    |+--------------------------------------------------------------------+

Why is clear:left; also clearing right?


My goal:

I want to only add a clear:right; to the DIVs marked with class: right. This should produce the following:

<style type="text/css">
div.left {float:left;clear:left;}
div.right {float:right;clear:right;}
</style>
+--------------------------------------------------------------------+| leftdata 1                                             rightdata 1 || leftdata 2                                             rightdata 2 ||                                                        rightdata 3 ||                                                        rightdata 4 |+--------------------------------------------------------------------+

最佳答案

在您的第一个示例中,rightdata-div 位于左侧的下方,因为它们稍后出现在文档中。将它们放在首位,您将获得预期的结果。

或者您可以尝试以下方法:

<style type="text/css">
div.left {
float: left;
}

div.right {
float: right;

}
div.container {
width:500px;
overflow: auto; /* to make sure .container expands vertically */
}

<div class="container">
<div class="left">
<div>leftdata 1</div>
<div>leftdata 2</div>
</div>
<div class="right">
<div>rightdata 1</div>
<div>rightdata 2</div>
<div>rightdata 3</div>
<div>rightdata 4</div>
</div>
</div>

关于html - 为什么 'clear:left' 也正确清除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1037901/

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