盒子居中的幾種方法

1.第一種

利用margin,div1的寬減去div2的寬就是div2margin-left的數(shù)值:(100-40)/2=30

div1的高減去div2的高就是div2margin-top的數(shù)值:(100-40)/2=30

按 Ctrl+C 復(fù)制代碼

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}
            .div22{
                margin-left: 30px;margin-top: 30px;
            }
        </style>
        <div class="div1">
            <div class="div2 div22">
            </div>
        </div>
    </body>
</html>
第一種

第2種

利用css的 position屬性,把div2相對(duì)于div1的top、left都設(shè)置為50%,然后再用margin-top設(shè)置為div2的高度的負(fù)一半拉回來(lái),用marg-left設(shè)置為寬度的負(fù)一半拉回來(lái),css如下設(shè)置

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}

            .div11{
                position: relative;
            }
            .div22{
                position: absolute;top:50%;left: 50%;margin-top: -20px;margin-left: -20px;
            }
        </style>

        <div class="div1 div11">
            <div class="div2 div22">

            </div>
        </div>

    </body>
</html>
第二種

第三種

還是用css的position屬性,如下的html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}

            .div11{
                position: relative;
            }
            .div22{
                position: absolute;margin:auto; top: 0;left: 0;right: 0;bottom: 0;
            }
        </style>

        <div class="div1 div11">
            <div class="div2 div22">

            </div>
        </div>

    </body>
</html>
第三種

第四種

利用css3的新增屬性table-cell, vertical-align:middle;

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; height: 40px; background-color: green;}

            .div11{
                display: table-cell;vertical-align: middle;
            }
            .div22{
                margin: auto;
            }
        </style>

        <div class="div1 div11">
            <div class="div2 div22">
            </div>
        </div>

    </body>
</html>
第四種

第五種方法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>demo</title>
    </head>
    <body>
        <style type="text/css">
            .div1{  width: 100px; height: 100px; border: 1px solid #000000;} 
            .div2{ width:40px ; background-color: green;}

            .div11{
                display: table-cell;vertical-align: middle;
            }
            .div22{
                margin: auto;
            }
        </style>

        <div class="div1 div11">
            <div class="div2 div22">
               div居中方法
            </div>
        </div>

    </body>
</html>
第五種方法

第六種方法

利用flexbox布局

直接在父元素上使用flexbox的布局

 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style type="text/css">
        .div1 {
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
        }

        .div2 {
            height: 40px;
            width: 40px;
            background-color: green;
        }

        .div11 {
            display: flex;
            /*!*flex-direction: column;*!可寫可不寫*/
            justify-content: center;
            align-items: center;
        }

    </style>
</head>
<body>


<div class="div1 div11">
    <div class="div2 div22">

    </div>
</div>

</body>
</html>
第六種方法

第七種方法

利用transform的屬性,注意子絕父相定位

缺點(diǎn):需要支持Html5
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style type="text/css">
        body {
            margin: 100px auto;
            position: relative;
        }

        .div1 {
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
            background-color: red;
        }

        .div2 {
            height: 40px;
            width: 40px;
            background-color: green;
        }

        .center {
            position: absolute;
            top: 50%;
            left: 50%;
            -ms-transform: translate(-50%, -50%);
            -moz-transform: translate(-50%, -50%);
            -o-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }

    </style>
</head>
<body>


<div class="div1 center">
    我是外部盒子
    <div class="div2 center">
        我要居中
    </div>
</div>
</body>
</html>
第七種

第八種

兩者都要固定定位,不常用

缺點(diǎn):需要設(shè)置position屬性,網(wǎng)頁(yè)復(fù)雜時(shí)容易擾亂頁(yè)面布局,而且只是元素的起始位置居中
<html>
<head>
    <meta charset="UTF-8">
    <title>demo</title>
    <style type="text/css">


        .div1 {
            width: 100px;
            height: 100px;
            border: 1px solid #000000;
            background-color: red;
            position: relative;
        }

        .div2 {
            height: 40px;
            width: 40px;
            background-color: green;
            margin:30px 30px;

        }

        .center{
            position: fixed;
            left: 50%;
        }

    </style>
</head>
<body>


<div class="div1 center">
   
    <div class="div2 center">
        我要居中
    </div>
</div>
</body>
</html>
第八種方法
最后編輯于
?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 問(wèn)答題47 /72 常見(jiàn)瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,166評(píng)論 1 92
  • jQuery基礎(chǔ) 什么是JQ?一個(gè)優(yōu)秀的JS庫(kù),大型開發(fā)必備JQ的好處?一簡(jiǎn)化JS的復(fù)雜操作二不再需要關(guān)心兼容性三...
    幺七閱讀 1,008評(píng)論 0 2
  • 本課來(lái)自http://www.imooc.com/learn/9請(qǐng)不要用作商業(yè)用途。 HTML5 HTML介紹 H...
    PYLON閱讀 3,450評(píng)論 0 5
  • 各種純css圖標(biāo) CSS3可以實(shí)現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,988評(píng)論 0 8
  • 人這一生中總會(huì)遇到那么一個(gè)人,他讓你愛(ài)的卑微,愛(ài)的落寞,愛(ài)的撕心裂肺,卻義無(wú)反顧的執(zhí)著! 1.18歲 “瀟瀟...
    等愛(ài)的故人閱讀 734評(píng)論 2 0

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