Centering an Absolutely Positioned Element

suggest change

For 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents