Centering an Absolutely Positioned Element
suggest changeFor an overlay or another deliberately positioned element, automatic margins can center it within its containing block:
.parent {
position: relative;
min-block-size: 20rem;
}
.centered-overlay {
position: absolute;
inset: 0;
margin: auto;
inline-size: fit-content;
block-size: fit-content;
}
inset: 0 sets all four inset properties. The automatic margins absorb the remaining space around the element.
Use Grid or Flexbox for ordinary in-flow content. Absolute positioning removes the element from normal flow and can cause overlap when content size, zoom level, or translations change.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents