css - Position relative, float 把div 脱离正常流

标签 css css-position

如何防止将 div 从正常流中取出但仍然使用 float,如下所示:

<div id="one">
    <div id="two"></div>
</div>

CSS:

#one {
    width: 100%;
    height: auto;
}

#two {
    width: 100px;
    height: 100px;
    position: relative;
    float: left;
}

现在 div "one"没有高度,div "two"看起来好像不在第一个 div 中。

最佳答案

啊,崩溃问题。这里有一篇关于 float 的优秀文章 http://css-tricks.com/all-about-floats/ 在你的情况下,我会添加

overflow: auto

#one

相关资料如下:

Techniques for Clearing Floats

If you are in a situation where you always know what the succeeding element is going to be, you can apply the clear: both; value to that element and go about your business. This is ideal as it requires no fancy hacks and no additional elements making it perfectly semantic. Of course things don't typically work out that way and we need to have more float-clearing tools in our toolbox.

The Empty Div Method is, quite literally, an empty div. <div style="clear: both;"></div>. Sometimes you'll see a <br /> element or some other random element used, but div is the most common because it has no brower default styling, doesn't have any special function, and is unlikely to be generically styled with CSS. This method is scorned by semantic purists since its presence has no contexual meaning at all to the page and is there purely for presentation. Of course in the strictest sense they are right, but it gets the job done right and doesn't hurt anybody.

The Overflow Method relies on setting the overflow CSS property on a parent element. If this property is set to auto or hidden on the parent element, the parent will expand to contain the floats, effectively clearing it for succeeding elements. This method can be beautifully semantic as it may not require an additional elements. However if you find yourself adding a new div just to apply this, it is equally as unsemantic as the empty div method and less adaptable. Also bear in mind that the overflow property isn't specifically for clearing floats. Be careful not to hide content or trigger unwanted scrollbars.

The Easy Clearing Method uses a clever CSS pseudo selector (:after) to clear floats. Rather than setting the overflow on the parent, you apply an additional class like "clearfix" to it. Then apply this CSS:

.clearfix:after { 
   content: "."; 
   visibility: hidden; 
   display: block; 
   height: 0; 
   clear: both;
}

This will apply a small bit of content, hidden from view, after the parent element which clears the float. This isn't quite the whole story, as additional code needs to be used to accomodate for older browsers.

关于css - Position relative, float 把div 脱离正常流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18906966/

相关文章:

css - 固定菜单不水平滚动

jquery - 删除空 div 的空间

jquery - 两个独立的固定列标题,使用 CSS 或 JQuery 进行水平滚动

javascript - 如何定位 Bootstrap 弹出窗口?

css - 如何正确地从绝对定位切换到固定定位

html - css:如何在不增加整个文本行高的情况下增加一个字符的字体大小?

html - 具有较高 z-index 的绝对定位 div 显示在其他 div 下方

javascript - 单击更改后使用 id 显示 div

css - Form Div 高度随内容展开

jquery - 禁用JQuery barrating rating star plugin hover and click effect