gpt4 book ai didi

html - 使用 flexbox 如何在 div 下居中文本

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:11 25 4
gpt4 key购买 nike

我正在尝试使用 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>

预览

preview

为了说明文本居中,请参阅:

border

关于html - 使用 flexbox 如何在 div 下居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50865257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com