居中方法

1.垂直居中(方法一)
.wrap h2 {
margin:0;
height:100px;
line-height:100px;
background:#ccc;
}

  <div class="wrap">
        <h2>Hello world</h2>
 </div>

總結(jié): line-height 設(shè)置垂直居中
行高,指代文本中,行與行之間的基線間的距離。
Line-height行高屬性,運用到對文字排版,實現(xiàn)上下排文字間隔距離,以及單排文字在一定高度情況上下垂直居中布局。

2.垂直居中(方法二)
#parent {
display: flex;
align-items: center;
/justify-content: center;/
/水平居中/
width: 200px;
height: 200px;
background: yellow;
}
<div id="parent">
這是一個盒子垂直居中
</div>

總結(jié):彈性盒的
justify-content:center;//元素在橫軸的對齊方式
align-items:center;//元素在縱軸的對齊方式

  1. vertical-align 設(shè)置圖片垂直居中(行內(nèi)元素 方法三)

    *{margin:0;padding:0;}
    .parent{
    margin-left: 100px;
    margin-top: 100px;
    width: 600px;
    height: 400px;
    border: 1px solid #ddd;
    border-radius: 15px;
    background-color: #fff;
    box-shadow: 0 3px 18px rgba(0,0,0,.5);
    text-align: center;
    }
    .child{
    width: 0;
    line-height: 400px;
    }
    img{
    vertical-align: middle;
    }

<div class="parent">
<img src="a21.png" alt="">
<span class="child"></span>
</div>

總結(jié):vertical-align 屬性設(shè)置元素的垂直對齊方式。

4.定位的居中方法(1)
.father{
width: 200px;
height: 200px;
background: pink;
position: relative;
}
.son{
width: 100px;
height: 100px;
background: yellowgreen;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div class="father">
<div class="son"></div>
</div>

總結(jié):首先我們要了解樣式中的這兩種定位;

absolute(絕對定位):將被賦予的對象從文檔流中拖出,使用left,right,top,bottom等屬性相對于最接近的一個最有定位設(shè)置的父級對象進行絕對定位,如果父級沒有進行定位屬性設(shè)置,則按照默認規(guī)則來設(shè)定(根據(jù)body左上角作為參考進行定位),同時絕對定位的對象可層疊。

relative(相對定位):對象不可重疊,使用left,right,top,bottom等屬性在正常的文檔流中進行定位,其對象不可以層疊。

5.水平居中行內(nèi)元素居中(1)
.div1{
text-align:center;
}
<div class="div1">Hello world</div>

6.水平居中相對定位(2)
.wrap{
float:left;
position:relative;
left:50%;
clear:both;
}
.wrap-center{
background:#ccc;
position:relative;
left:-50%;
}

<div class="wrap">
<div class="wrap-center">Hello world</div>
</div>

總結(jié): 通過給父元素設(shè)置 float,然后給父元素設(shè)置 position:relative 和 left:50%,
子元素設(shè)置 position:relative 和 left: -50% 來實現(xiàn)水平居中。

7.圖片水平居中
.tu img{
display: block;
margin:0 auto;
}

<div class="tu">
<img src="img/one.jpg" >
</div>

8.table布局居中方法(1)
.father{
width: 200px;
height: 200px;
background: pink;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.son{
display: inline-block;
width: 100px;
height: 100px;
background: yellowgreen;
}

     <div class="father">
    <div class="son"></div>
</div>

10.div絕對定位水平垂直居中【margin 負間距】

     div{
        width:200px;
        height: 200px;
        background:green;
        position: absolute;
        left:50%;
        top:50%;
        margin-left:-100px;
        margin-top:-100px;
    }

11、<div class="parent">
<div class="child">
111
</div>
</div>

.parent{
     height: 140px;
    background: red;
             display: grid;
}
  .child{
   margin: auto;
}

用法:grid 給他父級元素
margin: auto 給他的子元素
12、 .div{
width: 200px;
height: 200px;
background: pink;
margin: auto;
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
}
<div class="div"></div>

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

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

  • 各種純css圖標(biāo) CSS3可以實現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,996評論 0 8
  • CSS布局解決方案(終結(jié)版) 前端布局非常重要的一環(huán)就是頁面框架的搭建,也是最基礎(chǔ)的一環(huán)。在頁面框架的搭建之中,又...
    殺個程序猿祭天閱讀 635評論 0 2
  • 前端布局非常重要的一環(huán)就是頁面框架的搭建,也是最基礎(chǔ)的一環(huán)。在頁面框架的搭建之中,又有居中布局、多列布局以及全局布...
    一個敲代碼的前端妹子閱讀 874評論 0 12
  • 墨蘭公主 淘米水潔面乳超級好用,溫和不刺激,卸妝液都省了??淘米水,洗臉可以美白,淡化色素并且對痘痘肌膚,油性肌膚...
    我是阿酷閱讀 479評論 0 1
  • 漢傳佛教僧人為什么不乞食不吃肉? 佛在世時,出家僧人們每日身披袈裟,手持著缽,四處乞食游化,只有在雨季里,才回到精...
    行愿文化閱讀 1,095評論 0 2

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