I need the exact CSS snippet to making center position for child div into a parent div.
Asked
Active
Viewed 49 times
-2
-
I wrote its answer to : http://stackoverflow.com/questions/618097/how-do-you-easily-horizontally-center-a-div-using-css/30109735#30109735 – Mohammad Shahnewaz Sarker May 08 '15 at 08:59
1 Answers
2
You can do it like this, without the need to know the dimensions of either div:
<div class="container">
<div class="centered">
[...]
</div>
</div>
CSS:
.container {
position: relative;
}
.centered {
position: absolute;
// Center upper left corner
// Percentage is relative to width/height of parent container
top: 50%;
left: 50%;
// Move centered element so its center is centered
// Percentage is relative to width/height of centered element
transform: translate(-50%, -50%);
}

Jost
- 5,948
- 8
- 42
- 72