center aligning a fixed position div

Koen’s answer doesn’t exactly centers the element.

The proper way is to use CCS3 transform property. Although it’s not supported in some old browsers. And we don’t even need to set a fixed or relative width.

.centered {
    position: fixed;
    left: 50%;
    transform: translate(-50%, 0);
}

Working jsfiddle comparison here.

Leave a Comment