css居中幾種方式

前端經(jīng)常遇到對div進(jìn)行水平垂直居中問題,網(wǎng)上也有很多解決方式,但是我們需要根據(jù)不同的前提條件和兼容性等來選擇合適的方案

方案1、position+margin:auto (未知元素寬高)

      <style>
            .wrap {
                position:relative;
                height:500px;
            }
            
            .inner {
                width: 100px;
                height: 100px;
                background: pink;
            }
            
            .center {
                position: absolute;
                left: 0;
                right: 0;
                top: 0;
                bottom: 0;
                margin: auto;
            }
        </style>

        <div class="wrap">
            <div class="inner center">love</div>
        </div>
          

方案2、table-cell (未知元素寬高)

         <style>
            .content {
                display: table-cell;
                vertical-align: middle;
                text-align: center;
                width: 300px;
                height: 300px;
                background-color: cadetblue;
            }
            
            .inner {
                display: inline-block;
                width: 50px;
                height: 50px;
                line-height: 50px;
                background-color: pink;
            }
        </style>
        <div class="content">
            <div class="inner"> 111 </div>
        </div>

方案3、position+transform(css3存在兼容性問題) (未知元素寬高)

       <style>
            .wrap {
                position:relative;
                width:300px;
                height:300px;
                border:1px solid red;
            }
            
            .inner {
                width: 100px;
                height: 100px;
                background: pink;
            }
            
            .center {
                position: absolute;
                left:50%;
                top:50%;
                transform: translate(-50%,-50%);
            }
        </style>

        <div class="wrap">
            <div class="inner center"></div>
        </div>

或position+calc()(css3存在兼容性問題) (已知元素寬高)

         .center {
                position: absolute;
                top: calc(50% - 50px);
                left: calc(50% - 50px);
         }

或position+margin (已知元素寬高)

         .center {
                position: absolute;
                left:50%;
                top:50%;
                margin-left:-50px;
                margin-top:-50px;
         }

方案4、flex布局(低版本瀏覽器不兼容) (未知元素寬高)

         <style>
            .wrap {
                display: flex;
                justify-content: center;
                align-items: center;
                height:500px;
            }
            
            .inner {
                width: 100px;
                height: 100px;
                background: pink;
            }
            
        </style>

        <div class="wrap">
            <div class="inner">love</div>
        </div>

參考:CSS水平垂直居中常見方法總結(jié)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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