Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering a fluid DIV with max-width and absolute positioning

Before someone down votes me for asking yet another centering problem. Please read my situation before judging!

I'm familiar with most common techniques for centering stuff but here is my situation. I have a DIV that must be both vertically and horizontally centered within its parent but it must also be fluid and not exceed the width of 890px.

Max-width achieves the fluid-ness that I want but because absolute positioned elements need width rather than max-width my vertical/horizontal centering breaks. At the moment i'm having to sacrifice fluid-ness over centering (or vice-versa) but I NEED both.

I'd like to keep the content within my centered DIV in view at all times, my current code doesn't do this, it hides the content as the window is made smaller http://jsfiddle.net/cCQ2w/

Can anyone propose a solution that might work for me?

like image 225
egr103 Avatar asked Oct 06 '22 16:10

egr103


2 Answers

I've tried to solve your problem. See this page: http://jsfiddle.net/PGce2/. So it centered horizontally and vertically and it is "fluid" and not exceed the width of 890px.

like image 110
khomyakoshka Avatar answered Oct 12 '22 14:10

khomyakoshka


You can provide a width of 100% with max-width set to 890px.

I have shown [here][1] the sample of a div with both horizontally and vertically align center.

[1]: http://jsfiddle.net/r2qL5sgj/1/
like image 23
devender balodi Avatar answered Oct 12 '22 13:10

devender balodi