gpt4 book ai didi

html - float 的 div 不占用全宽

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

为什么 float div 不占满宽度?它们不是仍然是 block 元素吗?

这是一个例子 http://jsfiddle.net/GKjC8/

html

<div id="a">a</div>
<div id="b">b</div>

CSS

div {
background-color:cyan;
}
#a {
float:left;
}
#b {
clear:left;
}

a div 看起来像是内联,因为它占用的空间与其内容一样多。谁能解释一下?

最佳答案

你必须设置宽度:

#a {
float: left;
width: 100%;
}

“You should always set a width on floated items (except if applied directly to an image – which has implicit width). If no width is set, the results can be unpredictable.” Floatutorial: Float Basics

fiddle

关于html - float 的 div 不占用全宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24636335/

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