<分区>
<分区>
代码解释:
我正在创建一个包含三个链接的栏。我制作了栏,并尝试使用 float
、text-align
和 width
分隔链接。 (我试图让中心链接居中,其他两个与它等距,并且与两侧等距。)但是,当我最初使用 3 个 divs
时(divs
除了“I” divs
),背景颜色消失了。所以我弄乱了它并意识到第三个链接的 div
上的 float 是导致问题的原因。所以我添加了另一个 div
(最后一个 div
),它使用了一些文本。但是,由于我必须在其中放置文本,因此它会影响我的间距。所以我在另一边(第一个)做了一个 div
来平衡它。但是,它仍然会在没有 float 的情况下抛出我的间距!
问题:
为什么在一行中的最后一个 div
上设置 float
属性会导致背景颜色消失?
<div style="padding:0px;margin:0px;background-color:#3C3C3C;">
<div style="color:#3C3C3C;float:left;">
I
</div>
<div style="margin-left:50px;width:20%;float:left;text-align:center;">
<a style="color:#3690B7;" href="">
Hello
</a>
</div>
<div style="width:50%;float:left;text-align:center;">
<a style="color:#3690B7;" href="">
Hello
</a>
</div>
<div style="margin-right:50px;width:20%;float:left;text-align:center;">
<a style="color:#3690B7;" href="">
Hello
</a>
</div>
<div style="color:#3C3C3C;float:right;">
I
</div>
</div>
最佳答案
您必须通过在最终 div 下面添加例如另一个 div 来清除 float :
<div style="clear:both;"></div>
关于html - 背景颜色被 "float"取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18353910/