Flexbox布局问题:如何实现标题位于图标下方的三列横向排列
问题解决:Flexbox实现图标+标题垂直排列的3列区块
问题分析
你当前的核心问题是没有将每个图标与对应的标题封装为独立的垂直容器:原代码中图标(.square)和标题(.title)是平级的兄弟元素,都处于横向排列的.font容器中,自然会左右并排显示。无论是把标题放在容器外还是同一区块,都没解决「图标+标题作为一个整体」的布局逻辑。
修复方案
给每个图标+标题组合添加独立的容器,让容器内部采用垂直Flex布局,父容器保持横向排列3个这样的组合容器即可。以下是修改后的完整代码:
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,-50..200" rel="stylesheet" /> <style type="text/css"> #home-article container{ height: 1000px; } .main_txt{ margin-top: 0px; text-align: center; text-shadow: 3px 3px 3px #a9a9a9; font-size: 40px; } .main_txt h3{ margin-top: -8px; margin-bottom: -2px; font-weight: 400; } /* 父容器:横向排列3个feature-item */ .font{ margin-top: 10px; display: flex; flex-flow: row nowrap; gap: 10px; /* 替代margin,更灵活的间距控制 */ } /* 每个图标+标题的独立容器:内部垂直排列 */ .feature-item{ flex: 1; /* 3个容器均分父容器宽度 */ display: flex; flex-direction: column; align-items: center; /* 内部元素水平居中 */ } .square{ border-radius: 25px; width: 100%; /* 自适应父容器宽度 */ max-width: 360px; /* 限制最大宽度,避免太宽 */ height: 222px; background-color:#ab2e12; display: flex; align-items: center; justify-content: center; } .material-symbols-outlined { color: white; font-size: 200px; } .title{ margin-top: 15px; text-align: center; font-size: 18px; color: #333; } @media (max-width: 800px) { .font { flex-direction: column; /* 移动端改为纵向排列 */ } .square{ max-width: none; /* 移动端去掉最大宽度限制 */ } } </style> <div class="main_txt"> <h3>Centrum Zarządzania <strong>Nieruchomościami</strong></h3> </div> <div class="font"> <!-- 每个图标+标题包裹在feature-item里 --> <div class="feature-item"> <div class="square"><span class="material-symbols-outlined">attach_money</span></div> <div class="title">Brak prowizji</div> </div> <div class="feature-item"> <div class="square"><span class="material-symbols-outlined">campaign</span></div> <div class="title">Brak prowizji</div> </div> <div class="feature-item"> <div class="square"><span class="material-symbols-outlined">handshake</span></div> <div class="title">Brak prowizji</div> </div> </div>
关键改动说明
- 新增
.feature-item容器:将每个图标和标题封装为独立单元,内部设置flex-direction: column实现垂直排列,align-items: center确保内容水平居中,解决了移动端标题偏离的问题。 - 优化父容器间距:用
gap替代margin控制.font容器内子元素的间距,布局更整洁。 - 自适应宽度调整:给
.square设置width: 100%和max-width,兼顾桌面端的宽度限制和移动端的全屏显示。 - 简化媒体查询:移动端只需要把
.font改为纵向布局,.feature-item保持内部垂直排列即可,无需额外调整标题样式。
内容的提问来源于stack exchange,提问作者Konrad Kołak
相关产品推荐
相关产品推荐

