I am trying to get blue container in the middle of pink one, however seems vertical-align: middle;
doesn't do the job in that case.
<div style="display: block; position: absolute; left: 50px; top: 50px;">
<div style="text-align: left; position: absolute;height: 56px;vertical-align: middle;background-color: pink;">
<div style="background-color: lightblue;">test</div>
</div>
</div>
Result:
Expectation:
Please suggest how can I achieve that.
You may use
display:table
/table-cell;
Here is simple way using Top object.
eg: If absolute element size is 60px.
use this :
refer this link: https://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/
Please check this answer to a similar issue.
This is by far the easiest way I have found.
https://stackoverflow.com/a/26079837/4593442
NOTE. I used display: -webkit-flex; instead of display: flex; for testing inside safari.
FOOTNOTE. I am a novice only, sharing help from someone who is clearly experienced.
You can do it by using table and table-cell
test
First of all note that
vertical-align
is only applicable to table cells and inline-level elements.There are couple of ways to achieve vertical alignments which may or may not meet your needs. However I'll show you two methods from my favorites:
1. Using
transform
andtop
The key point is that a percentage value on
top
is relative to theheight
of the containing block; While a percentage value ontransform
s is relative to the size of the box itself (the bounding box).If you experience font rendering issues (blurry font), the fix is to add
perspective(1px)
to thetransform
declaration so it becomes:It's worth noting that CSS
transform
is supported in IE9+.2. Using
inline-block
(pseudo-)elementsIn this method, we have two sibling
inline-block
elements which are aligned vertically at the middle byvertical-align: middle
declaration.One of them has a
height
of100%
of its parent and the other is our desired element whose we wanted to align it at the middle.Finally, we should use one of the available methods to remove the gap between inline-level elements.