Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Floating Divs with different height are aligned with space between them

i am using left floating DIVs to simulate a two column layout (each div contains textfield to edit different data, like name, hobbies,...). So it should look like this

1 2
3 4
5 6

Now my div-boxes aren't always the same, since some DIVs have more elements than the other ones. Now my layout looks likes this

   1 2
     2 
   3 4
   5 6

You can also see the effect on this example if you scale your so that only four or three colums are shown. E.g. if 4 columns are shown in a row there is much space between Float 1 and Float 6. This doesn't look good on my UI. What I want is to have Float 6 following Float 1 with no space in between (except the margin I define)

Edit: My DIVs basically just contain a float:left and a width:40%, so that two fit on a screen

Here's a screenshot showing more alt text

like image 388
Tim Avatar asked Sep 02 '10 14:09

Tim


People also ask

Why is there a space between two divs?

Due to that floating , a white space has occurred between those two divs (between the div for header and div for main body). My question is, why this kind of problem is raising due to floating.? make sure your margin are all equal to 0 , so are the padding of your elements.

How do you make two divs float the same height?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.

How do I keep my divs the same height?

Place both DIVs into a container DIV that's set to 100% of page height and set both interior DIVS to 100%. They will fill up the container DIV and the height of both will be equal.


2 Answers

Here is a pure CSS solution. I took this example

Check it out if you want to learn more. He also use jQuery Masonry as fallback.

CSS:

.masonry { /* Masonry container */
    -moz-column-count: 4;
    -webkit-column-count: 4;
    column-count: 4;
    -moz-column-gap: 1em;
    -webkit-column-gap: 1em;
    column-gap: 1em;
}

.item { /* Masonry bricks or child elements */
    background-color: #eee;
    display: inline-block;
    margin: 0 0 1em;
    width: 100%;
}

HTML:

<div class="masonry">
    <div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
    <div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
    <div class="item">Incidunt sit unde minima in nostrum? Incidunt sit unde minima in nostrum?</div>
    <div class="item">Ducimus, voluptates, modi, delectus animi maiores consequuntur repellat quisquam fugiat eum possimus enim culpa totam praesentium magni quae!</div>
    <div class="item">Lorem ipsum dolor sit amet, dicta dolore adipisci hic ipsam velit deleniti possimus cumque accusantium rerum quibusdam.</div>
    <div class="item">Neque, vitae, fugiat, libero corrupti officiis sint facilis tempora quidem repudiandae praesentium odit similique adipisci aut.</div>
    <div class="item">Incidunt sit unde minima in nostrum?</div>
    <div class="item">Incidunt sit unde minima in unde minima in unde minima in nostrum?</div>
    <div class="item">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae, praesentium consequatur ducimus commodi quam ex illo omnis dicta reiciendis vel nesciunt deserunt aut sequi nam mollitia perferendis ipsam possimus temporibus!</div>
    <div class="item">Ab, adipisci, temporibus eaque quis harum perferendis incidunt cupiditate doloribus dolor numquam voluptates ipsum dolore aspernatur et voluptate ipsam beatae animi culpa.</div>
</div>

Hope this will help you. Thanks.

like image 104
AirBlaze Blaze Avatar answered Sep 19 '22 12:09

AirBlaze Blaze


The jQuery Masonry plugin will do exactly what you want.

If you wanted to stick with pure CSS, you could do something like the following, but I don't think it's what you're going for:

<div class="col">
   <div class="one"></div>
   <div class="three"></div>
   <div class="five"></div>
   <div class="seven"></div>
</div>
<div class="col">
   <div class="two"></div>
   <div class="four"></div>
   <div class="six"></div>
   <div class="eight">who do we appreciate</div>
</div>

And the CSS:

.col {
    float: left;
    width: 200px;
}
like image 32
Pat Avatar answered Sep 21 '22 12:09

Pat