我希望.container的内容以中心为中。我正在使用Float:左属性来创建画廊。它也是调整和裁剪缩略图大小的代码的一部分。
根本有任何方法?
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>