容器图文布局求助:调整图文与容器边距及元素间距
解决布局问题:图片贴容器右侧、文本贴容器左侧并保留合理间距
现有代码的问题分析
- HTML标签嵌套错误:存在
<h2>嵌套<h2>、<p>嵌套<p>的非法结构,会导致浏览器解析异常 - CSS语法与逻辑错误:
gap: 45缺少单位,CSS中必须指定长度单位(如px)padding-left:none写法无效,CSS里没有none这个padding值,需用0替代- 文本容器的
padding重复设置,且冗余的右侧/顶部padding导致文本无法贴近左侧边框 - 间距设置位置错误,把
gap放在图片容器内无法实现两个区块的间距控制
修正后的完整代码实现
修正后的HTML
<body> <div style="display: flex; gap: 45px;"> <div class="about-us-image"> <img class="about-image" src="file:///C:/xampp/htdocs/templatemo_591_villa_agency/assets/images/smiling-handsome-asian-manager-with-modern-device.jpg" alt="smiling-handsome-asian-manager-with-modern-device"> </div> <div class="about-us-text"> <h2 style="color: #56aeff; font-size: 40px; font-family: Arial;">Who We Are</h2> <p style="font-size:20px;">With more than a decade of experience, we are a trusted team of business and financial planners to help you start your business or retirement in Bali, Indonesia and other islands</p> </div> </div> </body>
修正后的CSS
.about-us-image { display: flex; justify-content: flex-end; align-items: center; width: 50%; } .about-image { height: 500px; width: 400px; box-sizing: border-box; } .about-us-text { text-align: left; width: 50%; box-sizing: border-box; align-self: start; padding: 0; }
关键调整说明
- 将两个区块的间距
gap: 45px移至父级flex容器,精准控制图片区与文本区的间距 - 修复HTML标签嵌套问题,保证结构合法
- 重置文本容器的padding为
0,让文本完全贴近自身容器左侧;图片容器通过justify-content: flex-end实现图片贴右侧边框 - 修正CSS语法错误,移除无效样式,保证样式正常生效
内容的提问来源于stack exchange,提问作者Samuel
相关产品推荐
相关产品推荐

