两列布局(一列定宽)
左列定宽
float +margin
<style>
.parent {
background-color: grey;
width: 300px;
height: 200px;
}
.left {
float: left;
width: 100px; //需要定宽
height: 100%;
background-color: skyblue;
}
.right {
margin-left: 110px;
background-color: greenyellow;
}
</style>
<html>
<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</html>
absolute + margin
.parent{
position: relative;
}
.side{
position:absolute;
width:300px;
top:0px;
left:0px;
background:#F00;
}
.main{
background:#0FC;
margin-left:300px;
}
margin-left 负值 + float
<div class="parent">
<div class="left">
<p>left</p>
</div>
<!-- html部分在这个地方进行了添加,使用right-fix把原有结构包裹住了 -->
<div class="right-fix">
<div class="right">
<p >right</p>
<p>right</p>
</div>
</div>
</div>
.left{
float: left; width: 100px;
position: relative;
}
.right-fix{
float: right; width: 100%;
margin-left: -100px;
}
.right{
margin-left: 120px;
}
BFC
<style>
.parent {
background-color: grey;
width: 300px;
height: 200px;
}
.left {
float: left;
width: 100px; //需要定宽
height: 100%;
margin-right: 10px;
background-color: skyblue;
}
.right {
height: 100%;
background: #0FC;
overflow: hidden;
padding: 20px;
box-sizing: border-box;
}
</style>
<html>
<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</html>
table 父元素定宽高
//第一种,同时可以实现等高布局
<style>
.parent {
background-color: grey;
width: 300px;
height: 200px;
display: table;
table-layout: fixed;
}
.left {
display: table-cell;
width: 100px;
background-color: skyblue;
border-right: 10px solid transparent;
background-clip: padding-box; //设置列之间的间距
}
.right {
display: table-cell;
background-color: greenyellow;
}
</style>
<html>
<div class="parent">
<div class="left">
<p>left</p>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</html>
//第二种方法
<style>
.parent {
background-color: grey;
width: 300px;
height: 200px;
display: table;
table-layout: fixed;
}
.left-container {
display: table-cell;
width: 100px;
}
.left {
margin-right: 10px;
background-color: skyblue;
}
.right {
display: table-cell;
background-color: greenyellow;
}
</style>
<html>
<div class="parent">
<div class="left-container">
<div class="left">
<p>left</p>
</div>
</div>
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>
</html>
flex
<div class="parent">
<div class="side"></div>
<div class="main"></div>
</div>
.parent{
display: flex;
}
.side{
width: 200px;
height: 200px;
margin-right: 10px;
background: #555;
}
.main{
flex: 1;
background: #ddd;
}
三列布局
float + margin + calc
<style>
.parent{
overflow: hidden;
}
.left, .right{
float: left;
width: 100px;
}
.center{
float: left;
width:calc(100% - 240px);
margin: 0 20px;
}
</style>
<html>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
</html>
table
<style>
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.left,.right,.centerWrap{
display:table-cell;
}
.left,.right{
width: 100px;
}
.center{
margin: 0 20px;
}
</style>
<html>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="centerWrap" style="background-color: orange;">
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
</html>
圣杯布局
<style>
.wrap{
padding-left: 200px;
padding-right: 150px;
}
.main{
position: relative;
width: 100%;
float: left;
background: deeppink;
}
.aside{
position: relative;
width: 200px;
float: left;
margin-left: -100%;
background: pink;
right: 200px;
}
.ad{
position: relative;
width: 150px;
float: left;
margin-right: -150px;
background: pink;
}
</style>
<html>
<div class="wrap">
<div class="main"> main </div>
<div class="aside"> aside </div>
<div class="ad"> ad </div>
</div>
</html>
双飞翼布局
<style>
.main{
width: 100%;
float: left;
}
.main > .inner{
margin-left: 200px;
margin-right: 150px;
background: deeppink;
}
.aside{
width: 200px;
float: left;
margin-left: -100%;
background: pink;
}
.ad{
width: 150px;
float: left;
margin-left: -150px;
background: pink;
}
</style>
<html>
<div class="main">
<div class="inner"> main </div>
</div>
<div class="aside"> aside </div>
<div class="ad"> ad </div>
</html>
等高布局
负margin
<style>
.parent{
overflow: hidden;
}
.left,.centerWrap,.right{
float: left;
width: 50%;
padding-bottom: 9999px;
margin-bottom: -9999px;
}
.center{
margin: 0 20px;
}
.left,.right{
width: 25%;
}
</style>
<html>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="centerWrap">
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
</html>
table
<style>
.parent{
display: table;
width: 100%;
table-layout: fixed;
}
.left,.centerWrap,.right{
display: table-cell;
}
.center{
margin: 0 20px;
}
</style>
<html>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="centerWrap">
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
</html>
position
<style>
.parent{
position: relative;
height: 40px;
}
.left,.center,.right{
position: absolute;
top: 0;
bottom: 0;
}
.left{
left: 0;
width: 100px;
}
.center{
left: 120px;
right: 120px;
}
.right{
width: 100px;
right: 0;
}
</style>
<html>
<div class="parent" style="background-color: lightgrey;">
<div class="left" style="background-color: lightblue;">
<p>left</p>
</div>
<div class="center" style="background-color: pink;">
<p>center</p>
<p>center</p>
</div>
<div class="right" style="background-color: lightgreen;">
<p>right</p>
</div>
</div>
</html>
两端对齐布局
负margin
<style>
.container{
margin:50px 10px;
border-top:1px solid #000;
overflow: hidden;
}
.main{
margin-top:10px;
margin-right:-2%;
}
.item{
width:23%;
height:30px;
line-height: 30px;
text-align: center;
margin-right:2%;
box-sizing:border-box;
float:left;
border:1px solid #cbd1d0;
margin-bottom:10px;
}
</style>
<html>
<div class="container">
<div class="main">
<div class="item">头条</div>
<div class="item">推荐</div>
<div class="item">视频</div>
<div class="item">娱乐</div>
<div class="item">体育</div>
<div class="item">高考</div>
<div class="item">汽车</div>
<div class="item">科技</div>
<div class="item">图片</div>
</div>
</div>
</html>
text-align: justify
只适用于单列,如果多列,最后一行也可以用加入多个空元素的方法。
<style>
.main {
text-align: justify;
font-size: 0;
}
.main li {
display: inline-block;
text-align: center;
margin: 10px;
}
</style>
<html>
<div class="main">
<ul>
<li><div class="img"></div><span>1</span></li>
<li><div class="img"></div><span>2</span></li>
<li><div class="img"></div><span>3</span></li>
<li><div class="img"></div><span>4</span></li>
<li><div class="img"></div><span>5</span></li>
<li><div class="img"></div><span>6</span></li>
<li><div class="img"></div><span>7</span></li>
<li><div class="img"></div><span>8</span></li>
</ul>
</div>
</html>
水平垂直居中
水平居中
水平居中没有什么好说的啦,对于行内元素使用text-align;对于块级元素使用margin: auto(前提是已经为元素设置了适当的 width 宽度);
垂直居中
单行文本
line-height == height
多行文本
伪元素before/after
<div class="parent">
<span class="child">etttttttttttttttttttttttttttttttttttttttttttttgdfsffffffffffffffffffffffffffffffffffffffffffffffffff</span>
</div>
.parent {
height: 250px;
text-align: center;
border: 1px solid;
}
.parent:before {
content: " ";
height: 100%;
display: inline-block;
vertical-align: middle;
}
.child {
width: 200px;
display: inline-block;
word-wrap: break-word;
border: 1px solid;
vertical-align: middle;
}
行内元素
line-height
<div class="parent">
<img class="child" src="./images/7.png"/>
</div>
.parent {
line-height: 200px;
}
.child {
vertical-align: middle;
}
伪元素before/after
<div class="parent">
<img class="child" src="./images/7.png"/>
</div>
.parent {
height: 200px;
border: 1px solid;
}
.parent:before {
content: " ";
height: 100%;
display: inline-block;
vertical-align: middle;
}
.child {
vertical-align: middle;
}
table-cell ie8+
<div class="parent">
<img class="child" src="./images/7.png"/>
</div>
.parent {
height: 200px;
border: 1px solid;
display: table-cell;
vertical-align: middle;
}
.child {
vertical-align: middle;
}
未知宽高
绝对居中+margin:auto
.parent {
position: relative;
}
.child {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
绝对定位+transform ie9+
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
flex ie10+
.parent {
display: flex;
justify-content: center;
align-items: center;
}
grid ie10+
.parent {
display: grid;
justify-content: center;
align-items: center;
}
已知宽高
负margin
要考虑兼容性
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
margin: -height/2 -width/2;
}
padding
.parent {
padding: (parent.height-child.height)/2 (parent.width-child.width)/2;
}
absolute + calc ie9+
.parent {
position: relative;
}
.child {
position: absolute;
top: calc(50% - height/2 );
left: calc(50% - height/2 );
}
浮动元素垂直居中
父元素table-cell
#demo {
width: 300px;
height: 200px;
background-color: grey;
display: table-cell;
vertical-align: middle;
}
.fl {
float: left;
width: 50px;
height: 50px;
background-color: black;
}
<div id="demo">
<div class="fl"></div>
</div>