這里主要還是由于沒有對(duì)父控件即控件本身進(jìn)行正確的定位。直接看代碼, 首先對(duì)父控件需要使用相對(duì)布局,之后對(duì)子控件需要使用絕對(duì)布局,并且利用top,和bottom屬性,結(jié)合margin: auto 0;,則可以達(dá)到效果。
.container-vertical { position: relative; width: 100%; height: 200px; background: deepskyblue; margin-bottom: 20px;.container-vertical-item { position: absolute; width: 130px; height: 80px; text-align: center; background: yellow; line-height: 80px; top: 0; bottom: 0; margin: auto 0;}
垂直方向上居中.png
水平垂直方向居中
有了5.2的經(jīng)驗(yàn),我們可以嘗試設(shè)置子控件的left和right,top,bottom屬性都為0,并且margin: auto;四個(gè)方向上都是自動(dòng)外邊距。則可以達(dá)到這樣的效果。其中需要注意的子控件需要必須是display: block; 屬性。
看代碼
.container-horization-vertical { position: relative; width: 100%; height: 200px; background: deepskyblue; margin-bottom: 20px;.container-horization-vertical-item { position: absolute; width: 150px; height: 80px; background: yellow; line-height: 80px; text-align: center; top: 0; left: 0; bottom: 0; right: 0; margin: auto;}
小結(jié): 這種方案在解決一些不算復(fù)雜的頁面布局時(shí)還是很不錯(cuò)的,可以適配任何界面以及幾乎所有的瀏覽器。但對(duì)于十分復(fù)雜的頁面可能會(huì)需要其他的解決方案,但是從這個(gè)思路出發(fā)也可以得到啟示。
相關(guān)推薦:
移動(dòng)端HTML5中判斷橫屏豎屏的方法
深入理解html5中的position
html5中必須知道的十件事
以上就是HTML5中垂直上下居中的解決方案的詳細(xì)內(nèi)容,其它編程語言
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選