-2

enter image description here

I need the exact CSS snippet to making center position for child div into a parent div.

1 Answers1

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