亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解HTML5中垂直上下居中的解決方案

  發(fā)布時(shí)間:2017-12-20 16:08:47   作者:xxyyxx   我要評(píng)論
本篇文章主要介紹了詳解HTML5中垂直上下居中的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在CSS中,則要吐槽一下,利用margin:0 auto;可以達(dá)到水平方向的居中,但是margin: auto 0則無(wú)法達(dá)到垂直方向的居中。

這里主要還是由于沒(méi)有對(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ù)雜的頁(yè)面布局時(shí)還是很不錯(cuò)的,可以適配任何界面以及幾乎所有的瀏覽器。但對(duì)于十分復(fù)雜的頁(yè)面可能會(huì)需要其他的解決方案,但是從這個(gè)思路出發(fā)也可以得到啟示

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論