views:

109

answers:

6

Hello,

Recently, a client asked that his site be percent-based rather than pixel-based. The percent was to be set to 80%. As you guys know, it is very easy to center the container if it is pixel-based but how do you center a percent-based main container?

#container
{
  width:80%;
  margin:0px auto;
}

That does not center the container :(

A: 

I know it sounds crazy but how about getting the width in jQuery then resetting it so it is a pixel value?

Or basing the width off the page width in jQuery?

It's pretty weird he wants a percent based layout.

Oh and last but not least, two 10% width divs surrounding it ;)

Kyle
.containerLeft { width:10%; float:left;}.container { width:80%; float:left;}.containerRight { width:10%; float:left;}
Kyle
+7  A: 

The margin property supports percentage values:

margin-left: 10%;
margin-right: 10%;
David
I can't belive it, how could I miss such simple thing !! Thanks anyways :)
Sarfraz
+1  A: 
#container
{
  width:80%;
  position:absolute;
  margin-left:-40%;
  left:50%;
}

or simply

#container
{
  width:80%;
  margin-left:10%;
}
oezi
A: 

Remove the px value in the margin, just like this:

#container { width: 80%; margin: 0 auto; }
Adirael
Kyle
It's different in some browsers, that code is working for me under Chrome, Safari and FireFox (all on OSX)This is my test code: http://dl.dropbox.com/u/155873/centering.html
Adirael
A: 

That does not center the container :(

Works perfectly for me.

Make sure you're in Standards Mode etc.

Full non-working code?

bobince
A: 
#container { width: 80%; margin: 0 auto; position relative; }

And the element parent of "container" must be position relative or absolute

for example:

body{position:relative;}
#container { width: 80%; margin: 0 auto; position relative; }
joanballester