Koen’s answer doesn’t exactly center the element.
The proper way is to use CSS3 transform property, although it’s not supported in some old browsers. We don’t even need to set a fixed or relative width.
.centered {
position: fixed;
left: 50%;
transform: translate(-50%, 0);
}
.almost-centered {
background-color: #eee;
position: fixed;
width: 40%;
text-align: center;
top: 5%;
left: 50%;
padding: 20px;
margin-left: -20%;
}
.centered {
background-color: #eee;
position: fixed;
width: 40%;
text-align: center;
top: 25%;
left: 50%;
padding: 20px;
transform: translate(-50%, 0);
}
<div class="almost-centered">
I'm almost centered DIV lorem ipmsum
</div>
<div class="centered">
I'm exactly centered DIV using CSS3
</div>