html css居中

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;//元素在縱軸的對齊方式

3.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>
9.div絕對定位水平垂直居中【margin 負間距】
div{
  width:200px;
  height: 200px;
  background:green;
  position: absolute;
  left:50%;
  top:50%;
  margin-left:-100px;
  margin-top:-100px;
}
10.margin居中
<div class="parent">
  <div class="child">
    111
  </div>
</div>

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

用法:grid 給他父級元素
margin: auto 給他的子元素

11.絕對定位+margin垂直居中
.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ù)。

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