8

There's a similar question here with no real answer: CSS columns bug — 5 column count only showing 4 (with images)

I'm using column-count to display elements in columns (in this case a set of section elements but this happens regardless of the element used (obviously)).

The problem is that Chrome and Firefox (haven't tried others) don't always respect the specified column count.

If I set it to 4, sometimes it will be 4 and sometimes it will be less than 4 (never more, thankfully).

If I use Firebug (or similar) to modify the height of some of the elements in the columns sometimes the columns jump around from 3 to 4.

This is really strange and really annoying and I'm hoping someone knows why this happens and hopefully how to fix it.

div {
    -moz-column-count: 4;
    -webkit-column-count: 4;
    column-count: 4;
}
div img {
    display: block;
    margin: 0 0 10px;
}
<div>
    <img src="http://placehold.it/80x80">
    <img src="http://placehold.it/80x80">
    <img src="http://placehold.it/80x80">
    <img src="http://placehold.it/80x80">
    <img src="http://placehold.it/80x80">
</div>

Here's a JSFiddle displaying the problem: http://jsfiddle.net/NY2Zx/ you can play around with the dimensions of the images to see the column count change.

TylerH
  • 20,799
  • 66
  • 75
  • 101
powerbuoy
  • 12,460
  • 7
  • 48
  • 78

5 Answers5

4

In your example (jsfiddle), there are 5 elements of equal size to be distributed into 4 columns. Since they won't fit next to each other (they are more than 4) , the first column will contain 2 elements. That defines the height of the container, so the second column will also get 2 elements, and so there's one remaining for the third column and none for the fourth column. There are four columns, but the fourth one is simply empty...

In other words: The height of the container is determined by the minimum height which is needed to fit all elements into the number of columns. Once that is done, the content will be filled into the columns starting from the left, and each column will get as much content as fits into it.

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • 2
    Right, one would hope if I ask for 4 columns, I'd get 4 columns :) – powerbuoy May 02 '17 at 12:17
  • 1
    well, if you have 7 elements, you'll see the 4 columns... They are there, after all, the fourth one is simply empty. – Johannes May 02 '17 at 12:18
  • 2
    I trust that this is correct so I'll give you the answer, but imo this makes column-count pretty much useless for many things. – powerbuoy May 02 '17 at 14:10
0

here work example, but with image wrap element.

.wrap {    
    -webkit-columns: 4 auto;
    -moz-columns: 4 auto;
    columns: 4 auto;
}

.wrap .img {
    display: block;
    margin: 0 0 10px;
    line-height: 0px;
}

​If change line-height then error there again.

http://jsfiddle.net/NY2Zx/4/

Mark Nolan
  • 85
  • 4
  • 1
    This (setting line-height to 0 on the column elements) actually seems to work - but why? :) Edit: Or... hmm. It doesn't work here: http://jsfiddle.net/NY2Zx/ and it actually doesn't work in my real world case. I found that setting the width to a px value rather than % works however. This almost seems random. – powerbuoy May 12 '12 at 12:44
  • This solution is not really working. We have the same issue if use delete one item: we see: 2 + 2 +2 + 0 elements. – Susana Sanz Jun 21 '21 at 08:56
0

check this http://www.w3.org/TR/css3-multicol/#pseudo-algorithm

JSFiddle example is working fine if written in local html file and loaded in firefox and chrome. Try specifying width to div element.

Thanos
  • 771
  • 7
  • 10
0

For whatever reason, I'm not sure why, having an empty paragraph trailing the text will reflow columns again. This is not an ideal fix, as it should flow without empty elements, but it is a possible quick fix I've encountered, for someone who is having this problem still.

creativename
  • 616
  • 1
  • 6
  • 7
-1

Try adding orphans:

div {
    // ...
    orphans: 1;
}
Peter
  • 4,021
  • 5
  • 37
  • 58
  • What is this responding to? There is no `.item` class in OP's code. This also has no effect on the code. – TylerH Mar 21 '23 at 16:05
  • Edited. Adding `orphans: 1;` to the container solved this exact problem in my code. Hopefully it helps someone in the future. You're correct that it does not change the example. – Peter Mar 21 '23 at 18:27