How do I center a div?
Ask QuestionAsked 3 days agoViewed 2.8k times
The eternal question. What is the modern, reliable way to center a block both horizontally and vertically?
SA
4 Answers
Absolute + translate(-50%,-50%) when you must position over other content.
LO- ✓✓ Accepted answer
Flexbox: display:flex; justify-content:center; align-items:center; on the parent does it cleanly.
PN Grid one-liner: display:grid; place-items:center; — even shorter than the flexbox version.
MWFor a fixed-width block, margin-inline:auto still works for horizontal centering.
SA