Why doesn't the height of a container element increase if it contains floated elements?

后端 未结 7 1038
情书的邮戳
情书的邮戳 2020-11-21 11:37

I would like to ask how height and float work. I have an outer div and an inner div that has content in it. Its height may vary depending on the content of the inner div but

7条回答
  •  生来不讨喜
    2020-11-21 11:59

    you can use overflow property to the container div if you don't have any div to show over the container eg:

    Content One
    Content Two

    Here is the following css:

    .container{
        width:100%;/* As per your requirment */
        height:auto;
        float:left;
        overflow:hidden;
    }
    .one{
        width:200px;/* As per your requirment */
        height:auto;
        float:left;
    }
    
    .two{
        width:200px;/* As per your requirment */
        height:auto;
        float:left;
    }
    

    -----------------------OR------------------------------

        
    Content One
    Content Two

    Here is the following css:

        .container{
            width:100%;/* As per your requirment */
            height:auto;
            float:left;
            overflow:hidden;
        }
        .one{
            width:200px;/* As per your requirment */
            height:auto;
            float:left;
        }
    
        .two{
            width:200px;/* As per your requirment */
            height:auto;
            float:left;
        }
        .clearfix:before,
        .clearfix:after{
            display: table;
            content: " ";
        }
        .clearfix:after{
            clear: both;
        }
    

提交回复
热议问题