77

I'm trying to build my first responsive layout. I want to display list items in a vertical line, depending on width.

<div style="height:800px;">
    <ul>
       <li>1</li>
       <li>2</li>
       <li>3</li>
       <li>4</li>
       <li>5</li>
       <li>6</li>
       <li>7</li>
    </ul>
</div>
1   5
2   6
3   7
4

If the browser gets resized, I want it to become

1  4  7
2  5
3  6

Can someone help me? I've been trying for hours and I can't get anything. I've tried using tables but I can't do it like that either.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Grozav Alex Ioan
  • 1,559
  • 3
  • 18
  • 26
  • 2
    http://techneblog.com/article/creating-responsive-multiple-column-list might be what you are looking for - demo: http://codepen.io/ThiefMaster/full/afGbB (resize the window) – ThiefMaster Sep 08 '12 at 16:38

7 Answers7

112

This can be done using CSS3 columns quite easily. Here's an example, HTML:

#limheight {
    height: 300px; /*your fixed height*/
    -webkit-column-count: 3;
       -moz-column-count: 3;
            column-count: 3; /*3 in those rules is just placeholder -- can be anything*/
}

#limheight li {
    display: inline-block; /*necessary*/
}
<ul id = "limheight">
 <li><a href="">Glee is awesome 1</a></li>
 <li><a href="">Glee is awesome 2</a></li>
 <li><a href="">Glee is awesome 3</a></li>
 <li><a href="">Glee is awesome 4</a></li>    
 <li><a href="">Glee is awesome 5</a></li>
 <li><a href="">Glee is awesome 6</a></li>
 <li><a href="">Glee is awesome 7</a></li>
 <li><a href="">Glee is awesome 8</a></li>
 <li><a href="">Glee is awesome 9</a></li>
 <li><a href="">Glee is awesome 10</a></li>
 <li><a href="">Glee is awesome 11</a></li>
 <li><a href="">Glee is awesome 12</a></li>    
 <li><a href="">Glee is awesome 13</a></li>
 <li><a href="">Glee is awesome 14</a></li>
 <li><a href="">Glee is awesome 15</a></li>
 <li><a href="">Glee is awesome 16</a></li>
 <li><a href="">Glee is awesome 17</a></li>    
 <li><a href="">Glee is awesome 18</a></li>
 <li><a href="">Glee is awesome 19</a></li>
 <li><a href="">Glee is awesome 20</a></li>
</ul>
Soviut
  • 88,194
  • 49
  • 192
  • 260
Chris
  • 26,544
  • 5
  • 58
  • 71
  • 4
    http://caniuse.com/#feat=multicolumn - hopefully your audience allows you to use it anyway; it's clearly the best solution. You could also add a shim for older browsers - google spits out http://www.csscripting.com/css-multi-column/; haven't tested it though – ThiefMaster Sep 08 '12 at 16:42
  • @ThiefMaster Yeah, if older-browsers support is required, I think JavaScript would be the only way to achieve this. – Chris Sep 08 '12 at 16:47
  • Thank you very much! This is clearly what Im looking for. Also, thank you ThiefMaster for the cross-browser compatibility solution. – Grozav Alex Ioan Sep 08 '12 at 16:50
  • 8
    Seems `display: inline-block;` for `li` is not needed here. – Dmitry Nov 09 '13 at 19:23
  • 1
    Resizing doesn't work. The column items don't change columns. Besides that - even worse - if the page gets wider than 6x item size, two successive items (1 and 2) are displayed next to eachother on the same line in column 1, then on the next line 3 and 4, etc. You need to add some horizontal padding to the `
  • ` to get this clear. The problem it the `inline-block`. Remove that and you get three columns (in Firefox and Chrome at least).
  • – SPRBRN Apr 15 '14 at 12:46
  • 1
    Here is an updated version: http://jsfiddle.net/Fa722/270/ . Also I notice that if the number of elements is not divisible by the number of columns, it shakes when you re-size the window. (this will work for you @SPRBRN) – Francisco Corrales Morales Sep 17 '14 at 17:52
  • Can anyone please help to arrange Glee awesome 1 ,glee awesome 2 ,glee awesome 3 etc. horizontally the same way using column-count property? – monisha Oct 25 '17 at 07:16
  • You may need `width: 100%` for the `
  • ` if there are some "very short sentences" there.
  • – Anh-Thi DINH Nov 28 '20 at 16:16