You have 4, maybe 5 solutions added to the bottom, as this is a combination of different css from your original and js to set the height:
- Use a table cell and center it vertically
- Use
display: table-cell; vertical-align: middle; display: table-cell; vertical-align: middle; like css for your div - Updating the margin vertex with every change in div height using javascript
- Use css3 flexbox (you need to use extensions for providers so that they do not work in some older browsers)
A simple example using the old flexbox version is chrome - add a wrapper div and give it a style:
#wrapper { display: -webkit-box; -webkit-box-align: center; } #wrapper > div { margin: auto; }
fiddle for this http://jsfiddle.net/gK7YU/
The new flexbox style is also a chrome version, you will need to add prefixes from other suppliers, as well as a version without any prefixes in the final product.
#wrapper { display: -webkit-flex; } #wrapper > div { margin: auto; }
fiddle for this: http://jsfiddle.net/LeHRD/
The scripts contain several more css properties so you can easily see what is happening.
Oh, sorry, you donβt need a div shell, you can just vertically concentrate any content using flexbox ... well, in any case, the solution I offer can be combined with display: table-cell; , so it works in older browsers as well.
You can also use absolute positioning with the specified height jsfiddle.net/N28AU/1
#wrapper { possition:relative } #wrapper > div { position:absolute;top:0;right:0;bottom:0;left:0;margin: auto;}
you can calculate the height from the contained elements and update it via js if you want to avoid negative fields.
xception
source share