css 实现三栏布局(圣杯布局)的5种方法
效果图:
1、浮动:设置float: left;,配合负margin来实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | .left { float: left; width: 100px; height: 200px; background-color: red; } .right { float: right; width: 100px; height: 200px; background-color: yellow; } .main { background-color: green; height: 200px; margin-left: 120px; margin-right: 120px; } .container { border: 1px solid black; } < div class="container"> < div class="left"></ div > < div class="right"></ div > < div class="main">float布局、float布局、</ div > </ div > |
2、圣杯布局:两端宽度固定,中间自适应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | .container1 { padding-left: 100px; padding-right: 100px; } .main1 { float: left; width: 100%; height: 200px; background-color: blue; } .left1 { float: left; width: 100px; height: 200px; margin-left: -100%; position: relative; left: -100px; background-color: red; } .right1 { float: left; width: 100px; height: 200px; margin-left: -100px; position: relative; right: -100px; background-color: yellow; } < div class="container1"> < div class="main1">圣杯布局、圣杯布局、圣杯布局、圣杯布局、圣杯布局、圣杯布局</ div > < div class="left1"></ div > < div class="right1"></ div > </ div > |
3、双飞翼布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | .main2 { float: left; width: 100%; } .content2 { height: 200px; margin-left: 110px; margin-right: 110px; background-color: green; } .left2 { float: left; height: 200px; width: 100px; margin-left: -100%; background-color: red; } .right2 { width: 100px; height: 200px; float: left; margin-left: -100px; background-color: blue; } /* 清除浮动 */ .container2::after { display: block; content: ''; font-size: 0; height: 0; clear: both; zoom: 1; } < div class="container2"> < div class="main2"> < div class="content2">双飞翼布局、双飞翼布局、双飞翼布局、双飞翼布局、双飞翼布局、双飞翼布局</ div > </ div > < div class="left2"></ div > < div class="right2"></ div > </ div > |
4、flex布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | .container3 { display: flex; height: 200px; } .left3 { flex: 0 100px; background-color: red; } .main3 { flex: 1; background-color: blue; } .right3 { flex: 0 100px; background-color: green; } < div class="container3"> < div class="left3"></ div > < div class="main3">flex布局</ div > < div class="right3"></ div > </ div > |
5、绝对定位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | .container4 { height: 200px; } .container4 div { height: 200px; } .left4 { position: absolute; left: 0px; width: 100px; background-color: red; } .main4 { position: absolute; left: 100px; right: 100px; background-color: yellow; } .right4 { position: absolute; right: 0px; width: 100px; background-color: green; } < div class="container4"> < div class="left4"></ div > < div class="main4">绝对定位、绝对定位、绝对定位、绝对定位、绝对定位 </ div > < div class="right4"></ div > </ div > |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)