CSS Centering Method Summary

Source: Internet
Author: User

Method One: Line-height

<div class= "vertical" style= "width:200px;height:200px;border:2px solid #999" > Center display </div>.vertical {  line-height:200px;//here will be the same as height        }

  

Method Two: Position positioning mode

Absolute positioning (Position:absolute) is set for the container, and the positioning height (top:50%) and margin-top are half the height (MARGIN-TOP:-HEIGHT/2)

<div class= "Vertical" > Center display </div>.vertical {
height:100px; Position:absolute; The position of the top edge of the top:50%;/* element is half the height of the parent element /margin-top: -50px;/* sets the top negative margin of the element, and the size is half the height of the element */ }

Method Three: Let div simulate table effect

  

Method Four: Give the upper and lower padding values

  

Method Five: margin:0 Auto for horizontal centering

Satisfies two conditions: first , the element needs to have a fixed width value, and the Margin-left and margin-right of the second element must be set to auto, neither of which has the effect of having the element horizontally centered without any one of the two conditions.

<div class= "Horizontal" >content</div>.horizontal {width:200px; margin:0 auto;}  

It is not possible to use this similar way to center vertically, only for horizontal centering

Method Six: Use absolute positioning with negative margin to achieve horizontal centering

CSS Centering Method Summary

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.