简体   繁体   English

css, html 求助:左右浮动,切断背景展开过去的div

[英]css, html help : float left and right, cutting off background to expand past div

I have a div floating left and a div floating right, and I would like to have a background-color changed.我有一个向左浮动的 div 和一个向右浮动的 div,我想更改背景颜色。 It changes the background, but it stops right before the floating divs.它会更改背景,但会在浮动 div 之前停止。 When I take them off, it continues having the correct background color that I desire.当我取下它们时,它会继续具有我想要的正确背景颜色。

<div style='clear:both;border-bottom:3px solid #999;border-top:#333 solid thin;background:#D7E7E8;position:relative;'>
    <div style='width:1091px;margin:0 auto;margin-top:70px;'>
        <div style='float:left;width:200px;border:thin solid black;margin-bottom:50px;position:relative;'>
            float LEFT
        </div>
        <div style='float:right;border:thin solid black; width:800px;border:thin solid black;margin-bottom:50px;position:relative;'>
            float RIGHT
        </div>
    </div>
</div>

thank you!谢谢你!

You must clear the floats so the parent can surround them.您必须清除浮动,以便父级可以包围它们。

<div style='clear:both;border-bottom:3px solid #999;border-top:#333 solid thin;background:#D7E7E8;position:relative;'>
    <div style='width:1091px;margin:0 auto;margin-top:70px;'>
        <div style='float:left;width:200px;border:thin solid black;margin-bottom:50px;position:relative;'>
            float LEFT
        </div>
        <div style='float:right;border:thin solid black; width:800px;border:thin solid black;margin-bottom:50px;position:relative;'>
            float RIGHT
        </div>

        <!--HERE IS THE CLEARING DIV: -->
        <div style="clear: left;"></div>
    </div>
</div>

You can also, make the parent itself float, and then you won't need the additional markup (clearing div).您还可以使父级本身浮动,然后您就不需要额外的标记(清除 div)。 If you do this, then your parent will need a width specified.如果你这样做,那么你的父母将需要指定一个宽度。

EXPLANATION:解释:

When an element is floating, the parent is not aware of its height (unless it is a floating element itself).当一个元素浮动时,父级不知道它的高度(除非它本身是一个浮动元素)。 You need a clear below the floats so that the parent div recognises the full height of all its children.您需要在浮动下方清除,以便父 div 识别其所有子元素的完整高度。

You don't need to force-clear the floats - you need only to define the overflow for any position:relative div.您不需要强制清除浮动 - 您只需为任何 position:relative div 定义溢出。 Overflow:hidden and overflow:auto close the div without any further intervention, on everything from IE6 up.溢出:隐藏和溢出:自动关闭 div,无需任何进一步干预,从 IE6 开始。

Change the first line to:将第一行更改为:

<div style='clear:both;border-bottom:3px solid #999;border-top:#333 solid thin;background:#D7E7E8;position:relative; overflow:hidden;'>

and it'll accomplish the same thing as forcing the div closed.它会完成与强制 div 关闭相同的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM