I have a (horizontally) centered outer div containing two elements of unknown width:
<div style='width:800px; margin:0 auto'>
<div style='float:left'>...</div>
<div style='float:right'>...</div>
</div>
Both floats are top-aligned by default, and are of varying/unknown and different heights. Is there any way to make them vertically centered?
I eventually made the outer div
display: table
and the inner divs
display: table-cell;
vertical-align: middle;
text-align: left/right;
but I’m just curious if there’s a way to do this with the floats.
You can’t do this directly, because floats are aligned to the top:
The exact rules say (emphasis mine):
That said, you can take advantage of rule #4:
display: inline-block.vertical-alignto align these wrapper vertically.Be aware that some space might appear between the inline-block wrappers. See How to remove the space between inline-block elements? to fix it.