I have divs with same width but different heights, they should be displayed in columns, floating up to the next div above them.
Fiddle: http://jsfiddle.net/kaljak/DAYSk/
Is there any way to handle that without placing them via Javascript?
One idea of mine would be to have divs for the columns and place the divs into them, but is there another possibility without adding more markup?
Thanks in advance!
Using CSS media queries You can create two columns simply by creating a two-column table and then using classes to have the columns stack. The block class turns the table cells into blocks on mobile, and the content behaves accordingly and automatically stacks on mobile. This results in a normal stack.
Just remove absolute positioning. Center the divs using margin:auto and then provide whatever vertical margins you like.
@giwook Basically what's happening is that you have bottom: 0 which will snap the content to the bottom of the page. When you scale the page down, the bottom of the page becomes closer to the header at which point the content will continue upward to cover the header.
To align div horizontally, one solution is to use css float property. But a better solution is to to use CSS display:inline-block on all divs which needs to be aligned horizontally and place them in some container div.
Another possible solution is to use CSS columns. Apply the columns property to the container
body {
-moz-column-width: 100px;
-webkit-column-width: 100px;
column-width: 100px;
}
Then use display: inline-block;
on the div
s.
Example: http://jsfiddle.net/tdwZe/
See also https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_multi-column_layouts
Note: CSS columns are not supported in IE older than IE10 (http://caniuse.com/#feat=multicolumn).
You can, but you would need to place those boxes in parent boxes
demo http://jsbin.com/ozazat/3/edit
For example 3 columns, each holding as many boxes as you wish:
div div {
background-color:red;
margin:10px;
}
.col1, .col2, .col3 {
width:100px;
float:left;
border:solid;
}
HTML
<div class="col1">
<div>text</div>
<div>text</div>
<div>text</div>
</div>
<div class="col2">
<div>text<br>text</div>
<div>text</div>
</div>
<div class="col3">
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
<div>text</div>
</div>
Yes. You can use absolute positions. Assuming you know the heights of your divs. This solution does not work well for dynamic content.
You will need to assign a class to each div, then provide the relevant CSS to position it correctly.
div1 {
height: 100;
position: absolute;
left: 0;
top: 0;
}
div2 {
height: 100;
position: absolute;
left: 0;
top: 100px; /* The height of the div above it */
}
Etc...
If you do have dynamic content, it may be better to use JavaScript, in particular the jQuery Masonry Plugin.
Another way to do this is, like you mentioned, stack your divs in columns.
See this question for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With