浮动容器被浮动图片覆盖,砖形布局排版异常求助
砖形布局元素错位问题修复
问题描述
我尝试制作砖形布局,起初运行正常,但现在出现异常:要么放置图片时容器被渲染在下方,要么放置容器时图片被渲染在下方。调换容器与图片的位置后,图片仍未处于正确位置。
问题代码
CSS 代码
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; } body { background: white; color: black; font-family: verdana; font-size: 14px; } .center { text-align: center; } .containerinvis { width: 1150px; margin: auto; margin-top: 40px; margin-bottom: 20px; position: relative; } .filenamebar { background-color: #FFCDD9; padding: 5px; border:solid 2px #EAA2B3 } .filename { vertical-align: middle; color: #341D22; font-size: 12px; float:left; } .windowedclosed { float:right; width: 100px; box-sizing: border-box; text-align: right; } .clear:after { clear: both; content: ""; display: table; } .wrapper { background-color: #FFE1E7; padding: 20px; padding-top: 0px; border-style:solid; border-width: 0px 2px 2px 2px; border-color: #FFBFCE; position:relative; } .inside-header { text-align:center; padding:20px; } .box1 { background-color: white; margin: 10px; margin-bottom: 5px; padding: 10px; border-radius: 5px; height:145px; width:760px; float: left; } .box2 { background-color: white; margin: 10px; margin-top:5px; margin-left:10px; margin-right:5px; padding: 10px; border-radius: 5px; height:145px; width:375px; float: left; } .img-float-left { width: 300px; height: 300px; border-radius: 5px; overflow: hidden; float: left; margin: 10px; } .img-float-right { width: 375px; height: 300px; border-radius: 5px; overflow: hidden; float: left; margin: 10px; margin-top:5px; } .box-header1 { color:#FF7192; text-align:left; } .box-text1 { color:#FF7192; text-align:left; margin-top: 5px; } .butt-container { background-color: red; width: 600px; height: 150px; float: left; margin:10px; }
HTML 代码
<body> <!-- FILENAME BAR --> <div class="containerinvis"> <div class="filenamebar"> <div class="filename"> <p> hello-world!.mpreg</p> </div> <div class="windowedclosed"> <p> - ☐ X </p> </div> <div class="clear"></div> </div> <!-- THE MAIN WINDOW --> <div class="wrapper"> <div class="inside-header"> <h3> [future header] </h3> </div> <!-- 第一行:左图 + 右方两个盒子 --> <div class="clear"> <div class="img-float-left"> <img src="https://i.pinimg.com/564x/9e/71/d2/9e71d22b450cc722dca87ac4c3729536.jpg" alt="dangerous" width="300" height="300"> </div> <div class="right-col"> <div class="box1"> <div class="box-header1"> <h2> [lil header] </h2> </div> <div class="box-text1"> <p> text </p> </div> </div> <div class="box2"></div> </div> </div> <!-- 第二行:右图 + 按钮容器 --> <div class="clear"> <div class="img-float-right"> <img src="https://i.pinimg.com/564x/9e/71/d2/9e71d22b450cc722dca87ac4c3729536.jpg" alt="be afraid" width="375" height="300"> </div> <div class="butt-container"> <p> not in the right place</p> </div> </div> </div> </div> </body> </html>
修复说明
修正CSS语法错误:
- 修正拼写错误:
-webkkit-box-sizing改为-webkit-box-sizing - 补充单位:
font-size、text-size添加px单位,无效属性text-color替换为color - 移除冗余属性:浮动元素无需
display: inline-block,避免布局冲突
- 修正拼写错误:
分组浮动并清理浮动:
- 将同一行的元素放入独立容器,给容器添加
clear类清除浮动,避免元素错位 - 给按钮容器添加
margin,保证与其他元素间距统一
- 将同一行的元素放入独立容器,给容器添加
调整图片尺寸:
- 将右图
img标签宽度改为375px,与容器宽度匹配,防止溢出打乱布局
- 将右图
更优方案:用Flexbox替代浮动
浮动布局易出现清除浮动、错位等问题,推荐用Flexbox实现,代码更简洁稳定:
/* 替换原浮动相关CSS,使用Flex布局 */ .wrapper { background-color: #FFE1E7; padding: 20px; padding-top: 0px; border-style:solid; border-width: 0px 2px 2px 2px; border-color: #FFBFCE; position:relative; display: flex; flex-direction: column; gap: 10px; } .row { display: flex; gap: 10px; align-items: flex-start; } .right-col { display: flex; flex-direction: column; gap: 5px; } .box1 { background-color: white; padding: 10px; border-radius: 5px; height:145px; width:760px; } .box2 { background-color: white; padding: 10px; border-radius: 5px; height:145px; width:375px; } .img-float-left { width: 300px; height: 300px; border-radius: 5px; overflow: hidden; } .img-float-right { width: 375px; height: 300px; border-radius: 5px; overflow: hidden; } .butt-container { background-color: red; width: 600px; height: 150px; }
对应HTML只需将每行元素放入row容器,右侧两个盒子放入right-col容器即可,无需手动清理浮动,布局稳定性大幅提升。
内容的提问来源于stack exchange,提问作者Beau
相关产品推荐
相关产品推荐

