Thursday, January 14, 2010

Center div with margin: 0 auto - a one liner

The best and cleanest way to center a div is to use the margin: 0 auto attributes in styles. Make sure however that you set a width of the div to something specific otherwise it's not going to work.

Note: it seems that this unfortunately doesn't work in IE7. Alternatively you may use the <center> tag.

See an example of the style attribute below:
(click in the box to highlight the code for copy)

No comments:

Post a Comment

Dear legitimate commenters, please post your comments below. I'd be happy to receive your feedback; helps me a lot improving, or getting reassurance that I'm doing something really great here.

IMPORTANT! A note to commenters planning on advertising their business in comments: please be informed that I'm NOT going to post your comments but will ALWAYS mark them as SPAM.