时间:2021-07-01 10:21:17 帮助过:38人阅读
前面已经介绍过单列定宽单列自适应和两列自适应的两列布局。本文介绍三列布局,分为两侧定宽中间自适应、两列定宽一侧自适应、中间定宽两侧自适应、一侧定宽两列自适应和三列自适应这五种情况
【1】float + margin + calc
<style> p{margin: 0;} .parent{overflow: hidden;} .left,.right{float: left;width: 100px;} .center{float: left; width:calc(100% - 240px);margin: 0 20px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
【2】float + margin + (fix)
<style> p{margin: 0;} .parent{overflow: hidden;} .left,.right{position: relative;float: left;width: 100px;} .centerWrap{float: left; width:100%; margin: 0 -100px;} .center{margin: 0 120px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="centerWrap" style="background-color: red;"> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
【1】inline-block + margin + calc
<style> p{margin: 0;} .parent{font-size: 0;} .left,.right,.center{display:inline-block; vertical-align: top;font-size: 16px;} .left,.right{width: 100px;} .center{width: calc(100% - 240px); margin: 0 20px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
【2】inline-block + margin + (fix)
<style> p{margin: 0;} .parent{font-size: 0;} .left,.right,.centerWrap{display:inline-block; vertical-align: top;font-size: 16px;} .left,.right{width: 100px;position:relative;} .centerWrap{width: 100%; margin: 0 -100px;} .center{margin: 0 120px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="centerWrap" style="background-color: orange;"> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
<style> p{margin: 0;} .parent{display: table; width: 100%;table-layout: fixed;} .left,.right,.centerWrap{display:table-cell;} .left,.right{width: 100px;} .center{margin: 0 20px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="centerWrap" style="background-color: orange;"> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
<style> p{margin: 0;} .parent{position: relative;height:40px;} .left,.right,.center{position: absolute;} .left{left: 0;width:100px;} .right{right: 0;width: 100px;} .center{left: 120px; right: 120px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
<style> p{margin: 0;} .parent{display: flex;} .left,.right{width: 100px;} .center{flex: 1; margin: 0 20px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> div> div>
这种布局与单列定宽单列自适应布局非常相似
【1】float + margin
缺点是IE6-浏览器的3px的bug,以及当自适应列中有元素清除浮动时,会使该元素不与左侧浮动元素同行,从而出现文字下沉现象
<style> p{margin: 0;} .parent{overflow: hidden;} .left,.center{float: left;width: 100px;margin-right: 20px;} .right{margin-left: 240px;} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> <p>rightp> div> div>
【2】float + margin + calc
<style> p{margin: 0;} .parent{overflow: hidden;} .left,.center{float: left;width: 100px;margin-right: 20px;} .right{float: left; width: calc(100% - 240px);} style>
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>leftp> div> <div class="center" style="background-color: pink;"> <p>centerp> div> <div class="right" style="background-color: lightgreen;"> <p>rightp> <p>rightp> div> div>
【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;} .right{margin-left: