「前端那些事兒」③ CSS 佈局方案

咱們在平常開發中常常遇到佈局問題,下面羅列幾種經常使用的css佈局方案 話很少說,上代碼!css

居中佈局

如下居中佈局均以不定寬爲前提,定寬狀況包含其中html

一、水平居中

效果圖

a) inline-block + text-align

.parent{
    text-align: center;
}
.child{
    display: inline-block;
}
複製代碼

tips:此方案兼容性較好,可兼容至IE8,對於IE567並不支持inline-block,須要使用css hack進行兼容css3

b) table + margin

.child{
    display: table;
    margin: 0 auto;
}
複製代碼

tips:此方案兼容至IE8,可使用<table/>代替css寫法,兼容性良好git

c) absolute + transform

.parent{
    position: relative;
    height:1.5em;
}
.child{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
複製代碼

tips:此方案兼容至IE9,由於transform兼容性限制,若是.child爲定寬元素,可使用如下寫法,兼容性極佳github

.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,生而爲佈局,它能夠輕鬆的知足各類居中、對其、平分的佈局要求,但因爲現瀏覽器兼容性問題,此方案不多被使用,可是值得期待瀏覽器兼容性良好但那一天!瀏覽器

二、垂直

效果圖

a) table-cell + vertial-align

.parent{
	display: table-cell;
	vertical-align: middle;
}
複製代碼

tips:可替換成<table />佈局,兼容性良好bash

b) absolute + transform

.parent{
	position: relative;
}
.child{
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
}
複製代碼

tips:存在css3兼容問題,定寬兼容性良好app

c) flex + align-items

.parent{
	display: flex;
	align-items: center;
}
複製代碼

tips:高版本瀏覽器兼容,低版本不適用佈局

三、水平垂直

效果圖

a) inline-block + table-cell + text-align + vertical-alignflex

.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:兼容差

多列布局

一、一列定寬,一列自適應

效果圖

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:我的經常使用寫法,此方案無論是多列定寬或是不定寬,均可以完美實現,同時能夠實現登高佈局 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;
}
複製代碼

二、多列定寬,一列自適應

效果圖

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;
}
複製代碼

三、一列不定寬,一列自適應

效果圖

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;}
複製代碼

四、多列不定寬,一列自適應

效果圖

a) float + overflow

.left,.center{
	float: left;
	margin-right: 20px;
}
.right{
	overflow: hidden;
}
.left p,.center p{
	width: 100px;
}
複製代碼

五、等分

效果圖
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;
}
複製代碼

六、等高

效果圖
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;
}
複製代碼

具體詳解請見下文 github.com/zwwill/blog…

聖盃佈局&雙飛翼佈局

此處僅爲代碼展現,差異講解請移駕下文 【方案】聖盃佈局&雙飛翼佈局

聖盃佈局

【demo】codepen.io/zwwill/pen/…

<div class="container">
    <div class="header">header</div>
    <div class="wrapper clearfix">
        <div class="main col">main</div>
        <div class="left col">left</div>
        <div class="right col">right</div>
    </div>
    <div class="footer">footer</div>
</div>
複製代碼
.container {width: 500px; margin: 50px auto;}
.wrapper {padding: 0 100px 0 100px;}
.col {position: relative; float: left;}
.header,.footer {height: 50px;}
.main {width: 100%;height: 200px;}
.left {width: 100px; height: 200px; margin-left: -100%;left: -100px;}
.right {width: 100px; height: 200px; margin-left: -100px; right: -100px;}
.clearfix::after {content: ""; display: block; clear: both; visibility: hidden; height: 0; overflow: hidden;}
複製代碼

雙飛翼佈局

ps: 「這不是同樣的圖嘛?」 「對!就是同樣的,由於是解決同一種問題的嘛。」

【demo】codepen.io/zwwill/pen/…

<div class="container">
    <div class="header">header</div>
    <div class="wrapper clearfix">
        <div class="main col">
            <div class="main-wrap">main</div>
        </div>
        <div class="left col">left</div>
        <div class="right col">right</div>
    </div>
    <div class="footer">footer</div>
</div>
複製代碼
.col {float: left;}
.header {height: 50px;}
.main {width: 100%;}
.main-wrap {margin: 0 100px 0 100px;height: 200px;}
.left {width: 100px; height: 200px; margin-left: -100%;}
.right {width: 100px; height: 200px; margin-left: -100px;}
.footer {height: 50px;}
.clearfix::after {content: ""; display: block; clear: both; visibility: hidden; height: 0; overflow: hidden;}
複製代碼

定位佈局

簡單的絕對定位便可解決問題,爲啥還要搞什麼聖盃和雙飛翼?緣由

<div class="header">header</div>
<div class="wrapper">
    <div class="main col">
        main
    </div>
    <div class="left col">
        left
    </div>
    <div class="right col">
        right
    </div>
</div>
<div class="footer">footer</div>
複製代碼
.wrapper { position: relative; }
.main { margin:0 100px;}
.left { position: absolute; left: 0; top: 0;}
.right { position: absolute; right: 0; top: 0;}
複製代碼

以上全部demo的源碼 github:github.com/zwwill/css-…

轉載請標註出處

首發:github.com/zwwill/blog… 做者:木羽

相關文章
相關標籤/搜索