CSS 布局整理,css布局整理
1.css垂直水平置中
效果:
HTML代碼:
<div id="container"> <div id="center-div"></div></div>
CSS實現:
都設定置中 div塊的寬高和背景色。
#center-div { width: 100px; height: 100px; background: #2b669a;}
a.用inline-block和vertical-align來實現置中:
#container { text-align: center; height: 200px; background: #cccccc;}#container:before { content: ""; height: 100%; display: inline-block; vertical-align: middle;margin-right: -0.25em;}#center-div { display: inline-block; vertical-align: middle;}
b.用相對絕對位置和負邊距實現上下左右置中
/* 首先相對父元素top,left定位在50%的位置,這時候只是圖片左上方置中,而中心點還沒置中,* 加上margin: -50px 0 0 -50px;利用負邊距把圖片的中心點位於父元素的中心點,從而實現垂直水平置中 */#container {height: 200px;position: relative; background: #ccc;}#center-div { display: inline-block; position: relative;top: 50%;left: 50%;margin: -50px 0 0 -50px;}
c.利用絕對位置來實現置中
#container { text-align: center; height: 200px; background: #cccccc; position: relative;}#center-div { position: absolute; margin: auto;top: 0;right: 0;left: 0;bottom: 0; }
d.使用table-cell,inline-block實現水平垂直置中
#container { display: table-cell; text-align: center; vertical-align: middle; width: 600px; height: 200px; background: #ccc;}#center-div { display: inline-block; }
因為 display:table-cell ,所以可以通過JS設定 #container 的固定寬度:
window.onload = function () {var pageWidth = document.documentElement.clientWidth || document.body.clientWidth;document.getElementById('container').style.width = pageWidth+"px";}
e.使用css3中的transform來時實現水平垂直置中
#container { position: relative; height: 200px; background: #cccccc;}#center-div {position: absolute; top: 50%; left:50%;transform: translate(-50%,-50%);}
f.使用Flex來實現水平垂直置中
#container { display: flex;justify-content: center; align-items: center; height: 200px;background: #cccccc;}
相關連結:用css實現垂直水平置中的幾種方法 || Centering in the Unknown
2.兩行布局:頭部高度固定,尾部高度為剩餘高度
效果:
代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>兩行布局:頭部高度固定,尾部高度為剩餘高度</title> <style> * { padding: 0;margin: 0;} header { width: 100%; height: 60px; background: #cccccc;} footer { width: 100%; background: #2b669a;position: absolute;top: 60px;bottom: 0;} </style></head><body><header></header><footer></footer></body></html>
3.雙飛翼布局:左右寬度固定,中部自適應
效果:
HTML代碼:
<div class="container"> <div class="left"></div> <div class="right"></div> <div class="middle">This is middle</div></div>
簡單的CSS Reset:
html,body { padding: 0;margin: 0;width: 100%;height: 100%;}.container { height: 100%; width: 100%;}
a.利用中部元素左右margin+左右元素的絕對位置:
.left { width: 120px; height: 100%;background: #1a929f; position: absolute; left: 0;top:0;}.right { width: 60px; height: 100%; background: #9f1c24;position: absolute;right: 0;top:0;}.middle { background: #cccccc; height: 100%;margin: 0 60px 0 120px;}
b.利用中部元素左右margin+左右元素的浮動(注意 html中類名為"middle"的div在最後面):
.left { width: 120px; height: 100%;background: #1a929f;float: left;}.right { width: 60px; height: 100%; background: #9f1c24;float: right;}.middle { background: #cccccc; height: 100%;margin: 0 60px 0 120px;}
4.頭部,尾部高度固定,中間調適型配置
效果:
代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>頭部,尾部高度固定,中間調適型配置</title> <style> * { padding: 0;margin: 0;} header { width: 100%; height: 60px; background: #cccccc;} main { width: 100%; background: #2b669a;position: absolute;top: 60px;bottom: 60px;} footer {width: 100%;height:60px;background: #9f1c24; position: absolute;bottom: 0;} </style></head><body><header></header><main>This is main!</main><footer></footer></body></html>
5. 見:
效果:
代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>header-fixed-else-3-cols</title> <style> html,body { padding: 0;margin: 0;height: 100%;width: 100%;} header { width: 100%; height: 100px; background: #cccccc;} /*main { height: 100%;margin-top: -60px; width: 100%;}*/ .left { width: 200px; position: absolute; top: 100px; bottom: 0;background: #1a929f;} .middle { position: absolute; left: 200px;right: 200px;top: 100px;bottom: 0;} .right { width: 200px;position: absolute; top: 100px; right: 0;bottom: 0; background: #2b669a;} </style></head><body><header></header><main> <section class="left"></section> <section class="middle">This is middle!</section> <section class="right"></section></main></body></html>
相關連結:常見的CSS布局總結
6.實現垂直置中水平置中的絕對位置置中技術
我們經常用 margin:0 auto來實現水平置中,而一直認為margin:auto不能實現垂直置中……實際上,實現垂直置中僅需要聲明元素高度和下面的CSS:
.Absolute-Center { margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }
優點:
1.支援跨瀏覽器,包括IE8-IE10.2.無需其他特殊標記,CSS代碼量少3.支持度百分比%屬性值和min-/max-屬性4.只用這一個類可實現任何內容塊置中5.不論是否設定padding都可置中(在不使用box-sizing屬性的前提下)6.內容塊可以被重繪。7.完美支援圖片置中。
缺點:
1.必須聲明高度(查看可變高度Variable Height)。2.建議設定overflow:auto來防止內容越界溢出。(查看溢出Overflow)。3.在Windows Phone裝置上不起作用。
瀏覽器安全色性:
Chrome,Firefox, Safari, Mobile Safari, IE8-10.
a.容器內(Within Container)
內容塊的父容器設定為position:relative,使用上述絕對置中方式,可以使內容置中顯示於父容器()。
顯示效果:
HTML代碼:
<div class="container"> <div class="center"></div></div>
CSS代碼:
.container { background: #2E5F3F;position: relative; height: 300px;}.center { margin: auto;position: absolute;top: 0; left: 0; bottom: 0; right: 0;}.center { background: #4FA46B; ;width: 50%;height: 120px; }
源碼:absolute-centering-in-css-01.html
b.視區內(Within Viewport)
想讓內容塊一直停留在可視地區內?將內容塊設定為position:fixed;並設定一個較大的z-index層疊屬性值。
.center.is-Fixed {position: fixed;z-index: 9999;}
顯示效果:
源碼:absolute-centering-in-css-02.html
c.側邊欄 (Offsets)
如果你要設定一個固頂的頭或增加其他的側邊欄,只需要在內容塊的樣式中加入像這樣的CSS樣式代碼:top:70px;bottom:auto;由於已經聲明了margin:auto;,該內容塊將會垂直置中於你通過top,left,bottom和right屬性定義的邊界框內。
你可以將內容塊固定與螢幕的左側或右側,並且保持內容塊垂直置中。使用right:0;left:auto;固定於螢幕右側,使用left:0;right:auto;固定與螢幕左側。
.center.is-Left { right:auto; left: 0;text-align: left; width: 40%;}.center.is-Right { left:auto; right: 0;text-align: right; width: 40%;}
顯示效果:
源碼:absolute-centering-in-css-03.html
d.響應式(Responsive)
絕對置中最大的優勢應該就是對百分比形式的寬高支援的非常完美。甚至min-width/max-width 和min-height/max-height這些屬性在自適應盒子內的表現也和預期很一致。
.center.is-Responsive { width: 60%;height: 60%;min-width: 200px;max-width: 400px;padding: 40px;}
源碼:absolute-centering-in-css-04.html
e.溢出情況(Overflow)
內容高度大於塊元素或容器(視區viewport或設為position:relative的父容器)會溢出,這時內容可能會顯示到塊與容器的外面,或者被截斷出現顯示不全(分別對應內容塊overflow屬性設定為visible和hidden的表現)。
加上overflow: auto會在內容高度超過容器高度的情況下給內容塊顯示捲軸而不越界。
.center.is-Overflow { overflow: auto; }
源碼:absolute-centering-in-css-05.html
f.重繪(Resizing)
你可以使用其他class類或JavaScript代碼來重繪內容塊同時保證置中,無須手動重新計算中心尺寸。當然,你也可以添加resize屬性來讓使用者拖拽實現內容塊的重繪。
絕對置中(Absolute Centering)可以保證內容塊始終置中,無論內容塊是否重繪。可以通過設定min-/max-來根據自己需要限制內容塊的大小,並防止內容溢出視窗/容器。
.center.is-Resizable {min-width: 20%;max-width: 80%;min-height: 20%;max-height: 80%;resize: both;overflow: auto;}
源碼:absolute-centering-in-css-06.html
g.圖片(Images)
絕對置中(AbsoluteCentering)也適用於圖片。
源碼:absolute-centering-in-css-07.html
h.可變高度(Variable Height)
這種情況下實現絕對置中(AbsoluteCentering)必須要聲明一個高度,不管你是基於百分比的高度還是通過max-height控制的高度,還有,別忘了設定合適的overflow屬性。對自適應/響應式情景,這種方法很不錯。
與聲明高度效果相同的另一種方法是設定display:table;這樣無論實際內容有多高,內容塊都會保持置中。
.center.is-Variable { display: table; height: auto;}
源碼:absolute-centering-in-css-08.html
相關連結:盤點8種CSS實現垂直置中水平置中的絕對位置置中技術 || Absolute Horizontal And Vertical Centering In CSS
(待續。。。)