by Eric Rowell (cofounder of www.adollo.com)
I was working on Adollo today and realized that I was having problems centering a div element inside of another div where it was necessary to use the ”float:left” attribute, and having it look the same in IE7 and FireFox. What a hassle! After about half an hour of trying different things, I finally realized that I’d have to turn to Google, which wasn’t very much help either. I even got out a web design book and starting reading about CSS! If any of you have had this same problem, I thought I’d share the solution with you.
The problem with centering a div element is that you can only work with percents, and fixed values, and not a mix of both. For example, if you wanted to center your web page which is inside a div tag of width 700 pixels, you would want to find the middle of the screen, and then position the div element to the left by 350 pixels. Theoretically, it would look something like this:
margin-left: 50% – 350px;
Unfortunately, this isn’t possible with traditional CSS layout methods! Luckily, there’s a slight-of-hand work around for this. If we use absolute positioning, we can use the “left” attribute, and the margin-left attribute to represent both components of the theoretical margin-left. This can be coded as follows:
position: absolute;
left: 50%;
margin-left: -350px;
And you’re done! Hope this is helpful to someone!