亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    CSS實(shí)現(xiàn)子元素div水平垂直居中的示例

    這篇文章主要介紹了CSS實(shí)現(xiàn)子元素div水平垂直居中的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

    div基本布局

    <div class=”main”>
    <div class=”center”></div>
    </div>

    css樣式

    1. 配合定位與margin:auto

    父元素加相對(duì)定位,子元素加絕對(duì)定位

    .main{
    width: 300px;
    height: 300px;
    background-color: red;
    position: relative;
    }
    .center{
    width: 100px;
    height: 100px;
    background-color: skyblue;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
    }

    2.利用flex布局,設(shè)置水平與豎直方向的內(nèi)容居中。

    .main{
    width: 300px;
    height: 300px;
    background-color: red;
    display: flex;
    justify-content: center;
    align-items: center;
    }
    .center{
    width: 100px;
    height: 100px;
    background-color: greenyellow;
    }

    3.利用position:absolute與transform

    :這里需要記住的是transform中translate使用百分比時(shí)相對(duì)的是自己的長寬,不是父盒子的。

    .main{
    width: 300px;
    height: 300px;
    background-color: red;
    position: relative;
    }
    .center{
    width: 100px;
    height: 100px;
    background-color: pink;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    }

    4.定位 與負(fù)margin配合

    只適合子盒子長寬固定的情況

    .main{
    width: 300px;
    height: 300px;
    background-color: red;
    position: relative;
    }
    .center{
    width: 100px;
    height: 100px;
    background-color: pink;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -50px;
    margin-top: -50px;
    }

    5.display:table-cell

    display:table-cell;與vertical-align:middle 的作用是讓子盒子在數(shù)值方向上居中

    margin:auto;則讓子盒子在水平方向居中,若只想讓盒子在某個(gè)方向居中,去掉另一個(gè)就可以了。

    .main{
    width: 300px;
    height: 300px;
    background-color: red;
    display: table-cell;
    vertical-align: middle;
    }
    .center{
    width: 100px;
    height: 100px;
    background-color: #000;
    margin: auto;
    }

    文章來源:腳本之家,原文鏈接:https://www.jb51.net/css/743597.html

    CSS實(shí)現(xiàn)子元素div水平垂直居中的示例

    申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)