To horizontally center a block element (like <div>
), use margin: auto;
.
Setting a width
ensures the element doesn't stretch to full width, and the space on both sides is equally distributed.
div
is horizontally centered!
To just center the text inside an element, use text-align: center;
.
To center an image, set the left and right margins to auto
and make it a block element using display: block;
.
One method for aligning elements is to use position: absolute;
.