Several methods that allow elements to be centered horizontally vertically

Source: Internet
Author: User

1. Negative margin method: This is a more commonly used method, in the knowledge of the width of the elements of the premise can be used

1 <div id= "a" ></div>2 3 #a{4 Height:300px;5 width:300px;6 position:Absolute;7 Top:50%;8  Left:50%;9 Margin-left:-150px;Ten Margin-top:-150px; One}

Note: Negative margin is a very interesting usage, and in-depth understanding will find him very useful in layout.
Advantages : Low code volume, good compatibility.
Cons: non-responsive methods, content may be outside the container.

2.transform Method:

 <div id= "a" ></div> #a  { width< /span>: 50% ; :  20% ;  background :  green ;  position :  absolute ; : 50% ;  left :  50% ;  Transform : translate ( -50%, -50%) ; -webkit-transform : translate ( -50%, -50%) ; -ms-transform : translate ( -50%, -50%) ;} 

Advantages : Low code volume, variable width and height, corresponding layout
cons : IE8 is not supported and may require a vendor prefix

3.Flexbox method

<div class= "Vertical-container" > <div class= "a" ></div></div>.vertical-container{Height:300px;width:300px;background:#ccc;Display:-webkit-flex;Display:Flex;-webkit-align-items:Center;Align-items:Center;-webkit-justify-content:Center;justify-content:Center;}. A{width:200px;Height:200px;background:Green;}

Note: The use of Flexbox is much more than that, and there are many interesting uses in the layout.

4. "Fully Horizontal vertical Center": Must set the height of the element, the picture itself has a height can not be set.

<div id= "a" ></div> #a {      width: 200px;     height: 200px;     background: Red;     margin:auto;     position: absolute;     top:0; Left:0; Right: 0; Bottom: 0;}

Advantages : Low code volume, good compatibility

The last method, I really do know, please forgive me really very dishes, please forgive me I really do not see, this is a few years ago by everyone with mushy method I actually now know, O (︶^︶) o!

Several methods that allow elements to be centered horizontally vertically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.