user2849793
user2849793

Reputation: 53

Images align in columns

I am trying to align several images horizontally and vertically inside a WordPress gallery. I want to have a grid of 3 images in a row, each aligned in the middle.

Here is the structure:

<div class="gallery">
    <dl>
        <dt><img src="#" /></dt>
    </dl>
    <dl>
        <dt><img src="#" /></dt>
    </dl>
    <dl>
        <dt><img src="#" /></dt>
    </dl>
    <dl>
        <dt><img src="#" /></dt>
    </dl>
</div>

And what I have so far in CSS:

.gallery {
    display: table;
    width: 100%;
    height: 100%;
}

dl {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}

The centering works well so far. But it makes ans many columns as there are images. What I want is a new row after every third image.

Here is a jsfiddle of what I have done so far.

Thanks for any help!

Upvotes: 4

Views: 321

Answers (1)

Alex Char
Alex Char

Reputation: 33218

You can use display: inline-block and add width: 33% instead:

.gallery {
  display: table;
  width: 100%;
  height: 100%;
}
dl {
  display: inline-block;/*add display inline block*/
  text-align: center;
  vertical-align: middle;
  width: 33%;/*set width to 33%*/
}
<div class="gallery">
  <dl> <dt><img src="http://placehold.it/400x100" /></dt>

  </dl>
  <dl> <dt><img src="http://placehold.it/200x150" /></dt>

  </dl>
  <dl> <dt><img src="http://placehold.it/350x180" /></dt>

  </dl>
  <dl> <dt><img src="http://placehold.it/250x150" /></dt>

  </dl>
</div>

Upvotes: 1

Related Questions