0

I have bunch of left floated card like elements with set pixel of width and height. Each of the elements are large enough that it looks off when there's not enough space on the right to fit another, I get a big whitespace on the right. I want to even out the white space on the both sides.

With simple left floats, it looks like this:

[---][---][---]--
[---][---][---]--
[---][---][---]--
[---][---][---]--
[---][---]-------

what I instead want is something like this:

-[---][---][---]-
-[---][---][---]-
-[---][---][---]-
-[---][---][---]-
-[---][---]------

or this:

-[---][---][---]-
-[---][---][---]-
-[---][---][---]-
-[---][---][---]-
---[---][---]----

How can I achieve this with CSS, and if it is not possible, is there any simple way to do this with JavaScript ?

Robert C. Holland
  • 1,651
  • 4
  • 24
  • 57

1 Answers1

3

You can use display: inline-block instead for the cards, and use text-align: center in the parent.

.card {
  display: inline-block;
}

.parent {
  text-align: center;
}

That way, the cards will work just like wrapping, centered text.

Jacob
  • 77,566
  • 24
  • 149
  • 228