Home >Web Front-end >HTML Tutorial >聊聊css水平垂直居中那些事_html/css_WEB-ITnose

聊聊css水平垂直居中那些事_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:05:421455browse

1、水平居中实现方法

<div class="demo">    我是一个水平居中的div</div>
/*方法1*/.demo{width: 960px;margin: 0 auto;}		/*方法2*/.demo{width: 960px;height: 400px;background: #f00;position: relative;left: 50%;margin-left: -480px;}

2、水平垂直居中实现方法

<div class="demo">    我是一个水平垂直居中的div</div>
/*方法1:无兼容性问题*/.demo{width: 400px;height: 400px;background: #f00;position: absolute;top: 50%;left: 50%;margin: -200px 0 0 -200px;}
/*方法2: 在ie7浏览器下不支持*/.demo{width: 400px;height: 400px;background: #f00;position: absolute;top: 0;left: 0;right: 0;bottom: 0;margin: auto;}






Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn