impress 2019-07-01
第一种方式:给div设置一个宽度,然后添加margin:0 auto属性.
div{
  width:200px;
  margin:0 auto;
}第二种方式:让绝对定位的div居中
div{
  position:absolute;
  width:300px;
  height:300px;
  margin:auto;
  top:0;
  left:0;
  bottom:0;
  right:0;
  background-color:pink;
}第三种方式:水平垂直居中一
div{
  position:absolute;
  width:500px;
  height:300px;
  top:50%;
  left:50%;
  margin:0 0 0 -250px;
  background-color:pink;
}第四种方式:未知容器的宽高,利用'transform'属性
div{
  position:absolute;
  width:500px;
  height:300px;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%)
  background-color:pink;
}第五种方式:利用flex布局,需考虑兼容性
container{
  display:flex;
  align-item:center;/*垂直居中*/
  justify-content:center;/*水平居中*/
}
container div{
  width:100px;
  height:100px;
  background-color:pink
}每日两道前端面试题20190307
希望睡一觉起来 阳光会出来.
/*垂直居中,div上边界距离窗口上边的距离为窗口高度的50%,并针对不同浏览器进行兼容。-- 在外层添加一个div,把行内容居中,添加.row .justify-content-center -->