居中的几种方法

一:通过margin负值

.one{ position: absolute; width: 200px; height: 200px; top: 50%; left: 50%; margin-left: -100px;}

兼容性好,但是必须要定宽高

二:通过margin:auto

.two{ position: absolute; width: 200px; height:200px; margin: auto 0;}

以上两种方法都可以把absolute换成fixed,注意,fixed在ie下不支持

三:行内元素居中

.three{ width: 100px; height: 100px; line-height: 100px; text-align: center;}

这种方法只能居中行内元素。常用于文字对其居中

四:transform居中

.four{ position: absolute; top: 50%; height: 50%; transform: translate(-50%, -50%);}

存在css3浏览器兼容问题

五:flex布局

.five{ display: flex; align-items: center; justify-content: center;}

存在css3浏览器兼容问题

posted @ 2017-10-27 21:52  MMOMPG  阅读(353)  评论(0)    收藏  举报