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.
Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • digg
  • del.icio.us
  • Reddit
  • YahooMyWeb
  • scuttle
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • De.lirio.us
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
divider

Leave a Comment