How to get different height html divs to float up

后端 未结 5 675
春和景丽
春和景丽 2020-12-30 02:36

I\'ve got a series of dynamically created divs of varying heights in a container div.

Varying text...<
5条回答
  •  悲&欢浪女
    2020-12-30 03:19

    If you want the divs to stack vertically in all browser agents, you'll need to wrap each 'section' in a containing element. Here's an example of what I mean.

    the css

    // let's reset our elements
    .site-container,
    .element-container,
    .my-element {
        margin: 0;
        padding: 0;
    }
    .site-container {
        display: block;
        width: 960px;
        margin: 0 auto; /* centers your site container on the page */
        clear: both; /* basic float clearing */
    }
    .element-container {
        display: inline-block;
        float: left;
        width: 300px; /* we'll have 3 sections width 10px spacing */
        margin-right: 10px;
    }
    .element-container.last {
        margin-right: 0;
    }
    .my-element {
        width: 280px; /* 300 - 20px [total padding] = 280px */
        margin-bottom: 10px; /* add a bottom margin */
        padding: 10px; /* makes our element 320px wide */
    }
    
    // make background-color classes
    .bg-red {
        background-color: #ff0000;
    }
    .bg-blue {
        background-color: #3b8acd;
    }
    

    the markup

    
    
    Vertical boxes!
    
    
        
    1
    2
    3
    4
    5
    6

    As far as the seventh div you have, I would suggest making it span across the entire site-container. It's aesthetically pleasing :)

提交回复
热议问题