CSS: DIV containing no height on float set

后端 未结 4 1590
感动是毒
感动是毒 2020-12-02 23:34

assume we have this code:

相关标签:
4条回答
  • 2020-12-03 00:19

    After

     <div id='leftDiv' style='float:left;width:25%;'>
       content2
     </div>
    

    add

         <div style="clear:both"></div>
    

    It will solve your problem.

    0 讨论(0)
  • 2020-12-03 00:30

    This is intentional as floats are designed for things like images in paragraphs (where multiple paragraphs can wrap around the image).

    Complex Spiral has a fuller explanation as to why and Ed Elliot describes a number of approaches to making containers expand around floats. I find the overflow: hidden approach works best in most situations.

    0 讨论(0)
  • 2020-12-03 00:31

    There is a new property introduced recently display: flow-root; Which will fix this issue without any hacks and have almost all major support

    <div id='upperDiv' style='border: 1px solid #000000; display: flow-root;'>
         <div id='rightDiv' style='float:right;width:75%;'>
           content1
         </div>  
         <div id='leftDiv' style='float:left;width:25%;'>
           content2
         </div>
    </div>
    <div id='lowerDiv' style='height:50px;border: 1px solid #000000;margin-top:5px;'>
       content3
    </div>

    0 讨论(0)
  • 2020-12-03 00:33

    Set #upperDiv any of the following:

    overflow: hidden;
    width: 100%;
    

    or

    float: left;
    width: 100%;
    

    or create a rule using CSS pseudo-elements (IE8+ compatible) like this

    #upperDiv:after {
      content: "";
      display: table;
      clear: both;
    }
    

    Best solution
    Creating a reusable class rule like the following.

    .group:after {
      content: "";
      display: table;
      clear: both;
    }
    

    Now you can apply it to anything that needs this same functionality. For example...

    <div id='upperDiv' class="group" ... >
    

    P.S. If you require IE 6/7 compatibility, checkout this post.

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