CSS 元素水平,垂直居中

HTML

    <div class="father">
        <div class="son"></div>
    </div>

1、利用 absolutetranslate

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100px;
            height: 100px;
            background: blue;
        }

2、利用 absolute 和 負(fù)的margin

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px;
            width: 100px;
            height: 100px;
            background: blue;
        }

3、利用 absolutetop right bottom leftmargin: auto

CSS

        .father{
            position: relative;
            margin: 0 auto;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            margin: auto;
            width: 100px;
            height: 100px;
            background: blue;
        }

4、利用 display: tabledisplay: table-cell

(1)display: tablepadding 會失效
(2)display: table-rowmargin、padding 同時失效
(3)display: table-cellmargin 會失效
(4) 表格中的單元格中的 div 設(shè)置寬度無效,是因為 div 被設(shè)置為 display: table-cell ,后將其修改為 display: block 則設(shè)置的寬度生效

CSS

        .father{
            display: table-cell;
            text-align: center;
            vertical-align: middle;
            width: 200px;
            height: 200px;
            background: red;
        }

        .son{
            display: inline-block;
            width: 100px;
            height: 100px;
            background: blue;
        }

效果

table-cell 居中

5、通過 font-size 實現(xiàn)居中

IE7 以下有效

font-size 值為 height / 1.14

HTML

    <div class="father">
        <span class="son"></span>
    </div>

CSS

        .father{
            width: 200px;
            height: 200px;
            font-size: 175.4px;  /* height / 1.14 */
            text-align: center;
            background: red;
        }

        .son{
            display: inline-block;
            zoom: 1;
            vertical-align: middle; /*inline 和 inline-block 才能設(shè)置該屬性*/
            font-size: 12px; /* 初始化字體大小 */
            width: 100px;
            height: 100px;
            background: blue;
        }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補...
    _Yfling閱讀 14,189評論 1 92
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,868評論 1 45
  • 一 外部式css樣式 (也可稱為外聯(lián)式)就是把css代碼寫一個單獨的外部文件中,這個css樣式文件以“.css...
    KunMitnic閱讀 1,131評論 0 1
  • 本課來自http://www.imooc.com/learn/9請不要用作商業(yè)用途。 HTML5 HTML介紹 H...
    PYLON閱讀 3,457評論 0 5
  • 〔詩/李蘊珂〕 年,春的使者 從遠(yuǎn)古穿越千年 從來未老 三百六十五天再輪回 又是新年 年,總在寒冬過后 應(yīng)春之邀 ...

友情鏈接更多精彩內(nèi)容