我正在尝试使用 flexbox 将文本居中放置在圆形 div 下,但我无法弄清楚。我的目标是在圆圈中放置图标,下面是文本作为标题,但 h2 最终出现在圆圈旁边,我无法弄清楚如何将它放在圆圈的底部。感谢您的帮助!
html,
body {
margin: 0;
padding: 0;
width: 100%;
}
.flex-container {
display: flex;
flex-wrap: wrap;
width: 1000px;
align-content: space-between;
align-tems: center;
justify-content: center;
position: absolute;
}
.circle {
border-radius: 50%;
background: #252525;
width: 150px;
height: 150px;
border: 10px solid #69bf4a;
margin: 20px;
}
.flex-container>div:hover {
background-color: #69bf4a;
}
h2 {
position: absolute;
display: inline;
flex-direction: column;
justify-content: flex-start;
}
<div class="flex-container">
<div class="circle"></div>
<div>
<h2>T-SHIRTS</h2>
</div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
</div>
在 flex-box View 中不能有任意元素。所以我建议你给每个 flex 元素一个包装器:
<div class="flex-wrap">
<div class="circle"></div>
<div><h2>T-SHIRTS</h2></div>
</div>
现在,回到样式,如果您希望它们具有相同的大小,请使用这种方式:
html, body {
margin: 0;
padding: 0;
}
.flex-container {
display: flex;
flex-wrap: wrap;
width: 1000px;
align-content: space-between;
align-items: top;
justify-content: center;
}
.circle {
border-radius: 50%;
background: #252525;
width: 150px;
height: 150px;
border: 10px solid #69bf4a;
margin: 20px;
}
.flex-container > div:hover {
background-color: #69bf4a;
}
h2 {
text-align: center;
}
<div class="flex-container">
<div class="flex-wrap">
<div class="circle"></div>
<div><h2>T-SHIRTS</h2></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
<div class="flex-wrap">
<div class="circle"></div>
</div>
</div>
预览
为了说明文本居中,请参阅:
我是一名优秀的程序员,十分优秀!