I have a div that has a width the entire width of the page. Inside it is another div.
How can I center that child div inside it's parent div vertically so that when the page is re-sized and the parent div grows, the child div stays in the center?
I have a div that has a width the entire width of the page. Inside it is another div.
How can I center that child div inside it's parent div vertically so that when the page is re-sized and the parent div grows, the child div stays in the center?
Give the value in percent. Set the left and top to 50% this moves the top left corner of the div to the center of the parent. Then minus half the height and width in the margin which makes the center of the div in the center (eg. if the height and width were both 50px then minus 25px).For example
css
:
#main{
height: 100vh;
width: 100%;
}
#secondary{
width: 50px;
height: 50px;
top : 50%;
left: 50%;
margin-left: -25px;
margin-top: -25px;
}
Check out my js fiddle