Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically stack divs in different columns css only

Tags:

css

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!

like image 982
kaljak Avatar asked Jul 13 '13 16:07

kaljak


People also ask

How do I stack two columns in CSS?

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.

How do I show DIVS vertically?

Just remove absolute positioning. Center the divs using margin:auto and then provide whatever vertical margins you like.

Why are my DIVS stacking on top of each other?

@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.

How do you stack divs horizontally?

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.


3 Answers

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 divs.

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).

like image 98
Daniel Johansson Avatar answered Sep 27 '22 21:09

Daniel Johansson


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>
like image 39
user1721135 Avatar answered Sep 27 '22 21:09

user1721135


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.

like image 22
Mohamad Avatar answered Sep 27 '22 22:09

Mohamad