国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

CSS布局之如何實現居中布局

2024-07-11 09:00:04
字體:
來源:轉載
供稿:網友

1. 父級容器設置成表格,子級設為行內元素。

適合子級內容為文本展示。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        display: table-cell;    /* 轉變成表格 */        text-align: center;     /* 水平 */        vertical-align: middle; /* 垂直 */    }    #child {        background-color: blue;        color: white;        display: inline;        /* 子元素設置為行內或行內塊 */    }</style><!-- html --><div id="parent">    <div id="child">內容</div></div>

2. 父級容器設置相對定位,子級設置絕對定位后通過外邊距居中。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        position: relative;     /* 設置相對定位 */    }    #child {        height: 50px;        width: 50px;        color: white;        background-color: blue;        /* 絕對定位,4 個方向設置為 0 后,margin 設為 auto */        position: absolute;        left: 0;        top: 0;        right: 0;        bottom: 0;        margin: auto;    }</style><!-- html --><div id="parent">    <div id="child"></div></div>

3. 父級容器設置為彈性盒,子級設置外邊距。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        display: flex;          /* 父元素轉換為彈性盒 */        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. 父級容器設置相對定位,子級設置絕對定位,左邊距和上邊距設置負一半寬度。

適合子級的寬高固定的情況。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        position: relative;     /* 設置相對定位 */    }    #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. 父級容器設置相對定位,子級設置絕對定位,通過變形屬性設置水平和垂直方向負一半。

適合子級的寬高不固定的情況。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        position: relative;     /* 設置相對定位 */    }    #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">            內容1            <br/>            內容2        </div>    </div></div>

6. 父級設置為彈性盒,設置對齊屬性。

<!-- css --><style>    #parent {        height: 200px;        width: 200px;        border: 1px solid red;        display: flex;          /* 父元素轉換為彈性盒 */        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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聊城市| 平湖市| 丹阳市| 宾川县| 台中市| 丹凤县| 鄂托克旗| 瑞安市| 汕头市| 大庆市| 隆化县| 镇巴县| 沅陵县| 封开县| 大洼县| 绵竹市| 象山县| 永年县| 卢龙县| 二连浩特市| 扎囊县| 北宁市| 武清区| 锡林浩特市| 叙永县| 旌德县| 女性| 宝清县| 黄浦区| 龙江县| 泰顺县| 登封市| 偏关县| 那曲县| 青阳县| 岐山县| 巫山县| 金沙县| 淮安市| 洞头县| 满洲里市|