The parent div is set to ‘vertical-align: top;’ and the child div is set to ‘vertical-align: middle’ but the child is still aligning to the top. How do I make the child align to the middle relative to the height of the parent. I don’t know the specific height of the elements, so I can’t move them based on their height.
#parent {
vertical-align: top;
display: inline-block;
height: 1em;
width: 20px;
position: relative;
background-color: rgba(100,100,100,0.2);
}
#child {
vertical-align: middle;
display: inline-block;
height: 2em;
width: 5px;
background-color: blue;
}
<div id="parent">
<div id="child">
</div>
</div>
3
Answers
I think Flexbox would be more appropriate. You could declare
align-items
on the parent oralign-self
on the child element.Ensure the parent has enough height to accommodate the child
In your code replace
display: inline-block
withdisplay: flex
in#parent
& also addalign-items: center
that’s all.