Centering a div on a page is easy. If you don’t know how to do it, a quick google search will turn up tons or answers for it.
Today, I had to stop and think for a few minutes. I wanted to center a div on a page, but I didn’t know the exact width that the div needed to be. This proposed a slight problem to me because the contents of my div happened to not fill up the entire div. So if I just centered the div on the page using margin:auto, there would be a huge whitespace off to the right side.
I guess I could have accomplished this in javascript, but since I wanted to do it in CSS, I did, and thought I would share.
You first need to set a container div to 100%.
<div style=”width:100%”>
<div style=”float:left; width:auto; position:relative; left:50%;”>
<div style=”float:left; position:relative; left:-50%;”>
<div style=”width:100%”><div style=”float:left; width:auto; position:relative; left:50%;”><div style=”float:left; position:relative; left:-50%;”>Content Here</div></div></div>