- Set the display of the targeted element to flex and use
align-items: center
for vertical centering andjustify-content: center
for horizontal centering
<div class="wrapper"> <span>I'm vertically/horizontally centered!</span> </div>
html, body, .container { height: 100%; } .container { display: flex; align-items: center; justify-content: center; }
- Use
top: 50%/left: 50%
in combination withtranslateX(-50%) translateY(-50%)
to dynamically vertically/horizontally center the element.
.container { position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); }