如何實(shí)現(xiàn)居中

水平居中
1、內(nèi)聯(lián)元素居中:父元素設(shè)置text-align:center

 <div>hello world<div>

div{
  text-align:center;
}

2、塊級(jí)元素居中:該元素設(shè)置margin-left:auto;margin-right:auto

 <div class="parent">
    <div class="child"></div>
  <div>
.parent{
  height:100px;
  background:red;
}
.child{
  height:50px;
  width:40px;
  background:green;
  margin-left:auto;
  margin-right:auto;
}

垂直居中
塊級(jí)元素居中:
方法一:不設(shè)置.parent的height,設(shè)置.parent{padding:10px 0}

<div class="parent">
    <div class="child"></div>
<div>
.parent{
  padding:10px 0;
  background:red;
}
.child{
  height:50px;
  width:40px;
  background:green;
  margin-left:auto;
  margin-right:auto;
}

方法二:設(shè)置.parent的height,通過(guò)postion absoulte margin auto實(shí)現(xiàn)

 <div class="parent">
    <div class="child"></div>
  <div>
.parent{
  height:600px;
  background:red;
  position:relative;  
}
.child{
  background:green;
  position:absolute;
  width:300px;
  height:300px;
  top:0;
  left:0;
  right:0;
  bottom:0;
  margin:auto;
}

方法三:設(shè)置.parent的height,通過(guò)postion absolute margin 負(fù)值實(shí)現(xiàn)

  <div id="parent">
    <div id="child"></div>
  <div>
.parent{
  height:600px;
  background:red;
  position:relative;  
}
.child{
  background:green;
  position:absolute;
  width:300px;
  height:300px;
  top:50%;
  left:50%;
  margin-left:-150px;
  margin-top:-150px;
}

方法四:設(shè)置.parent的height,通過(guò)transform: translate(-50%,-50%);實(shí)現(xiàn)
translate(x,y) 括號(hào)的百分比數(shù)據(jù),會(huì)以本身的長(zhǎng)寬做參考,比如,本身的長(zhǎng)為100px,高為100px. 那填(50%,50%)就是向右,向下移動(dòng)50px,添加負(fù)號(hào)就是向著相反的方向移動(dòng)

 <div class="parent">
    <div class="child">
    </div>
  <div>
.parent{
  height:600px;
  background:red;
  position:relative;  
}
.child{
  background:green;
  position:absolute;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);
}

方法五:通過(guò)flex實(shí)現(xiàn)

  <div class="parent">
    <div class="child"></div>
  <div>
.parent{
  background:red;
  height:600px;
  display: flex;
  justify-content: center;//justify-content屬性定義了項(xiàng)目在主軸上的對(duì)齊方式。
  align-items: center; //align-items屬性定義項(xiàng)目在交叉軸上如何對(duì)齊。
}
.child{
  background:green;
  width:300px;
  height:200px;
}

常用屬性
transform
translate(x,y)
scale(x,y)
rotate(angle)

display:flex
justify-content:center//屬性定義了項(xiàng)目在主軸上的對(duì)齊方式。
align-items:center//屬性定義項(xiàng)目在交叉軸上如何對(duì)齊。

最后編輯于
?著作權(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)書(shū)系信息發(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,135評(píng)論 1 92
  • 收聽(tīng)音頻,戳鏈接,舊號(hào)itclan已暫停使用,歡迎關(guān)注微信itclanCoder公眾號(hào)可收聽(tīng)更多音頻 前言 關(guān)于網(wǎng)...
    itclanCoder閱讀 8,349評(píng)論 3 30
  • 原文地址:CSS各種居中實(shí)現(xiàn)方式 CSS居中是每次布局都需要面對(duì)的問(wèn)題,但是同一個(gè)居中方法并不是任何元素都能使用的...
    薛普定朗諤克閱讀 904評(píng)論 1 13
  • 單列布局水平居中水平居中 水平居中的頁(yè)面布局中最為常見(jiàn)的一種布局形式,多出現(xiàn)于標(biāo)題,以及內(nèi)容區(qū)域的組織形式,下面介...
    Osmond_wang閱讀 380評(píng)論 0 1
  • 植物園出來(lái)直奔獅峰龍井附近,買了一次茶,和茶農(nóng)聊了一會(huì)兒,喝了幾杯茶,知道了一些茶識(shí)。 龍井茶特點(diǎn)“色綠、香郁、味...
    oriyum閱讀 487評(píng)論 0 1

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