I have the following markup:
<div class="container">
<div class="grandparent">
<span>Hello World</span>
<div class="parent">
<div class="child">
<i class="fa fa-star fa-5x"></i>
<span>Good For Yoouuu...</span>
</div>
</div>
</div>
</div>
And this CSS:
.container {
position: relative;
height: 350px;
/*This is unknown, set here for example purposes */
}
.grandparent {
height: 100%;
border: 1px solid black;
}
.parent {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.child {
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child span {
display: block;
}
That doesn't seem to take into account the span
element outside of the .parent
element.
How can I vertically center the child div
in the container, regardless of the competing span
element, without setting an explicit height?
Here is a fiddle: https://jsfiddle.net/15hgwu54/
Edit: I don't think this is a duplicate of the proposed question because that one doesn't take into account elements that compete for space inside the container. The solutions in the linked question would work if the only element were the .parent
div, but since there is also a span
taking up space, those answers don't properly center the .parent
div.