Horizontally centering block elements in CSS

Filed under: CSS Stylesheets

Quick tip: How does one horizontally center block elements using CSS? Try using:

margin: 0 auto;

Your browser will automatically set the left and right margins to center your block element, such as a div.

Example:

<div style="width:25em; height:5em; margin:0 auto; border:1px solid #CECECE">
Block element horizontally centered within its parent element.
</div>

Block element horizontally centered within its parent element.
divider

Leave a Comment