html - 为什么 float collapse 不会出现 fixed position

标签 html css css-float

我玩花车,然后我注意到固定位置不会出现“float collapse bug”。这是 example .

所以我有两个 div:

<body
    <div class="static">
        <img>
        <p>text text text</p>
    </div>
    <div class="fixed">
        <img>
        <p>text text text</p>
    </div>
</body>

第一个是静态位置,第二个是固定位置:

.fixed, .static{
    outline: 1px solid black;
    width: 150px;
}
.fixed{
    position: fixed;
    left: 200px;
    top: 200px;
}
img{
   float: right;
   background-color: green;
   width: 100px;
   height: 100px;
}

结果:

result

那么为什么第二个 fixed-div 不需要像 .clearfix 这样的东西来修复 float 折叠?

最佳答案

因为 position: fixed; 创建了 Block formatting context .

也试试下面的样式,它们在您的 div 中有类似的效果。

  • float
  • position absolutefixed
  • display - inline-blockstabletable-cells
  • 溢出 - 隐藏自动

关于html - 为什么 float collapse 不会出现 fixed position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32032938/

相关文章:

jQuery 不触发附加项

html - 循环中的CSS 3d动画变换大小

javascript - 在公共(public)站点中使用所有 AJAX 进行服务器调用是否存在问题?

css - 是否有可能使可变大小 'wrap'/stick/flow 的 div 相互 float ?

html - 百分比 float 网格布局子像素错误

html - 内部 div 应该在父 div 之外 - 重叠

css - wordpress 中的导航菜单溢出

CSS - 隐藏 <ul> 中的中间 <li> 元素

javascript - 如何从 Bootstrap 调整模态大小

html - 是什么导致 CSS 中的延迟滚动?