宽高都200px的div在浏览器窗口居中(水平垂直都居中)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27855219/article/details/89175149

1.fixed,从中间移动定位

  position:fixed;width:200px;height:200px;left:50%;top:50%;

  margin-left:-100px;margin-top:-100px

第一行设置完:盒子的左上角的点,刚好处于浏览器页面中间,需要向左向上移动才可实现整体的居中,所以margin-left=负的盒子宽的一半,margin-top=负的盒子高的一半

2.从左上利用margin:auto定位

{width:200px;

height:200px;

position:absolute;

top:0;left:0;

right:0;

bottom:0;

margin:auto;}

猜你喜欢

转载自blog.csdn.net/qq_27855219/article/details/89175149