【问题标题】:Images align in columns图像在列中对齐
【发布时间】:2015-07-16 17:32:52
【问题描述】:

我正在尝试在 WordPress 画廊中水平和垂直对齐多个图像。我想要一个连续 3 个图像的网格,每个图像都在中间对齐。

结构如下:

<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>

到目前为止,我在 CSS 中所拥有的:

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

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

到目前为止,居中效果很好。但它会产生与图像一样多的列。我想要的是每第三张图片之后的新行。

这是我迄今为止所做的jsfiddle

感谢您的帮助!

【问题讨论】:

    标签: html css alignment


    【解决方案1】:

    您可以使用display: inline-block 并添加width: 33% 代替:

    .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>

    【讨论】:

    • 很高兴我帮助了你。
    猜你喜欢
    • 2013-03-08
    • 2012-07-10
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多