CSS Layout - Horizontal & Vertical Alignment
Horizontal & Vertical Center Alignment
Center Aligning Elements
To horizontally center an element (such as
Setting a width on the element will prevent it from overflowing the edges of its container.
The element is given a specified width, and the empty margins on both sides are distributed equally:
The div element is centered
Example
.center
{
margin:
auto
;
width:
50
%
;
border:
3
px
solid
green
;
padding:
10
px
;
}
Try it »
Note: If the width property is not set (or is set to 100%), centering will not work.
Text Center Alignment
If you only want to center the text within an element, you can use text-align: center;
Text Center Alignment
Example
.center
{
text-align:
center
;
border:
3
px
solid
green
;
}
Try it »
Tip: For more text alignment examples, see CSS Text section.
Image Center Alignment
To center an image, you can use margin: auto; and place it in a blocks element:
Example
img
{
display:
block
;
margin:
auto
;
width:
40
%
;
}
Try it »
Left and Right Alignment - Using Positioning
We can use the position: absolute; property to align elements:
Runoob Tutorial -- learn not only technology, but also dreams!!!
Example
.right
{
position:
absolute
;
right:
0
px
;
width:
300
px
;
border:
3
px
solid
#73AD21
;
padding:
10
px
;
}
Try it »
Note: An absolutely positioned element is removed from the normal flow and can overlap other elements.
Tip: When using position to align elements, the element is usually given margin and padding. This helps avoid visible differences across different browsers.
When using the position property, there is a problem in IE8 and earlier versions. If the container element (in our case,
Example
body
{
margin:
0
;
padding:
0
;
}
.container
{
position:
relative
;
width:
100
%
;
}
.right
{
position:
absolute
;
right:
0
px
;
width:
300
px
;
background-color:
#b0e0e6
;
}
Try it »
Left and Right Alignment - Using Float
We can also use the float property to align elements:
Example
.right
{
float:
right
;
width:
300
px
;
border:
3
px
solid
#73AD21
;
padding:
10
px
;
}
Try it »
When aligning elements like this, it is a good idea to predefine the margins and padding of the element. This helps avoid visible differences across different browsers.
Note: If a child element is taller than its parent and the child is floated, the child will overflow. In this case, you can use the "clearfix(clear float)" to solve this problem.
We can add overflow: auto; to the parent element to solve the child overflow problem:
Example
.clearfix
{
overflow:
auto
;
}
Try it »
When using the float property, there is a problem in IE8 and earlier versions. If the !DOCTYPE declaration is omitted, IE8 and earlier versions will add a 17px margin on the right. This seems to be space reserved for the scrollbar. When using the float property, always set the !DOCTYPE declaration:
Example
body
{
margin:
0
;
padding:
0
;
}
.right
{
float:
right
;
width:
300
px
;
background-color:
#b0e0e6
;
}
Try it »
Vertical Center Alignment - Using Padding
There are many ways to achieve vertical center alignment in CSS. A simple approach is to use padding at the top:
I am vertically centered.
Example
.center
{
padding:
70
px
0
;
border:
3
px
solid
green
;
}
Try it »
To center both horizontally and vertically, you can use padding and text-align: center:
I am horizontally and vertically centered.
Example
.center
{
padding:
70
px
0
;
border:
3
px
solid
green
;
text-align:
center
;
}
Try it »
Vertical Centering - Using line-height
I am vertically centered.
Example
.center
{
line-height:
200
px
;
height:
200
px
;
border:
3
px
solid
green
;
text-align:
center
;
}
/*
If the text has multiple lines, add the following code:
*/
.center
p
{
line-height:
1.5
;
display:
inline-block
;
vertical-align:
middle
;
}
Try it »
Vertical Centering - Using Position and Transform
In addition to using the padding and line-height properties, we can also use the transform property to set vertical centering:
Example
.center
{
height:
200
px
;
position:
relative
;
border:
3
px
solid
green
;
}
.center
p
{
margin:
0
;
position:
absolute
;
top:
50
%
;
left:
50
%
;
transform:
translate
(-
50
%
, -
50
%
)
;
}
Try it »
Tip: For more on the transform property, see the 2D Transform chapter。