CSS Float does not stretch Div Height

前端 未结 4 1813
慢半拍i
慢半拍i 2021-02-03 15:42

I have had this problem for a long time when working with HTML/CSS and Floats.

In the image you can see I have a Box Div that is Floated left as there is many of these B

相关标签:
4条回答
  • 2021-02-03 15:46

    Try applying overflow:auto to the container, the one whose height you want to resize based on the height of its contents (the floated items).

    I'd add a jsfiddle link but it's not been working at all for me recently. :/

    0 讨论(0)
  • 2021-02-03 15:50

    You need to clear your floats using a clearfix or clearing element.

    Method #1 clearfix

    .clearfix:before,
    .clearfix:after {
        content: " ";
        display: table;
    }
    
    .clearfix:after {
        clear: both;
    }
    
    .clearfix {
        *zoom: 1;
    }
    

    Then you add the class on the containing element

     <div class="module-box-body clearfix" style="display: block;">
    

    http://jsfiddle.net/u5HXu/8/

    Method #2 Clearing Element

    .clear {
        clear: both;
    }
    

    Then you add the following HTML after your floated elements.

    <div class="clear"></div>
    

    http://jsfiddle.net/u5HXu/9/

    0 讨论(0)
  • 2021-02-03 15:57

    You can use the overflow: hidden trick on the parent container (..module-box-body) ;) fixes everything.

    http://jsfiddle.net/teddyrised/ct6gr/

    0 讨论(0)
  • 2021-02-03 16:12

    Change your .module-box-body li to

    .module-box-body li {
        display: inline-block;
        margin: 0 12px 0;
        list-style: none;
    }
    

    Updated fiddle.

    0 讨论(0)
提交回复
热议问题