当前位置: 代码迷 >> Web前端 >> DIV的上下左右均正中
  详细解决方案

DIV的上下左右均正中

热度:153   发布时间:2013-11-04 16:56:03.0
DIV的上下左右均居中
主要用于弹出框,用css实现。
div {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: -150px 0 0 -200px;
  width: 400px;
  height: 300px;
  background-color: #CCC;
}


核心思路:
第一步:定位到屏幕中央,即top和height都用50%
第二步:用margin的负值,使div向上和向左分别移动高度和跨度的一半。

  相关解决方案