CSS Layout - Horizontal & Vertical Align
Horizontal & vertical center alignment
Element center alignment
To horizontally center-align an element (such as <div>), you can usemargin: auto;。
Setting the width of the element will prevent it from overflowing to the edges of the container.
The element is centered by specifying a width and distributing the empty margins on both sides equally:
The div element is centered
Example
Try it Yourself »
Note:If not setwidthproperty (or setting it to 100%), the center alignment will not work.
Text center alignment
If you only want to center the text within an element, you can usetext-align: center;
Text center alignment
Tip:For more text alignment examples, please refer toCSS TextChapter.
Image center alignment
To center an image, you can usemargin: auto;And place it intoblockelement:
Left/right alignment - using positioning
We can useposition: absolute;property to align elements:
Example Tutorial -- Learning is not only technology, but also dreams!!!
Example
Try it Yourself »
Note:Absolutely positioned elements are removed from the normal flow and can overlap other elements.
Tip:When usingpositionWhen aligning elements, usually<body>the element will setmarginandpadding. This can avoid visible differences in different browsers.
When using the position property, IE8 and earlier versions have a problem. If the container element (in our case <div class="container">) has a specified width and the !DOCTYPE declaration is omitted, then IE8 and earlier versions will add an extra 17px margin on the right side. This seems to be space reserved for the scrollbar. When using the position property, always set the !DOCTYPE declaration:
Example
Try it Yourself »
Left/Right Align - Using the float Method
We can also usefloatproperty to align elements:
Example
Try it Yourself »
When aligning elements like this, it is a good idea to predefine the margin and padding of the <body> element. This avoids visible differences in different browsers.
Note: If the child element's height is greater than the parent element, and the child element is set to float, then the child element will overflow. At this point, you can use "clearfix(clear float)" to solve this problem.
We can add overflow: auto; to the parent element to solve the problem of child element overflow:
When using the float property, IE8 and earlier versions have a problem. If the !DOCTYPE declaration is omitted, then IE8 and earlier versions will add an extra 17px margin on the right side. This seems to be space reserved for the scrollbar. When using the float property, always set the !DOCTYPE declaration:
Example
Try it Yourself »
Vertical Center Alignment - Using padding
There are many ways in CSS to achieve vertical centering. A simple way is to use at the top of the headerpadding:
I am vertically centered.
If you want to center both horizontally and vertically, you can usepaddingandtext-align: center:
I am centered both horizontally and vertically.
Vertical centering - using line-height
I am vertically centered.
Example
Try it Yourself »
Vertical centering - using position and transform
In addition to usingpaddingandline-heightBesides the property, we can also usetransformproperty to set vertical centering:
Example
Try it Yourself »
Tip:For more content on the transform property, you can refer to2D Flip Chapter。
More Examples
CSS Use margin to center align div
CSS Use absolute positioning to right align div
other extensions