how to make a div to wrap two float divs inside?

后端 未结 11 2163
野趣味
野趣味 2020-12-07 15:40

I don\'t know if it\'s a common problem, but I can\'t find the solution in web so far. I would like to have two divs wrapped inside another div, however these two divs insid

相关标签:
11条回答
  • 2020-12-07 15:54

    HTML

    <div id="wrapper">
        <div id="nav"></div>
        <div id="content"></div>      
        <div class="clearFloat"></div>
    </div>
    

    CSS

    .clearFloat {
        font-size: 0px;
        line-height: 0px;
        margin: 0px;
        padding: 0px;
        clear: both;
        height: 0px;
    }
    
    0 讨论(0)
  • 2020-12-07 15:55

    Here's another, I found helpful. It works even for Responsive CSS design too.

    #wrap
    {
       display: table;
       table-layout: fixed; /* it'll enable the div to be responsive */
       width: 100%; /* as display table will shrink the div to content-wide */
    }
    

    WARNING: But this theory won't work for holder contains inner elements with absolute positions. And it will create problem for fixed-width layout. But for responsive design, it's just excellent. :)

    ADDITION TO Meep3D's ANSWER

    With CSS3, in a dynamic portion, you can add clear float to the last element by:

    #wrap [class*='my-div-class']:last-of-type {
      display: block;
      clear: both;
    }
    

    Where your divs are:

    <div id="wrap">
    <?php for( $i = 0; $i < 3; $i++ ) {
       <div class="my-div-class-<?php echo $i ?>>
          <p>content</p>
       </div> <!-- .my-div-class-<?php echo $i ?> -->
    }
    </div>
    

    Reference:

    • :last-of-type - CSS-TRICKS
    0 讨论(0)
  • 2020-12-07 15:58

    Here i show you a snippet where your problem is solved (i know, it's been too long since you posted it, but i think this is cleaner than de "clear" fix)

    #nav
            {
                float: left;
                width: 25%;
                height: 150px;
                background-color: #999;
                margin-bottom: 10px;
            }
    
            #content
            {
                float: left;
                margin-left: 1%;
                width: 65%;
                height: 150px;
                background-color: #999;
                margin-bottom: 10px;
            }       
            #wrap
            {
              background-color:#DDD;
              overflow: hidden
            }
        <div id="wrap">
        <h1>wrap1 </h1>
        <div id="nav"></div>
        <div id="content"><a href="index.htm">&lt; Back to article</a></div>
        </div>

    0 讨论(0)
  • 2020-12-07 16:02

    It's a common problem when you have two floats inside a block. The best way of fixing it is using clear:both after the second div.

    <div style="display: block; clear: both;"></div>
    

    It should force the container to be the correct height.

    0 讨论(0)
  • 2020-12-07 16:04

    Aside from the clear: both hack, you can skip the extra element and use overflow: hidden on the wrapping div:

    <div style="overflow: hidden;">
        <div style="float: left;"></div>
        <div style="float: left;"></div>
    </div>
    
    0 讨论(0)
  • 2020-12-07 16:05

    overflow:hidden (as mentioned by @Mikael S) doesn't work in every situation, but it should work in most situations.

    Another option is to use the :after trick:

    <div class="wrapper">
      <div class="col"></div>
      <div class="col"></div>
    </div>
    
    .wrapper {
      min-height: 1px; /* Required for IE7 */
      }
    
    .wrapper:after {
      clear: both;
      display: block;
      height: 0;
      overflow: hidden;
      visibility: hidden;
      content: ".";
      font-size: 0;
      }
    
    .col {
      display: inline;
      float: left;
      }
    

    And for IE6:

    .wrapper { height: 1px; }
    
    0 讨论(0)
提交回复
热议问题