Summary of CSS Vertical center arrangement

Source: Internet
Author: User

Preface: This is the author's understanding and finishing after learning. If there are errors or questions, please correct me, I will continue to update!

There are a lot of places for vertical centering, and there are many ways to solve them.

known element width can be used position positioning + margin negative method

Absolute Positioning + 4 directions all 0px + Margin:auto can be based on the parent container horizontal vertical Center, if only vertical center, it can be left and right to delete, and horizontal center is also a lot of methods
Absolute positioning + Left 50% + margin left: Half the width of negative values can be done horizontally center right can also
Absolute Positioning + Upper 50% + margin: The negative value of the height of half can be vertical center bottom can also


<!        DOCTYPE html>


View Code

unknown element Width can be moved with position positioning + transform:translate (x, y)
Absolute Positioning + Upper 50% + 50% + transform:translate ( -50%,-50%) can be vertically centered


<!        DOCTYPE html>



the IMG image in P is centered vertically :

Can be used Vertical-align:middle, but this property only in the Inline-block type (inline also has influence) the element body function, vertical-align Understanding I must continue to study, also welcome everybody to correct


Method One: Picture Vertical-align:middle + Parent element height is consistent with Line-height


<! DOCTYPE html>


View Code

Method Two: Parent element settings Display:table-cell and Vertical-align


<! DOCTYPE html>
Related Article

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.