如何使浮动图片库居中

How to center a floated image gallery

我希望 .container 的内容居中。 我正在使用 float:left 属性 创建图库。它也是调整缩略图大小和裁剪代码的一部分。

有什么方法可以做到这一点?

CSS:

.grid-item { 
    float: left;
    width: 175px;
    height: 120px;
    overflow: hidden;
}
.grid-item img {
    display: block;
    max-width: 100%;
    border: 2px solid #fff;
    border-radius: 5px;
    margin: 0 auto;
}

HTML:

<div class="container text-center">
    <div id="links">
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
    </div>
</div>

您应该使用 flexbox 来对齐您的内容,您可以阅读 CSS 中的评论,了解 属性 的用途。希望对你有帮助。

#links{
display: flex;  /*Generates a flexbox layout with default flex direction as row */
  width: 100%; /* Not really required */
  align-items: center; /*Aligns contents vertically */
  justify-content: space-around; /*Aligns contents horizontally */
  text-align: center; /*Aligns further text in the center */
  flex-direction:row; /*By default its row, you can change to column for vertical alignment */
  flex-flow:row wrap; /*To wrap items in other row when no more can be fit in the same row*/
}

.grid-item {
  /* flex:1; If you need them to grow or shrink flexibly */
  width: 175px;
  height: 120px;
  overflow: hidden;
}

.grid-item img {
  display: block;
  max-width: 100%;
  border: 2px solid #fff;
  border-radius: 5px;
  margin: 0 auto;
}
<div class="container text-center">
  <div id="links">
    <a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
      <img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
    </a>
    <a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
      <img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
    </a>
    <a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
      <img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
    </a>
  </div>
</div>

通常当我 运行 遇到这种情况时,我会将容器中的所有内容嵌套在子容器中并根据自己的喜好进行调整:

.container .center {
  /* 
  This is just a way of centering the element,
  you can use whatever technique.
  */
  position:absolute;
  top:50%;
  left:50%;
  transform:translateX(-50%) translateY(-50%);
}
<div class="container text-center">
  <div class="center">
    <div id="links">
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
        <a href="img.jpg" title="" class="grid-item" data-gallery>
            <img src="img.jpg" alt="" class="img-rounded">
        </a>
    </div>
  </div>
</div>