CSS Tutorial
To horizontally center a block element (like <div>), use margin: auto;
By adjusting the element’s width, you can stop it from extending past the boundaries of its container.
After that, the element will occupy the designated width, with the remaining space being divided equally between the two margins:
.center {
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
Note: If the width property is left at 100% or is not specified at all, center alignment has no effect.
To just center the text inside an element, use text-align: center;
.center {
text-align: center;
border: 3px solid green;
}
Advice: Refer to the CSS Text chapter for additional examples of text alignment.
To center an image, make it a block element and set the left and right margins to auto:
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 40%;
}
Advice: Refer to the CSS Text chapter for additional examples of text alignment.
One method for aligning elements is to use position: absolute;:
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
Note: components that are positioned absolute are separated from the regular flow and may overlap other components.
Using the float property is another way to align elements:
.right {
float: right;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
Note: An element will overflow outside of its container if it is taller than the element that contains it and it is floated. This can be fixed with the “clearfix hack” (see example below).
Then, to address this issue, we can append the clearfix hack to the contained element:
.clearfix::after {
content: "";
clear: both;
display: table;
}
In CSS, there are numerous methods for vertical element centering. Using padding at the top and bottom is an easy fix:
.center {
padding: 70px 0;
border: 3px solid green;
}
To center both vertically and horizontally, use padding and text-align: center:
.center {
padding: 70px 0;
border: 3px solid green;
text-align: center;
}
Using the line-height property and setting its value to match the height property is another trick:
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* If the text has multiple lines, add the following: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
Using positioning and the transform property is an additional alternative if padding and line-height aren’t available:
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Advice: Our 2D Transforms Chapter contains more information about the transform property.
Flexbox is another tool for centering objects. Just be aware that Internet Explorer 10 and previous versions do not support flexbox:
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid green;
}
Tip: Check out our CSS Flexbox Chapter for additional information on Flexbox.
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.