<p>Two ways to horizontally center blocks that are absolute positioned</p>
<div class="container">
<div class="lightbox-1">
Good when you need old browser support, must know lightbox width
</div>
</div>
<div class="container">
<div class="lightbox-2">
Good when you don't need older browser support whether
width is known or not.
</div>
</div>
.container {
background-color: #CCC;
position: relative;
height: 120px;
margin: 10px;
}
.lightbox-1 {
position: absolute;
width: 400px;
height: 100px;
background-color: red;
top: 10px;
/*
Centering
*/
left: 50%;
margin-left: -200px;
}
.lightbox-2 {
position: absolute;
width: 400px;
height: 100px;
background-color: green;
top: 10px;
/*
Centering
*/
transform: translateX(-50%);
left: 50%;
}
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.