Height of parent div is zero even if it has child with finite heights

后端 未结 2 1040
广开言路
广开言路 2020-12-04 09:29

I have a website whose layout has been shown in the diagram. The body consists of a main container, which comprises of header, parent div

2条回答
  •  爱一瞬间的悲伤
    2020-12-04 10:17

    Seems like you got a case for the clearfix class.

    So I'm guessing you're floating the child div and that's why the parent div's height is 0. When you use floats, the parent doesn't adapt to the height of the children.

    You can apply the 'clearfix' classes to the parent of the floating elements (of course you need to have it in your stylesheet) and it will add an insivible '.' at the end. Your parent will then have the correct height.

    Note, it's cross platform, compatible IE6 +, Chrome, Safari, Firefox, you name it!

    .clearfix:after {
        content: ".";
        display: block;
        clear: both;
        visibility: hidden;
        line-height: 0;
        height: 0;
    }
    
    .clearfix {
        display: inline-block;
    }
    
    html[xmlns] .clearfix {
        display: block;
    }
    
    * html .clearfix {
        height: 1%;
    }
    

提交回复
热议问题