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

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

WBOY
Release: 2016-06-21 09:08:45
Original
1161 people have browsed it

html结构为:

<div id='div2'>	<div id='div1'></div></div>
Copy after login

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;}
Copy after login

其中最核心的css是这三行

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

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template