元素在页面垂直水平居中的方法

1.绝对定位

.box {
  width: 100px;
  height: 100px;
  background: orange;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left:-50px;/*-width/2*/
  margin-top:-50px;/*-height/2*/       
}

2.绝对定位+css3 translate

注:translate 百分比是相对于自己的宽高

.box {
  width: 100px;
  height: 100px;
  background: orange;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

3.父级设置display:box; box-pack:center; box-align:center;

.box {
  width: 100px;
  height: 100px;
  background: orange;
  /* Firefox */
  display: -moz-box;
  -moz-box-pack: center;
  -moz-box-align: center;
  /* Safari、Opera 以及 Chrome */
  display: -webkit-box;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  /* W3C */
  display: box;
  box-pack: center;
  box-align: center;
 }

 

posted @ 2016-10-11 13:51  菜鸟一小只  阅读(433)  评论(0)    收藏  举报