css三栏布局详解(CSS三列布局的多种表现形式)
类别:Web前端 浏览量:1387
时间:2021-11-08 16:19:59 css三栏布局详解
CSS三列布局的多种表现形式一、两侧定宽中间自适应布局
思路一: float
【1】float + margin + calc
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.rightright{float: left;width: 100px;}
- .center{float: left; width:calc(100% - 240px);margin: 0 20px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
【2】float + margin + (fix)
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.rightright{position: relative;float: left;width: 100px;}
- .centerWrap{float: left; width:100%; margin: 0 -100px;}
- .center{margin: 0 120px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="centerWrap" style="background-color: red;">
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
思路二: inline-block
【1】inline-block + margin + calc
- <style>
- p{margin: 0;}
- .parent{font-size: 0;}
- .left,.rightright,.center{display:inline-block; vertical-align: top;font-size: 16px;}
- .left,.rightright{width: 100px;}
- .center{width: calc(100% - 240px); margin: 0 20px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
【2】inline-block + margin + (fix)
- <style>
- p{margin: 0;}
- .parent{font-size: 0;}
- .left,.rightright,.centerWrap{display:inline-block; vertical-align: top;font-size: 16px;}
- .left,.rightright{width: 100px;position:relative;}
- .centerWrap{width: 100%; margin: 0 -100px;}
- .center{margin: 0 120px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="centerWrap" style="background-color: orange;">
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
思路三: table
- <style>
- p{margin: 0;}
- .parent{display: table; width: 100%;table-layout: fixed;}
- .left,.rightright,.centerWrap{display:table-cell;}
- .left,.rightright{width: 100px;}
- .center{margin: 0 20px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="centerWrap" style="background-color: orange;">
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
思路四: absolute
- <style>
- p{margin: 0;}
- .parent{position: relative;height:40px;}
- .left,.rightright,.center{position: absolute;}
- .left{left: 0;width:100px;}
- .rightright{rightright: 0;width: 100px;}
- .center{left: 120px; rightright: 120px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
思路五: flex
- <style>
- p{margin: 0;}
- .parent{display: flex;}
- .left,.rightright{width: 100px;}
- .center{flex: 1; margin: 0 20px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- </li>
- </li>
二、两列定宽一侧自适应布局
这种布局与单列定宽单列自适应布局非常相似
思路一: float
【1】float + margin
缺点是IE6-浏览器的3px的bug,以及当自适应列中有元素清除浮动时,会使该元素不与左侧浮动元素同行,从而出现文字下沉现象
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.center{float: left;width: 100px;margin-right: 20px;}
- .rightright{margin-left: 240px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- <p>right</p>
- </li>
- </li>
【2】float + margin + calc
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.center{float: left;width: 100px;margin-right: 20px;}
- .rightright{float: left; width: calc(100% - 240px);}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- </li>
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- <p>right</p>
- </li>
- </li>
【3】float + margin + (fix)
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.center{position: relative; float: left;width: 100px;margin-right: 20px;}
- .rightWrap{float: left; width: 100%; margin-left: -240px;}
- .rightright{margin-left:240px;}
- </style>
- <li class="parent" style="background-color: lightgrey;">
- <li class="left" style="background-color: lightblue;">
- <p>left</p>
- </li>
- <li class="center" style="background-color: pink;">
- <p>center</p>
- </li>
- <li class="rightWrap">
- <li class="right" style="background-color: lightgreen;">
- <p>right</p>
- <p>right</p>
- </li>
- </li>
- </li>
【4】float + overflow
- <style>
- p{margin: 0;}
- .parent{overflow: hidden;}
- .left,.center{position: relative; float: left;width: 100px;margin-right: 20px;}
- .rightright{overflow: hidden;zoom:1;}
- </style>
- <li class=
热门推荐
- 微信小程序开发如何实现自动保存(微信小程序开发实用技巧之数据传递和存储)
- 浅谈Python3中strip()、lstrip()、rstrip()用法详解(浅谈Python3中strip、lstrip、rstrip用法详解)
- extjs中treepanel属性和方法
- python用于机器人(python机器人运动范围问题的解答)
- dockerip访问限制(docker win ping 不通容器 避坑指南)
- eclipse配置python(eclipse创建python项目步骤详解)
- mysql怎么做教材信息管理系统(python+mysql实现学生信息查询系统)
- 云服务器哪个安全速度快(云服务器速度快不快?有哪些测试办法?)
- docker获取自己的容器id(docker 如何添加证书)
- nginxlocation匹配参数(nginx location优先级的深入讲解)