3

I have 6 elements which should result in two rows of 3 elements each, so I've floated them. But the content of the elements varies quite a bit, and the layout breaks when one taller element prevents subsequent siblings from floating all the way left:

Floated elements breaking layout

Here is example CSS:

figure { width: 30%; float: left; margin-left: 1%; font-size: small; outline: solid #999 1px; }
img { max-width: 100%; }

and HTML:

<figure>
  <img src="http://placekitten.com/150/200?image=1" alt="Kitten 1" />
  <figcaption>Bacon ipsum dolor sit amet short ribs pork chop pork belly spare ribs shoulder tri-tip beef ribs turkey brisket short loin tenderloin ground round. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=2" alt="Kitten 2" />
  <figcaption>Short ribs cow corned beef, beef tenderloin swine biltong short loin. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=3" alt="Kitten 3" />
  <figcaption>Boudin chuck ground round, pig pastrami salami turkey ham hock beef ribs tongue. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=4" alt="Kitten 4" />
  <figcaption>Tri-tip pork loin tongue corned beef shankle ball tip. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=5" alt="Kitten 5" />
  <figcaption>Turkey swine tenderloin spare ribs sausage filet mignon hamburger. Leberkas andouille prosciutto, bresaola tri-tip short loin meatloaf shank pig shoulder spare ribs ribeye. </figcaption>
</figure>
<figure>
  <img src="http://placekitten.com/150/200?image=6" alt="Kitten 6" />
  <figcaption>Pastrami andouille tongue tri-tip jerky.</figcaption>
</figure>

And an example JSFiddle: http://jsfiddle.net/KatieK/5Upbt/

How can I get second row of figure elements to line up below the first 3 elements?

HTML/CSS solutions are preferable to JavaScript / jQuery solutions.

4
  • Wrap every 3 figures in a DIV?
    – nice ass
    Commented Feb 26, 2013 at 20:06
  • +1 for cute kitten photos. You need to to use the css clear: left to start the floating all the way on the left again.
    – Brad M
    Commented Feb 26, 2013 at 20:06
  • @BradM woudln't that push all the figures to the left, though?
    – crush
    Commented Feb 26, 2013 at 20:08
  • @KatieK I think you have to either do what One Trick Pony suggests, or you'd have to use jQuery to position them like Pinterest does.
    – crush
    Commented Feb 26, 2013 at 20:09

3 Answers 3

3

How about a CSS only solution? Add this rule:

figure:nth-of-type(3n+1) {
    clear:left;
}

jsFiddle example

0
3

You can use the :nth-child pseudo class to clear every fourth element.

figure:nth-child(4n){clear: left;}

EDIT:

4n doesn't quite cut it. 3n + 1 is what you want.

figure:nth-child(3n + 1){clear: left;}

http://jsfiddle.net/jMCng/1/

3
  • @j08691 You're right. (4n) wasn't giving rows of 3. I've updated it and the fiddle to use (3n + 1)
    – idrumgood
    Commented Feb 26, 2013 at 20:18
  • You sure? Looks like rows of 3 kitties to me.
    – idrumgood
    Commented Feb 26, 2013 at 20:27
  • Sorry. It is fine now. I don't know what I was thinking when I checked it last time. Commented Feb 26, 2013 at 20:35
2

Here is a solution I tested: http://jsfiddle.net/5Upbt/7/

I modify your figure style

figure { display: inline-block; vertical-align: top; width: 30%; margin-left: 1%; font-size: small; outline: solid #999 1px; }

This is based on the more general solution here: http://jsfiddle.net/bazmegakapa/Ft9d2/

1
  • FWIW, I'd rather not lock down the figure element to a specific size.
    – KatieK
    Commented Feb 26, 2013 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.