在CSS中,則要吐槽一下,利用margin:0 auto;可以達到水平方向的居中,但是margin: auto 0則無法達到垂直方向的居中。
這裡主要還是由於沒有對父控製即控製本身進行正確的定位。直接看程式碼, 首先對父控制項需要使用相對佈局,之後對子控制項需要使用絕對佈局,並且利用top,和bottom屬性,結合margin: auto 0;,則可以達到效果。
.container-vertical { position: relative; width: 100%; height: 200px; background: deepskyblue; margin-bottom: 20px;} .container-vertical-item { position: absolute; width: 1302x; align: center; background: yellow; line-height: 80px; 頂: 0; bottom: 0; margin: auto 0;}
垂直方向上居中.png
水平垂直方向居中有了5.2的經驗,我們可以嘗試設定子控制項的left和right,top,bottom屬性都為0,且margin: auto;四個方向上都是自動外邊距。則可以達到這樣的效果。其中需要注意的子控制項需要必須是display: block; 屬性。
看程式碼
.container-horization-vertical { position: relative; width: 100%; height: 200px; background: deepskyblue; margin-bottom: 20px;} .container-horization-vertical-item { position: abute;} .container-horization-vertical-item { position: abute; 80px; background: yellow; line-height: 80px; text-align: center; top: 0; left: 0; bottom: 0; right: 0; margin: auto;}
小結: 這種方案在解決一些不算複雜的頁面佈局時還是很不錯的,可以適配任何介面以及幾乎所有的瀏覽器。但對於十分複雜的頁面可能需要其他的解決方案,但是從這個思路出發也可以得到啟示
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。