My situation is this: I got a pagewrap with in it three div’s. A left div, a main div and a right div. The left one is about 200px widt and must be on the left :geek: the right one must be on the right :geek: and as you expected the main div must become in the middle of them. My question is: how can you center that main div? Because it will float left automatically. Here is a sample code to show what I want.
P.S. To be one step before some critics: I don’t use inline style-elements normally, but just for now to test and post this example I used them ;)
how about this for an answer, got it from ‘CSS Cookbook’
CSS to get the effect is:
float: right; /*floated to the right of the left column (but will still position to the left of the rightmost column /*
So basically this method works by enclosing the left and maincontent divs in an additional div which is then floated to the left.
hope that helps.
Considering you’d only know the width of the sidebars, you could float the divs (left and right) above the main content-div. This, of course, wouldn’t be good for SEO nor accessibilty as you’d have to heap through the sidebars before the content.
Right after the body-start:
This is above the #page-wrap
This is below the #page-wrap
…then the CSS:
In this example you only give the #main margins, and the sidebars gets fixed widths. If you’d use this I’d recommend using overflow: hidden; on the sidebars, to not interrupt with the main content area.
As i understand it, you are creating this 3 column display.
the left and right column are fixed and the maincontent div is variable.
If that is correct then why not simply use the max / min-width selectors in the maincontent div.
This should also work if you want to set the widths to either em, px etc.
see here for further explanation: http://reference.sitepoint.com/css/max-width
The reason why your centre column is not centering is due to the fact that there are 2 major errors in your code that you need to fix first.
1. You have a div with ID of ‘page-wrap’ that contains floated elements.
You have given all your ‘columns’ a fixed width but not the containing div. One of the CSS rules states that when a element contains floated, in needs to have a fixed width (or em based). What is happening is your right ‘column’ is floated right but has no boundaries as to what point it will float. The wider your browser is made the further that ‘column’ floats.
So therefore, add a 800px width to div ‘page-wrap’ and give it the boundary it needs. (200px + 400px + 200px = 800px)
That will now but all three ‘column’ right against each other. That takes care of one problem.
2. The next problem in your code is pertaining to the above mentioned containing ‘page-wrap’ div.
As it contains floated elements, you not only need to give it a fixed with like we have done, but, you also need to take care of itÂ´s position in the ‘flow’ of the document. Your three ‘columns’ are floated and are taken out of the ‘flow’ of the document. The problem is this: ‘page-wrap’ has no float but contains floated elements and therefore does not ‘wrap’ the floated elements it contains. In order to do this, add ‘position: relative’ to page-wrap’ or float it too. One or the other would do but I would use the first mentioned property. The reason why i would use ‘position: relative’ is that if you wanted to place a ‘footer’ div below that needs to ‘clear’ all your ‘columns’, it would clear the containing ‘page-wrap’ div too, witch is not what you want.
Hope that helped you and did not put you to sleep.
You must be logged in to reply to this topic.