如何实现响应式布局:将文本分置竖线两侧且避免变形
解决方案
需求梳理
要实现:
- "Education" 标题固定在左侧
- 教育经历内容对应放在竖线右侧
- 响应式布局,避免页面缩放时布局变形
修改后的HTML代码
<div class="flex-container"> <div id="curriculumID" class="curriculum"> <h1>Curriculum</h1> <div class="education-section"> <h1 class="education-title">Education</h1> <div class="vertical-line"></div> <div class="education-content"> <!-- 第一份教育经历 --> <div class="education-item"> <p class="years">2020-Present</p> <h2>College in my area</h2> <div class="item-details"> <img src="https://i.ibb.co/Jjy7qVv/logoIsep.png" alt="ISEP Icon" /> <p>Pursuing a degree in some Engineering.</p> </div> </div> <!-- 分隔线 --> <hr /> <!-- 第二份教育经历 --> <div class="education-item"> <p class="years">2017-2020</p> <h2>Escola Secundária de somewhere</h2> <div class="item-details"> <img src="https://i.ibb.co/5v52S84/Logo-Boa-Nova.png" alt="ESBN Icon" /> <p>Completed highschool in Science and Technology.</p> </div> </div> </div> </div> </div> </div>
修改后的CSS代码
.flex-container { display: flex; flex-direction: column; align-items: center; } .curriculum { background-color: #182436; padding: 30px; margin: 25px; border-radius: 15px; width: 90%; max-width: 1000px; } .curriculum > h1 { color: #15faa6; font-weight: bold; font-size: 40px; text-align: center; margin: 0 0 30px; } /* 教育模块三栏布局 */ .education-section { display: grid; grid-template-columns: 180px 1px 1fr; gap: 20px; align-items: start; } .education-title { color: #f2f2f2; font-size: 30px; margin: 0; text-align: right; } /* 竖线分隔 */ .vertical-line { background-color: #f2f2f2; height: 100%; } .education-content { display: flex; flex-direction: column; gap: 25px; } .education-item { display: flex; flex-direction: column; gap: 10px; } .years { color: #aaaaaa; margin: 0; } .education-item h2 { color: #f2f2f2; margin: 0; font-size: 22px; } .item-details { display: flex; align-items: center; gap: 15px; } .item-details img { width: 50px; height: auto; } .item-details p { color: #f2f2f2; margin: 0; } .education-content hr { border: none; border-top: 1px solid #f2f2f2; margin: 0; } /* 移动端响应式适配 */ @media (max-width: 768px) { .education-section { grid-template-columns: 1fr; grid-template-rows: auto 1px auto; } .education-title { text-align: center; } .vertical-line { height: 1px; width: 100%; } }
实现思路
- Grid布局解决两侧分栏:用Grid实现左侧标题、竖线、右侧内容的三栏结构,替代容易变形的绝对定位,天然支持响应式缩放。
- 模块化封装:把每份教育经历封装成独立容器,统一样式逻辑,避免布局混乱。
- 响应式适配:小屏幕下自动切换为上下布局,竖线转为水平线,保证移动端显示正常。
- 精简冗余样式:删除原代码中
float、inline-block等易引发布局问题的属性,用Flex和Grid实现更稳定的布局。
内容的提问来源于stack exchange,提问作者Alexandre Gomes
相关产品推荐
相关产品推荐

