I'm trying to center two divs with dynamic heights inside a dynamic wrapper... I've been playing with all sorts of tricks to try and get this working cross-browser to no avail, does anyone have a suggestion? See graphic for explanation! The purple block is another block on content I want to sit 20px below the dynamic wrapper.
- NOTE: For the note to the left of the red box, I meant to say "It should scale to be as tall as whichever contained div (green or red) is taller.
- I also do not know whether the red or green box will be taller - just that the green box's height is dynamic and the red's height is fixed.
So I've had a quick poke at your question, it's not a very big jQuery solution, in fact it's so simple that even i could do it !
What i did was worked out which div is bigger dynamically (i.e. is red or green bigger), we then ignore the bigger div and work out the correct vertical margin for the smaller div.
View this example for a better understanding: http://jsfiddle.net/6fN48/
CSS
jQuery
HTML
I hope this helps, the only other way is to use css positioning with a known height, which obviously is not dynamic. :)
Use
display: inline-block
+vertical-align: middle
on the blocks, so they would be aligned just like you want them to.Look at this example: http://jsfiddle.net/kizu/Tky8T/
Even more: the height of the red div can be dynamic too!
If you make the wrapper
div
to beposititon: relative
. Then have the greendiv
to beposition: absolute
, you can make it vertically centered. Here is an example: http://jsfiddle.net/56Ase/Why don't use flex?