我們在日常開發(fā)中經(jīng)常遇到布局問題隙姿,下面羅列幾種常用的css布局方案
話不多說,上代碼画恰!
以下所有demo的源碼
鏈接: http://pan.baidu.com/s/1cHBH3g
密碼:obkb
居中布局
以下居中布局均以不定寬為前提,定寬情況包含其中
1、水平居中
a) inline-block + text-align
.parent{
text-align: center;
}
.child{
display: inline-block;
}
tips:此方案兼容性較好雹锣,可兼容至IE8,對于IE567并不支持inline-block癞蚕,需要使用css hack進行兼容
b) table + margin
.child{
display: table;
margin: 0 auto;
}
tips:此方案兼容至IE8蕊爵,可以使用<table/>
代替css寫法,兼容性良好
c) absolute + transform
.parent{
position: relative;
height:1.5em;
}
.child{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
tips:此方案兼容至IE9桦山,因為transform兼容性限制攒射,如果.child
為定寬元素,可以使用以下寫法恒水,兼容性極佳
.parent{
position: relative;
height:1.5em;
}
.child{
position: absolute;
width:100px;
left: 50%;
margin-left:-50px;
}
d) flex + justify-content
.parent{
display: flex;
justify-content: center;
}
.child{
margin: 0 auto;
}
tips:flex是一個強大的css会放,生而為布局,它可以輕松的滿足各種居中钉凌、對其咧最、平分的布局要求,但由于現(xiàn)瀏覽器兼容性問題御雕,此方案很少被使用矢沿,但是值得期待瀏覽器兼容性良好但那一天!
2酸纲、垂直
a) table-cell + vertial-align
.parent{
display: table-cell;
vertical-align: middle;
}
tips:可替換成<table />
布局捣鲸,兼容性良好
b) absolute + transform
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
transform: translateY(-50%);
}
tips:存在css3兼容問題,定寬兼容性良好
c) flex + align-items
.parent{
display: flex;
align-items: center;
}
tips:高版本瀏覽器兼容闽坡,低版本不適用
3栽惶、水平垂直
a) inline-block + table-cell + text-align + vertical-align
.parent{
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
display: inline-block;
}
tips:兼容至IE8
b) absolute + transform
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
tips:兼容性稍差,兼容IE10以上
c) flex
.parent{
display: flex;
justify-content: center;
align-items: center;
}
tips:兼容差
多列布局
1疾嗅、一列定寬外厂,一列自適應
a) float + margin
.left{
float: left;
width: 100px;
}
.right{
margin-left: 120px;
}
tips:此方案對于定寬布局比較好,不定寬布局推薦方法b
b) float + overflow
.left{
float: left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
tips:個人常用寫法宪迟,此方案不管是多列定寬或是不定寬酣衷,都可以完美實現(xiàn),同時可以實現(xiàn)登高布局
c) table
.parent{
display: table; width: 100%;
table-layout: fixed;
}
.left,.right{
display: table-cell;
}
.left{
width: 100px;
padding-right: 20px;
}
d) flex
.parent{
display: flex;
}
.left{
width: 100px;
padding-right: 20px;
}
.right{
flex: 1;
}
2次泽、多列定寬穿仪,一列自適應
a) float + overflow
.left,.center{
float: left;
width: 100px;
margin-right: 20px;
}
.right{
overflow: hidden;
}
b) table
.parent{
display: table; width: 100%;
table-layout: fixed;
}
.left,.center,.right{
display: table-cell;
}
.right{
width: 100px;
padding-right: 20px;
}
c) flex
.parent{
display: flex;
}
.left,.center{
width: 100px;
padding-right: 20px;
}
.right{
flex: 1;
}
3、一列不定寬意荤,一列自適應
a) float + overflow
.left{
float: left;
margin-right: 20px;
}
.right{
overflow: hidden;
}
.left p{width: 200px;}
b) table
.parent{
display: table; width: 100%;
}
.left,.right{
display: table-cell;
}
.left{
width: 0.1%;
padding-right: 20px;
}
.left p{width:200px;}
c) flex
.parent{
display: flex;
}
.left{
margin-right: 20px;
}
.right{
flex: 1;
}
.left p{width: 200px;}
4啊片、多列不定寬,一列自適應
a) float + overflow
.left,.center{
float: left;
margin-right: 20px;
}
.right{
overflow: hidden;
}
.left p,.center p{
width: 100px;
}
5玖像、等分
a) float + margin
.parent{
margin-left: -20px;
}
.column{
float: left;
width: 25%;
padding-left: 20px;
box-sizing: border-box;
}
b) table + margin
.parent-fix{
margin-left: -20px;
}
.parent{
display: table;
width:100%;
table-layout: fixed;
}
.column{
display: table-cell;
padding-left: 20px;
}
c) flex
.parent{
display: flex;
}
.column{
flex: 1;
}
.column+.column{
margin-left:20px;
}
6紫谷、等高
a) float + overflow
.parent{
overflow: hidden;
}
.left,.right{
padding-bottom: 9999px;
margin-bottom: -9999px;
}
.left{
float: left; width: 100px;
}
.right{
overflow: hidden;
}
b) table
.parent{
display: table;
width: 100%;
}
.left{
display:table-cell;
width: 100px;
margin-right: 20px;
}
.right{
display:table-cell;
}
c) flex
.parent{
display:flex;
width: 100%;
}
.left{
width: 100px;
}
.right{
flex:1;
}
如果你覺得此文對你有一定的幫助,可以點擊下方的【喜歡】收藏備用