码迷,mamicode.com
首页 > 其他好文 > 详细

关于居中

时间:2016-01-14 18:53:08      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

 

根据块状元素和行内元素的特性来区分

块状元素:主要特点是能占据一行,高度行高内边距外边距能控制,能容纳内联元素和其他块状元素

行内元素:不能独占一行,随内容大小而定。无法设置宽高,设置的外边距只能在左右上起作用,上下没有作用

所以要根据元素的特性做的居中

父元素是块状元素,子元素是行内元素

水平居中:

给父元素添加text-align:center

---------------------举一个栗子---------------------


1
2
3
4
5
6
7
8
9
10
11
12
13
.parent{
    width200px;
    height200px;
    text-aligncenter;
    background#3cffff;
}
.child{
   background#f9ffc3;
}
 
<div class="parent">
     <span class="child" >苗呜呜</span>
</div>

技术分享


 

垂直居中:

子元素上添加line-height:父元素的高度

---------------------举一个栗子---------------------


1
2
3
4
5
6
7
8
9
10
11
.parent{
    width200px;
    height200px;
   }
   .child{
    line-height200px;
   }
 <div class="parent">
     <span class="child" >苗呜呜</span>
   </div>
    

 技术分享


以上两个方法合在一起就是行内元素的垂直居中了

---------------------举一个栗子---------------------


1
2
3
4
5
6
7
8
9
10
11
12
13
.parent{
  width200px;
  height200px;
  background#3cffff;
  text-align:center;
}
.child{
  line-height200px;
  background#f9ffc3;
}
<div class="parent">
  <span class="child" >苗呜呜</span>
</div>

技术分享


如果是子元素是img,img元素比较特殊,属于替换内联元素, line-height对img没有效果,无法实现垂直居中

---------------------举一个栗子---------------------


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.parent{
  width:1000px;
  height600px
  background#3cffff;
  text-aligncenter;
   }
.child{
  vertical-alignmiddle;
}
.child2{
  height100%;
  width0;
}
<div class="parent">
  <img class="child2">
</div>

技术分享


 

css为child2的img是起到辅助作用,不要写src,否则会多一个边框,其实使用其他行内元素也行,比如span,只要把该元素的高度设置为父元素一样的高度就行了。vertical-align这个属性的特点,它是相对兄弟级行高(line-height)来定位,它只对行内样式有效。下面设置一个img只是撑开父元素的行高。

关于内联元素td的垂直水平居中

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
.parent{
  width:200px;
  height:200px;
  text-aligncenter;
  background#3cffff;
}
<table class="parent">
  <tr>
    <td>~喵了个咪~</td>
  </tr>
</table>

技术分享


用table-cell ,文字和图片居中,但是background是铺满整个父元素,而不是居中

---------------------举一个栗子---------------------


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.parent {
    width1000px;
    height600px;
    display: table;
    text-aligncenter;
    background#3cffff;
}
 
.child {
    displaytable-cell;
    vertical-alignmiddle;
    margin0 auto;
    background#f9ffc3;
}
<div class="parent">
  <div class="child">~喵了个咪~</div>
</div>

 

技术分享


 

 

父元素是块状元素,内元素是块状元素

1.水平居中

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.parent{
width:1000px;
height:600px;
position:relative;
background#3cffff;
}
.child{
width:200px;
height:200px;
margin0 auto;
background#f9ffc3;
}
<div class="parent">
    <div class="child"></div>
</div>

技术分享


 

2.如果要让上面的垂直水平居中的话,要多加一个position:absolute,left:0;right:0;top:0;bottom:0;

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.parent{
  width:1000px;
  height:600px;
  position:relative;
  background#3cffff;
}
.child{
  width:200px;
  height:200px;
  marginauto;
  position:absolute;
  left:0;
  right:0;
  top:0;
  bottom:0;
  background#f9ffc3;
}
<div class="parent">
    <div class="child"></div>
</div>

技术分享


 

3.借助其他元素实现垂直水平居中

记得把辅助元素放在子元素前面哈。

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.parent{
    positionrelative;
    width:1000px;
    height:600px;
}
.child-2{
    height50%;
    margin-bottom-100px/*是child的(padding+height)/2*/
}
.child{
    width:200px;
    height:200px;
    marginauto;
}
<div class="parent ">
     <div class="child-2"></div>
     <div class=" child"></div>
</div>

技术分享


 4.垂直居中 行内元素 块状元素都可以

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.parent{
  displayblock;
  positionrelative;
  width:1000px;
  height:600px;
  background#3cffff;
}
.parent:after{
  display: inline-block;
  vertical-alignmiddle;
  content‘‘;
  height100%;
  background#f9ffc3;
}
.child{
  display: inline-block;
  vertical-alignmiddle;
}
<div class="parent">
     <div class="child"></div>
</div>

技术分享


5.弹性盒式布局  ie11以上才支持  只针对父元素做了设置,子元素不需要设置。

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.parent{
  width:1000px;
  height:1000px;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items:center;
          align-items:center;
  -webkit-justify-content:center;  /*适用于父元素*/
          justify-content:center;
          background#3cffff;
}
.child{
  width:200px;
  height:200px;
  background#f9ffc3;
}
<div class="parent">
     <div class="child"></div>
</div>

技术分享


6.使用margin-top;

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.parent{
  positionrelative;
  width:1000px;
  height:600px;
  background#3cffff;
}
.child{
  width:200px;
  height:200px;
  positionabsolute;
  top:50%;
  left0;
  right0;
  marginauto;
  margin-top-100px;
  background#f9ffc3;
}
<div class="parent">
     <div class="child"></div>
</div>

技术分享


7.根据上面还有一种传统的大家都通用的办法,这个办法具有兼容性,兼容到ie6

父元素的position和子元素的position不能相同,要么父元素为absolute,子元素为relative,要么父元素为relative,子元素为absolute;

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.parent {
  width1000px;
  height600px;
  background#c3ffff;
  positionrelative;
  /*必须加上,否则child不能居中*/
}
     
.child {
  width200px;
  height200px;
  margin-100px 0 0 -100px;
  positionabsolute;
  left50%;
  top50%;
  background#f9ffc3;
}
<div class="parent">
     <div class="child"></div>
</div>

技术分享


8.用transform,只设置子元素

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.parent{
  width:1000px;
  height:600px;
  background#3cffff;
}
.child{
  width:200px;
  height:200px;
  position:relative;
  top50%;
  left50%;
  transform:translate(-50%,-50%);
  -webkit-transform:translate(-50%,-50%); 
  background#f9ffc3;
}
<div class="parent">
     <div class="child"></div>
</div>

技术分享


9.如果居中是一张图片的话,其实用background也就可以了

---------------------举一个栗子--------------------- 


1
2
3
4
5
6
.parent{
  backgroundurl(http://f5.topitme.com/5/93/a8/115632420139ea8935o.jpg) no-repeat center  center #3cffff;
  width:1000px;
  height:600px;
}
<div class=" parent"></div>

技术分享


 

技术分享就是这样,我已知的居中方法就这些,如果有人还有更好的方法或者我上面有错的地方及时提出哈~

关于居中

标签:

原文地址:http://www.cnblogs.com/yinglin/p/5129763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!