Why is the parent div height zero when it has floated children

后端 未结 4 1637
梦如初夏
梦如初夏 2020-11-27 09:45

I have the following in my CSS. All margins/paddings/borders are globally reset to 0.

#wrapper{width: 75%; min-width:         


        
相关标签:
4条回答
  • 2020-11-27 10:08

    Content that is floating does not influence the height of its container. The element contains no content that isn't floating (so nothing stops the height of the container being 0, as if it were empty).

    Setting overflow: hidden on the container will avoid that by establishing a new block formatting context. See methods for containing floats for other techniques and containing floats for an explanation about why CSS was designed this way.

    0 讨论(0)
  • 2020-11-27 10:30

    Ordinarily, floats aren't counted in the layout of their parents.

    To prevent that, add overflow: hidden to the parent.

    0 讨论(0)
  • 2020-11-27 10:31

    Now, you can

    #wrapper { display: flow-root; }
    
    • Compatibility https://caniuse.com/flow-root
    • History https://css-tricks.com/snippets/css/clear-fix/
    0 讨论(0)
  • 2020-11-27 10:32

    I'm not sure this is a right way but I solved it by adding display: inline-block; to the wrapper div.

    #wrapper{
        display: inline-block;
        /*border: 1px black solid;*/
        width: 75%;
        min-width: 800px;
    }
    
    .content{
        text-align: justify; 
        float: right; 
        width: 90%;
    }
    
    .lbar{
        text-align: justify; 
        float: left; 
        width: 10%;
    }
    
    0 讨论(0)
提交回复
热议问题