Home > Web Front-end > HTML Tutorial > Three ways to center an element vertically and horizontally

Three ways to center an element vertically and horizontally

PHP中文网
Release: 2017-04-01 17:04:28
Original
1509 people have browsed it

The first method:

p.box{
weight:200px;
height:400px;
position:absolute;
left:50%;
top:50%;
margin-left:-100px;
margin-top:-200px;
}
Copy after login

*Good compatibility; Disadvantage: Must know the width and height of the element

-------------

The second method Method:

p.box{
weight:200px;
height:400px;
position:absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
Copy after login

*This is the style in css3; Disadvantages: Poor compatibility, only supports IE9+ browsers

---------------

The third method

p.box{
weight:200px;
height:400px;
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
}
Copy after login

*Better compatibility, Disadvantage: Browsers below IE7 are not supported

The above are the content of the three methods to center an element vertically and horizontally. Please pay attention to more related content PHP Chinese website (www.php.cn)!


Related labels:
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template