最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

CSS布局之如何實(shí)現(xiàn)居中布局

  發(fā)布時(shí)間:2019-06-24 15:21:15   作者:趙龍   我要評(píng)論
這篇文章主要介紹了CSS布局之如何實(shí)現(xiàn)居中布局,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1. 父級(jí)容器設(shè)置成表格,子級(jí)設(shè)為行內(nèi)元素。

適合子級(jí)內(nèi)容為文本展示。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: table-cell;    /* 轉(zhuǎn)變成表格 */
        text-align: center;     /* 水平 */
        vertical-align: middle; /* 垂直 */
    }
    #child {
        background-color: blue;
        color: white;

        display: inline;        /* 子元素設(shè)置為行內(nèi)或行內(nèi)塊 */
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child">內(nèi)容</div>
</div>

2. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位后通過外邊距居中。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative;     /* 設(shè)置相對(duì)定位 */
    }
    #child {
        height: 50px;
        width: 50px;
        color: white;
        background-color: blue;

        /* 絕對(duì)定位,4 個(gè)方向設(shè)置為 0 后,margin 設(shè)為 auto */
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        margin: auto;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

3. 父級(jí)容器設(shè)置為彈性盒,子級(jí)設(shè)置外邊距。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: flex;          /* 父元素轉(zhuǎn)換為彈性盒 */
        display: -webkit-flex;  /* Safari */
    }
    #child {
        height: 50px;
        width: 50px;
        background-color: blue;

        margin: auto;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

4. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,左邊距和上邊距設(shè)置負(fù)一半寬度。

適合子級(jí)的寬高固定的情況。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative;     /* 設(shè)置相對(duì)定位 */
    }
    #child {                      /* 子元素已知自身寬高的情況下 */
        background-color: blue;

        width: 50px;
        height: 50px;
        margin-top: -25px;
        margin-left: -25px;
        position: absolute;
        left: 50%;
        top: 50%;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

5. 父級(jí)容器設(shè)置相對(duì)定位,子級(jí)設(shè)置絕對(duì)定位,通過變形屬性設(shè)置水平和垂直方向負(fù)一半。

適合子級(jí)的寬高不固定的情況。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        position: relative;     /* 設(shè)置相對(duì)定位 */
    }
    #child {                      /* 子元素未知自己的寬高,使用 transform 的 translate */
        border: 1px solid blue;

        position: absolute;
        top: 50%;
        left: 50%;
        -webkit-transform: translate(-50%,-50%);
        -ms-transform: translate(-50%,-50%);
        -o-transform: translate(-50%,-50%);
        transform: translate(-50%,-50%);
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child">
        <div id="content">
            內(nèi)容1
            <br/>
            內(nèi)容2
        </div>
    </div>
</div>

6. 父級(jí)設(shè)置為彈性盒,設(shè)置對(duì)齊屬性。

<!-- css -->
<style>
    #parent {
        height: 200px;
        width: 200px;
        border: 1px solid red;

        display: flex;          /* 父元素轉(zhuǎn)換為彈性盒 */
        display: -webkit-flex;  /* Safari */
        justify-content: center;/* 水平 */
        align-items: center;    /* 垂直 */
    }
    #child {
        height: 50px;
        width: 50px;
        background-color: blue;
    }
</style>

<!-- html -->
<div id="parent">
    <div id="child"></div>
</div>

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

相關(guān)文章

最新評(píng)論

宁乡县| 定州市| 互助| 鹤壁市| 桐柏县| 舒兰市| 文化| 浑源县| 四川省| 兴国县| 富阳市| 安图县| 修武县| 曲周县| 肇庆市| 荣昌县| 利辛县| 榕江县| 仲巴县| 正镶白旗| 寿阳县| 土默特左旗| 乐平市| 双峰县| 天门市| 蕉岭县| 手机| 许昌县| 双柏县| 厦门市| 兰西县| 雷波县| 白河县| 嘉善县| 蒲城县| 嘉定区| 双鸭山市| 拜城县| 绥滨县| 鹤壁市| 嘉禾县|