Sometimes, we want to horizontally center a fixed div with CSS.
In this article, we’ll look at how to horizontally center a fixed div with CSS.
How to horizontally center a fixed div with CSS?
To horizontally center a fixed div with CSS, we use translate
.
For instance, we write
div {
//...
left: 50%;
transform: translateX(-50%);
}
to set the left
position to 50%.
Then we make the div centered with transform: translateX(-50%);
.
Conclusion
To horizontally center a fixed div with CSS, we use translate
.