I'm trying to design a responsive website layout and I need to center a div exactly in the middle of the page (both vertically and horizontally). I've tried using margin and position, but it doesn't work as expected across all screen sizes.
1 Answer
You can use these for that;
HTML
<div class="parent">
<div class="child"></div>
</div>
CSS
/*FLEX*/
.parent {
display: flex;
justify-content: center;
align-items: center;
}
// OR
/*POSITION-RELATIVE-ABSOLUTE*/
.parent {
position: relative;
}
.parent .child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}