居中解決方案薈萃

原文鏈接 http://blog.poetries.top/2016/11/12/CSS-center-methods/

關(guān)注公眾號(hào)獲取更多資訊

在線預(yù)覽 http://sandbox.runjs.cn/show/oljhupi9

水平居中方案


方案一:text-align + inline-block


<div id="parent1">
    <div class="child">水平居中</div>
</div>
#parent1{
    text-align: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent1 .child{
    display: inline-block;
    background:#666;
    color:#fff;
}

方案二:margin:0 auto


<div id="parent2">
    <div class="child">水平居中</div>
</div>
#parent2{
    text-align: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent2 .child{
    display: table;
    margin: 0 auto;
    background:#666;
    color:#fff;
}

方案三:absolute+transform


<div id="parent3">
    <div class="child">水平居中</div>
</div>
#parent3{
    position: relative;
    background:#ddd;
    margin-bottom:20px;
}
#parent3 .child{
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    background:#666;
    color:#fff;
}


方案四:justify-content


<div id="parent4">
    <div class="child">水平居中</div>
</div>
#parent4{
    display: flex;
    justify-content: center;
    background:#ddd;
    margin-bottom:20px;
}
#parent4 .child{
    margin:0 auto;
    background:#666;
    color:#fff;
}

垂直居中方案


方案一: 利用 line-height 實(shí)現(xiàn)垂直居中


  • 這種方法適用于單行文本垂直居中,如果文本內(nèi)容太長,出現(xiàn)了換行,換行后的內(nèi)容會(huì)溢出
<div id="example1">
    單行文字垂直居中
</div>
#example1 {
    height: 100px;
    line-height: 100px;
    background: #161616;
    color: #fff;
    width: 200px;
}

方案二 利用 display: table 實(shí)現(xiàn)垂直居中


<div id="example2">
    <div class="inner">塊區(qū)域垂直居中</div>
</div>
#example2 {
    height: 100px;
    background: #161616;
    color: #fff;
    width: 400px;
    overflow: hidden;
    display: table;
            margin-bottom:20px;
}
#example2 .inner{
    display: table-cell;
    vertical-align: middle;
    height: 50px;
    background:#999;
}

方案三 margin 填充 這種方法需要知道內(nèi)外容器的大小


<div id="example3">
    <div class="inner">塊區(qū)域垂直居中</div>
</div>
#example3 {
    height: 100px;
    background: #161616;
    color: #fff;
    width: 400px;
    overflow: hidden;
            margin-bottom:20px;
}
#example3 .inner{
    margin-left: auto;
    margin-right: auto;
    margin-top: calc((100px - 50px)/2);
    height: 50px;
    background:#999;
}

方案四:經(jīng)典 absolute 布局上下文垂直居中


<div id="example4">
    <div class="inner">塊區(qū)域垂直居中</div>
</div>
#example4 {
    width: 400px;
    height: 100px;
    background: #161616;
    color: #fff;
    position: relative;
        margin-bottom:20px;
}
#example4 .inner{
    height: 50px;
    width: 200px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -25px;
    margin-left: -100px;
    background:#999;
}

方案五:absolute+transform


<div id="example5">
    <div class="inner">塊區(qū)域垂直居中</div>
</div>
#example5 {
    width: 400px;
    height: 100px;
    background: #161616;
    color: #fff;
    position: relative;
    margin-bottom:20px;
}
#example5 .inner{
    position: absolute;
    left: 50%;
    top: 50%;
    background: #999;
    transform: translateX(-50%) translateY(-50%);
}

方案六 利用margin:auto 居中


<div id="expample6">
    <div class="inner">Content here</div>
</div>
#expample6 {
    width: 400px;
    height: 100px;
    background: #eee;
    position: relative;
        margin-bottom:20px;
}

#expample6 .inner {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
    height: 50px;
    width: 70%;
    background: #aaa;
    color:#222;
}

方案七 利用 Flex布局 居中


<div id="expample7">
    <div class="inner">Content here</div>
</div>
#expample7 {
    width: 400px;
    height: 100px;
    background: #eee;
    display: flex;
    justify-content: center;
    align-items: center;
}

#expample7 .inner {
    height: 50px;
    width: 70%;
    background: #aaa;
    color:#222;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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