Can I order my CSS columns horizontally instead of vertically?
Asked Answered
C

5

30

Here is my code:

.column {
    column-count: 4;
    column-gap: 10px;
    -moz-column-count: 4;
    -moz-column-gap: 10px;
    -webkit-column-count: 4;
    -webkit-column-gap: 10px;
}
<div class="column">
    <div class="inner">1</div>
    <div class="inner">2</div>
    <div class="inner">3</div>
    <div class="inner">4</div>
    <div class="inner">5</div>
    <div class="inner">6</div>
    <div class="inner">7</div>
    <div class="inner">8</div>
    <div class="inner">9</div>
    <div class="inner">10</div>
    <div class="inner">11</div>
    <div class="inner">12</div>
</div>

The result is:
1 4 7 10
2 5 8 11
3 6 9 12

What I want is:
1 2 3 4
5 6 7 8
9 10 11 12

Is that possible? How should I make it?

Catafalque answered 26/6, 2013 at 14:56 Comment(3)
This post might be somewhat helpful: css-tricks.com/forums/discussion/23914/…Keijo
This really helpful actuallyCatafalque
The link is dead, any chance you have an offline version of it?Limnology
T
17

Is even easier:

.inner:nth-child(4n+1) {
    clear: left;
}

.inner {
    float: left;
    margin: 5px;
}
<div class="column">
    <div class="inner">1</div>
    <div class="inner">2</div>
    <div class="inner">3</div>
    <div class="inner">4</div>
    <div class="inner">5</div>
    <div class="inner">6</div>
    <div class="inner">7</div>
    <div class="inner">8</div>
    <div class="inner">9</div>
    <div class="inner">10</div>
    <div class="inner">11</div>
    <div class="inner">12</div>
</div>

You can apply float: left and clear float: left every 4n+1 elements.

Ref:

:nth-child

Tyler answered 25/9, 2014 at 18:12 Comment(5)
I found this solution applied nicely also to a long <ul> that I needed to display in two horizontal columns.Indira
This is okay but it doesn't allow for different height elements.March
Love this solution. Any way of allowing different height?Purkey
I couldn't make this work with <ul> - does it need some extra work, and what do you need to apply to <ul> and <li> respectively?Champignon
Also I think this solution is not very friendly to responsive design. If say you choose 4 columns, but then you make the browser window less wide to only fit 3 or 2 columns, then items will be misaligned and you'll have gaps with no items. Which makes sense, since what then physically fits on the screen no longer matches the (4n+1).Champignon
G
1

Mansonry.js is the solution. Check this out http://masonry.desandro.com/ Also check out this demo. This is what you need i guess.. I am also trying to implement such thing :) http://tympanus.net/Development/GridLoadingEffects/index2.html

As far as I know there's no way to do this with CSS only which is sad

Gudrun answered 20/3, 2014 at 3:17 Comment(0)
S
0

I actually put some effort forward this time. ignore everything from previous edit

the display property's inline-blocks is probably what you want to use.

Here is a thorough guide on how to use it

And here's a brief demo

li {
    width: 200px;
    display: inline-block;
}
Strength answered 26/6, 2013 at 15:1 Comment(5)
@CallMeAhLun You dont think that it would work or that a better solution exists? The former seems more likely which makes me sadStrength
I looking for a better solution and the answer you gave me isn't a better solution. I need work in CMS, I need it in dynamic.Catafalque
@CallMeAhLun well you should have mentioned that i'll do some research for yaStrength
@CallMeAhLun there ya go, thats a real answer this timeStrength
that is the different thing I want.Catafalque
C
-1

check my simple codes below url. https://github.com/JJ81/column-count maybe you can have what you want.

Counterbalance answered 22/5, 2014 at 6:25 Comment(1)
It's good to include the relevant code in your answer as links aren't future proof.Entomologize
O
-2

Apply this:

.column { 
  width: 100px; 
  overflow:hidden;
} 
.column .inner {
  width:  20px; 
  float:left; 
  text-align:center;
}
Oireachtas answered 25/9, 2014 at 17:51 Comment(1)
could you explain in the body of your answer?Persistent

© 2022 - 2024 — McMap. All rights reserved.