溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

css3怎么讓盒子水平居中

發(fā)布時間:2022-01-21 09:38:04 來源:億速云 閱讀:254 作者:kk 欄目:web開發(fā)

這篇文章的內(nèi)容主要圍繞css3怎么讓盒子水平居中進行講述,文章內(nèi)容清晰易懂,條理清晰,非常適合新手學習,值得大家去閱讀。感興趣的朋友可以跟隨小編一起閱讀吧。希望大家通過這篇文章有所收獲!

css3讓盒子水平居中的方法:1、使用margin屬性,給盒子元素添加“margin: 0 auto;”樣式即可水平居中;2、利用flex彈性布局來實現(xiàn)水平居中;3、利用position和transform屬性實現(xiàn)水平居中。

本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。

在CSS中如何讓盒子水平居中是很常見的面試題,盒子居中是相對于父元素來說的,因此我們讓盒子居中時,往往采用嵌套的方式,讓父盒子套著子盒子 。

在父子盒子嵌套下,讓子盒子居中的方式:

  • 第一種方法:margin: 0 auto,使用邊框,但是margin使用會影響其他盒子的使用,不太推薦使用;

  • 第二種方法:position, 使用定位,子絕父相,再left:50%,margin-left:負的盒子寬度的一半,這是最常用的方法;

  • 第三種方法:flex,彈性布局,讓子盒子居中,但是樣式要寫在父盒子中,display:flex,just-content:center;

  • 第四種方法:在position基礎(chǔ)上,把margin-left換成CSS3中的transform:translate(-50px);

  • 第五種方法:在position的基礎(chǔ)上,只保留子絕父相,然后在子盒子中加上margin:auto、left:0、right:0;

    補充:在第五種方法上,加上top:0,bottom:0,可以實現(xiàn)垂直和水平都居中

<div id="father">
    <div id="son"></div>
</div>
<style>
    #father{
        width: 400px;
        height: 200px;
        border: 3px solid pink;
    }
    #son{
        width: 100px;
        height: 100px;
        border: 2px solid red;
    }
</style>

css3怎么讓盒子水平居中

使用margin實現(xiàn)水平居中:

<style>
#father{
    width: 400px;
    height: 200px;
    border: 3px solid pink;
    margin: 30px auto; /* 讓父元素相對于body居中 */
}
#son{
    width: 100px;
    height: 100px;
    border: 2px solid red;
    margin: 0 auto;/* 讓子元素相對于father居中 */
}
</style>

使用定位,子絕父相,再left:50%,margin-left:負的盒子寬度的一半:

<style>
#father{
    width: 400px;
    height: 200px;
    border: 3px solid pink;
    margin: 0 auto;
    position: relative;
}
#son{
    width: 100px;
    height: 100px;
    border: 2px solid red;
    position: absolute;
    left: 50%;
    margin-left: -50px;
}
</style>

flex,彈性布局,讓子盒子居中,但是樣式要寫在父盒子中:

<style>
#father{
    width: 400px;
    height: 200px;
    border: 3px solid pink;
    margin: 0 auto;
    display: flex;
    justify-content: center;
}
#son{
    width: 100px;
    height: 100px;
    border: 2px solid red;
}
</style>

在position的基礎(chǔ)上,只保留子絕父相,然后在子盒子中加上margin:auto、left:0、right:0:

<style>
#father{
    width: 400px;
    height: 200px;
    border: 3px solid pink;
    margin: 0 auto;
    position: relative;
}
#son{
    width: 100px;
    height: 100px;
    border: 2px solid red;
    position: absolute;
    margin: auto;
    left: 0;
    right: 0;
}
</style>

以上幾種方法都可以實現(xiàn)盒子的水平居中,如果大家有其它優(yōu)(奇)秀(葩)方法,歡迎交流鴨!

css3怎么讓盒子水平居中

第五種方法補充:再加上top:0,bottom:0可以實現(xiàn)水平和垂直都居中 :

<style>
#father{
    width: 400px;
    height: 200px;
    border: 3px solid pink;
    margin: 0 auto;
    position: relative;
}
#son{
    width: 100px;
    height: 100px;
    border: 2px solid red;
    position: absolute;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
}
</style>

css3怎么讓盒子水平居中

什么是css

css是一種用來表現(xiàn)HTML或XML等文件樣式的計算機語言,主要是用來設(shè)計網(wǎng)頁的樣式,使網(wǎng)頁更加美化。它也是一種定義樣式結(jié)構(gòu)如字體、顏色、位置等的語言,并且css樣式可以直接存儲于HTML網(wǎng)頁或者單獨的樣式單文件中,而樣式規(guī)則的優(yōu)先級由css根據(jù)這個層次結(jié)構(gòu)決定,從而實現(xiàn)級聯(lián)效果,發(fā)展至今,css不僅能裝飾網(wǎng)頁,也可以配合各種腳本對于網(wǎng)頁進行格式化。

感謝你的閱讀,相信你對“css3怎么讓盒子水平居中”這一問題有一定的了解,快去動手實踐吧,如果想了解更多相關(guān)知識點,可以關(guān)注億速云網(wǎng)站!小編會繼續(xù)為大家?guī)砀玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI