如何在弹性项目(单元格)中居中图像
How to center an image within a flex item (cell)
我有一个 3x3 的 flexbox,每个 item/cell 都有一个图标和标题。我只是想水平居中图标(svg 图像)。我试过 justify-content: center;
但它似乎不起作用。任何人都可以告诉我如何在 flexbox 项目(或单元格,正如我所指的那样)中水平居中图像吗?下面的代码。
HTML:
<div class="grid">
<div class="cell">
<img src="images/cs_icon_01.svg">
<div class="service-title">Materials Handling, Warehousing & Storage Facility Management</div>
</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
</div>
CSS:
.grid {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
margin-bottom: 130px;
}
.cell {
flex: 0 0 30%;
height: 220px;
margin-bottom: 20px;
justify-content: center;
}
.grid img {
width: 45px;
}
.service-title {
display: inline-block;
font-family: Montserrat, Helvetica, Arial, sans-serif;
font-size: 16px;
font-weight: 300;
color: #4e4e4e;
line-height: 24px;
padding: 20px 16px 4px 16px;
text-align: center;
}
你可以试试经典的display
+margin
:
.grid img {
width: 45px;
display:block;
margin:auto;/* horizontal center effect */
}
或使用text-align:
.cell {
flex: 0 0 30%;
height: 220px;
margin-bottom: 20px;
/*justify-content*/ text-align: center;
}
我有一个 3x3 的 flexbox,每个 item/cell 都有一个图标和标题。我只是想水平居中图标(svg 图像)。我试过 justify-content: center;
但它似乎不起作用。任何人都可以告诉我如何在 flexbox 项目(或单元格,正如我所指的那样)中水平居中图像吗?下面的代码。
HTML:
<div class="grid">
<div class="cell">
<img src="images/cs_icon_01.svg">
<div class="service-title">Materials Handling, Warehousing & Storage Facility Management</div>
</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
<div class="cell">Content</div>
</div>
CSS:
.grid {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
margin-bottom: 130px;
}
.cell {
flex: 0 0 30%;
height: 220px;
margin-bottom: 20px;
justify-content: center;
}
.grid img {
width: 45px;
}
.service-title {
display: inline-block;
font-family: Montserrat, Helvetica, Arial, sans-serif;
font-size: 16px;
font-weight: 300;
color: #4e4e4e;
line-height: 24px;
padding: 20px 16px 4px 16px;
text-align: center;
}
你可以试试经典的display
+margin
:
.grid img {
width: 45px;
display:block;
margin:auto;/* horizontal center effect */
}
或使用text-align:
.cell {
flex: 0 0 30%;
height: 220px;
margin-bottom: 20px;
/*justify-content*/ text-align: center;
}