** SOLUCIÓN RESPONSABLE **
Asumiendo el elemento en el div, es otro div ...
esta solución funciona bien
<div class="container">
<div class="center"></div>
</div>
el contenedor puede ser de cualquier tamaño (debe ser relativo a la posición)
.container {
position: relative;/*important*/
width: 200px;/*any width*/
height: 200px;/*any height*/
background: red;
}
El elemento ( div ) también puede ser de cualquier tamaño (debe ser más pequeño que el contenedor )
.center {
position: absolute;/*important*/
top: 50%;/*position Y halfway in*/
left: 50%;/*position X halfway in*/
transform: translate(-50%,-50%);/*move it halfway back(x,y)*/
width: 100px;/*any width*/
height: 100px;/*any height*/
background: blue;
}
El resultado se verá así. Ejecute el fragmento de código
.container {
position: relative;
width: 200px;
height: 200px;
background: red;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
</div>
Lo encontré muy útil