How do I vertically align a child element with the parent having a dynamic height.
CSS code:
parent{
height: 400px;
}
.child{
position:relative;
top:50%;
transform: translateY(-50%;);
}
This sets the child once, then doesn't change. How do I change it so that it changes with the dynamic height of the parent?
inner-box, center the image tag horizontally using text-align:center property as it's the inline element. Finally, add vertical-align:middle to the inner div which is pushed to vertically center to its parent element.
Use the margin property to make sure that the <div> element is center aligned according to its parent element.
The child DIV needs to be the same width as the browser viewport. The child DIV must stay as a child of the parent div.
We can set the child element as an in-line element, display: inline-block; , and then set the father element as text-align: center; , so that the child element can be horizontally centered.
You can use display: flex
.
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 1em;
background-color: tomato;
}
.child {
width: 10px;
height: 10px;
background-color: yellow;
}
You can use `displa: flex`.
The following doesn't rely on parent's height.
<div class="parent">
<div class="child">
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With