css常用布局
居中布局
以下居中布局均以不定寬為前提,定寬情況包含其中
1、水平居中
a) inline-block + text-align
tips:此方案兼容性較好,可兼容至IE8,對于IE567并不支持inline-block,需要使用css hack進行兼容
b) table + margin
tips:此方案兼容至IE8,可以使用<table/>代替css寫法,兼容性良好
c) absolute + transform
tips:此方案兼容至IE9,因為transform兼容性限制,如果.child為定寬元素,可以使用以下寫法,兼容性極佳
d) flex + justify-content
tips:flex是一個強大的css,生而為布局,它可以輕松的滿足各種居中、對其、平分的布局要求,但由于現瀏覽器兼容性問題,此方案很少被使用,但是值得期待瀏覽器兼容性良好但那一天!
2、垂直
a) table-cell + vertial-align
.parent{?display: table-cell;?vertical-align: middle;}tips:可替換成<table />布局,兼容性良好
b) absolute + transform
tips:存在css3兼容問題,定寬兼容性良好
c) flex + align-items
.parent{?display: flex;?align-items: center;}tips:高版本瀏覽器兼容,低版本不適用
3、水平垂直
a) inline-block + table-cell + text-align + vertical-align
tips:兼容至IE8
b) absolute + transform
tips:兼容性稍差,兼容IE10以上
c) flex
.parent{?display: flex;?justify-content: center;?align-items: center;}tips:兼容差
多列布局
1、一列定寬,一列自適應
a) float + margin
tips:此方案對于定寬布局比較好,不定寬布局推薦方法b
b) float + overflow
tips:個人常用寫法,此方案不管是多列定寬或是不定寬,都可以完美實現,同時可以實現登高布局
c) table
d) flex
2、多列定寬,一列自適應
a) float + overflow
b) table
c) flex
3、一列不定寬,一列自適應
a) float + overflow
b) table
c) flex
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;}并排等分,單排對齊靠左布局
效果圖
flex
.main?{?display: flex;?flex-flow: row wrap;?justify-content: space-between;}.item?{?display: inline-block;}.empty{?height:?0;?visibility: hidden;}?
轉載于:https://www.cnblogs.com/lavezhang/p/7509969.html
總結
- 上一篇: SQL SERVER 运维日记
- 下一篇: hdu 5008 Boring Stri