CSS居中的幾種方式

1.水平居中的 margin: 0 auto;

這個(gè)用于子元素上的,前提是不受float影響。

2.水平居中的 text-align: center;

img的display:inline-block;類似一樣在不受float影響下進(jìn)行
是在父元素上添加效果讓它進(jìn)行水平居中

3.水平垂直居中(一)定位和需要定位的元素的margin減去寬高的一半

這種方法的局限性在于需要知道需要垂直居中的寬高才能實(shí)現(xiàn),經(jīng)常使用這種方法

    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;
        }
        img{
            width: 100px;
            height: 150px;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -75px;
            margin-left: -50px;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

4.水平垂直居中(二)定位和margin:auto;

這個(gè)方法也很實(shí)用,不用受到寬高的限制,也很好用

<style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            position: relative;

        }
        img{
            width: 100px;
            height: 100px;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

5.水平垂直居中(三)絕對(duì)定位和transfrom

這個(gè)是不需要知道居中元素的寬高就可以使用的.面試中大部分人會(huì)問如果不知道寬高該如何居中,答這個(gè)

<style>
    *{
        padding: 0;
        margin: 0;
    }
    .box{
        width: 300px;
        height: 300px;
        background:#e9dfc7; 
        border:1px solid red;
        position: relative;

    }
    img{
        width: 100px;
        height: 100px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
    }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

6.水平垂直居中(四)diplay:table-cell

其實(shí)這個(gè)就是把其變成表格樣式,再利用表格的樣式來進(jìn)行居中,很方便

<style>
    .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            display: table-cell;
            vertical-align: middle;
            text-align: center;
        }
        img{
            width: 100px;
            height: 150px;
            /*margin: 0 auto;*/  這個(gè)也行
        }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

7.水平垂直居中(五)flexBox居中

這個(gè)用了C3新特性flex,非常方便快捷,在移動(dòng)端使用完美,pc端有兼容性問題,以后會(huì)成為主流的

<style>
    .box{
            width: 300px;
            height: 300px;
            background:#e9dfc7; 
            border:1px solid red;
            display: flex;
            justify-content: center;
            align-items:center;
        }
        img{
            width: 150px;
            height: 100px;
        }
</style>
<!--html -->
<body>
    <div class="box" >
        ![](../img1.png)
    </div>
</body>

8.水平垂直居中(六)利用vertical-align:middle;

這個(gè)方法關(guān)鍵要有一個(gè)和容器一樣高的元素作為居中的一個(gè)參照就像b元素一樣

<style>
    .wrap{
            width:300px;
            height:300px; 
            background:rgba(0,0,0,0.5);
            text-align:center;
            font-size:0;
            }
    .vamb{
        display:inline-block; 
        width:0px;
        height:100%;
        vertical-align:middle;
        }
    .test{
        display:inline-block;
        vertical-align:middle;
        font-size:16px;
        text-align:left;
        background:red;
        }
</style>
<body>
    <div class="wrap">
        <b class="vamb"></b>
        <div class="test">
        寬高不定<br>
        垂直水平居中
        </div>
    </div>
</body>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,154評(píng)論 1 92
  • 水平居中方式 1.margig:0 auto;(不能受到float影響) 2.text-align: center...
    MGd閱讀 596評(píng)論 0 1
  • 本文主要總結(jié)幾種常見的CSS居中方式,下面我準(zhǔn)備分為三個(gè)方向來寫,分別是水平居中,垂直居中,水平垂直居中。水平居中...
    IrisLong閱讀 752評(píng)論 0 2
  • 1.水平居中的 margin:0 auto; 2.水平居中 text-align:center; 3.水平垂直居中...
    被遺忘的傳說閱讀 1,254評(píng)論 0 0
  • 上課了。 所有人抽出了物理必修二除了我,我抽出來的東西會(huì)更大一些,更軟一些,放在桌子上大小正好,但略顯空曠。感覺若...
    貝龍閱讀 940評(píng)論 3 5

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