Home >Web Front-end >HTML Tutorial >三行css代码实现垂直居中_html/css_WEB-ITnose

三行css代码实现垂直居中_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:08:451173browse

html结构为:

<div id='div2'>	<div id='div1'></div></div>

css代码为:

#div2{	height:300px;width:300px;	border:1px red solid;	text-align:center;	margin:auto;}#div1{      height:50px;width:50px;      position: relative;      top: 50%;      transform: translateY(-50%);      border:1px red solid;      margin:auto;}

其中最核心的css是这三行

      position: relative;      top: 50%;      transform: translateY(-50%);

为了浏览器兼容性,需要在transform属性前添加浏览器前缀

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