日本語

How to Center an Element on the Screen with CSS

​​CSS​HTML,Playwright

To center a single element both vertically and horizontally on the screen, such as a dialog or a notice, set the parent element to display: grid, add place-items: center, and stretch the parent to the full screen height with min-height: 100dvh. If you want the element to stay in the same place while scrolling, fix it to the screen with position: fixed and then center it. If you center an element with flexbox inside a full-screen overlay, the top edge can get cut off when the content is taller than the screen. I also show how to fix that.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page with only HTML and CSS in Playwright, changed the screen size, and checked the element positions with getBoundingClientRect(). I did not test Firefox, Safari, or real smartphones. In headless Chromium, scrollbars did not take up any width. I did not check the positions in environments where scrollbars take up width.

A light blue box labeled "the box in the middle" (the label is in Japanese) sits inside a white frame. With the mouse, the lower-right corner of the frame is dragged to resize it three times: wider, taller, and then a short, wide shape. At every size, the box stays in the center of the frame. Below the frame, the numbers for the space above and below the box and the space to its left and right are always equal. Only at the moment the corner is released do the numbers turn pink.
The box stays in the center through three screen size changes. The white frame stands in for the screen, and a script changes its size. The mouse icon and the black square at the corner were added by hand for the GIF.

Center with place-items: center in a grid

In the HTML, you only need to put the element you want to center inside body.

html
<body class="center-grid">
  <div class="box">真ん中に置く要素</div>
</body>
css
/* Remove the default margin (8px) that browsers add to body */
body {
  margin: 0;
}

/* Use a grid to center the child element on the screen */
.center-grid {
  display: grid;
  place-items: center; /* Center both vertically and horizontally */
  min-height: 100vh; /* For browsers that do not support dvh */
  min-height: 100dvh; /* Stretch to the full screen height */
}

place-items is a shorthand for align-items (vertical) and justify-items (horizontal). If you give it only one value, that value is applied to both. In Chromium, the computed values of align-items and justify-items were both center.

I placed a 300px wide by 200px tall box and checked its position at different screen sizes.

Screen size Top-left position of the box Left and right space Top and bottom space
800×600 (250, 200) 250px each 200px each
375×667 (37.5, 233.5) 37.5px each 233.5px each

At both sizes, the left and right space were equal, and the top and bottom space were equal. As in the GIF above, the box stays in the center even while the screen size is changing.

If the parent is not as tall as the screen, vertical centering does not work

When I tried only display: grid and place-items: center, without min-height, the box was centered horizontally but stuck to the top edge of the screen (0px) vertically. That is because the height of body was only 200px, the same as the box inside it. To center vertically, the parent must be stretched to the full screen height.

I also tried height: 100% instead of min-height. If you do not set a height on html, the height of body stayed at 200px. A percentage for height is based on the height of the containing block (here, html), so you need to set height: 100% on both html and body. When I set both, the height of body became 600px, and the box was at (250, 200). Using 100vh or 100dvh, which refer directly to the viewport height, is simpler because you only need to write it in one place.

There is also a reason to write body { margin: 0; }. In Chromium, the default margin of body was 8px. If I kept that margin and used min-height: 100vh, the document height became 616px for a screen height of 600px. The extra top and bottom margin let the page scroll vertically, and the box shifted 8px down.

The difference between vh and dvh

The MDN explanation of <length> says that vh is the same as the large viewport height (lvh). This is the largest screen height on a smartphone browser, when the address bar is hidden. It also says that when the address bar is showing, an element sized with 100vh may be partly hidden behind the address bar.

dvh is the dynamic viewport unit. It changes size as the address bar appears and hides. That is why the code above uses min-height: 100dvh, and it places min-height: 100vh on the line before it for browsers that do not support dvh. As the MDN explanation of CSS error handling says, browsers ignore declarations they do not understand. A browser that does not know dvh skips the second declaration and uses the first one, 100vh.

In desktop Chromium with a screen height of 600px, the computed values of 100vh, 100dvh, and 100svh were all 600px. I did not test the differences on smartphone browsers where the address bar appears and hides, because I do not have a real device.

When using flexbox, center both properties

You can center an element the same way with flexbox.

css
/* Use flexbox to center the child element on the screen */
.center-flex {
  display: flex;
  justify-content: center; /* Center along the main axis (horizontal) */
  align-items: center; /* Center along the cross axis (vertical) */
  min-height: 100vh;
  min-height: 100dvh;
}

In flexbox, justify-content sets the alignment along the main axis, which is the direction items are laid out in. align-items sets the alignment along the cross axis, which is perpendicular to it. If you do not write flex-direction, the default is row, which lays items out horizontally. (The computed value in Chromium was also row.) So justify-content controls horizontal alignment and align-items controls vertical alignment.

With the same box, the position at 800×600 and 375×667 matched the grid version.

For an element that must stay in place while scrolling, use position: fixed

For an element like a dialog or a notice that should stay in the center of the screen while the page scrolls, fix it to the screen with position: fixed.

html
<div class="box fixed-center">真ん中に固定する要素</div>
css
/* Centered element that stays in place while scrolling */
.fixed-center {
  position: fixed;
  top: 50%;
  left: 50%;
  translate: -50% -50%; /* Move back up and left by half of its own width and height */
}

top: 50% and left: 50% place the top-left corner of the element at the center of the screen. Without more, the element would be offset from the center toward the bottom-right. On an 800×600 screen without translate, the top-left corner of the 300px by 200px box was at (400, 300), so it extended from the center toward the bottom-right.

So I used translate to move the element back up and left. As the MDN explanation of translate() says, a percentage value is based on the element's own width for the horizontal axis and its own height for the vertical axis. With -50% -50%, the element moves back by exactly half its size, even if you do not know its size in advance.

I tested it on a page 3000px tall. The top-left position of the box was (250, 200) at 800×600 and (37.5, 233.5) at 375×667. The position did not change after scrolling 1200px down.

If the content is taller than the screen, the top edge may be cut off

When you center a dialog inside a full-screen overlay (such as a modal background), stretch the overlay to fill the screen with position: fixed and inset: 0, and then center the content inside it.

css
/* Center a dialog inside a full-screen overlay (such as a modal background) */
.overlay {
  position: fixed;
  inset: 0; /* Set top, right, bottom, and left all to 0 */
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: auto; /* Allow scrolling when the content is taller than the screen */
}

While the content is small, it is centered as described above. The problem happens when the content becomes taller than the screen. On a 600px tall screen, I placed a 900px tall box inside this overlay, and the top edge of the box was at -150px. The box extends 150px above and below the center. You can scroll to see the part that extends below, but even when scrolled all the way to the top, the top edge of the box stayed at -150px. The top 150px could not be seen at all.

Two screens stacked vertically. In the top screen, with "align-items: center", the heading of the dialog is cut off at the top edge of the screen and cannot be read. In the bottom screen, with "align-items: safe center", the heading of the dialog can be read. The label in the image is the code string, not Japanese text.
A dialog taller than the screen, shown when scrolled all the way to the top. Top: align-items: center. Bottom: align-items: safe center

To prevent this, add safe before center.

css
/* The top edge is not cut off even when the content is taller than the screen */
.overlay-safe {
  align-items: safe center;
}

The MDN explanation of align-items says that with safe, when an element overflows and part of it would be hidden, the element is placed as if it were aligned to start (the top edge). When I applied both .overlay and .overlay-safe and tested again, the top edge of the box was at 0px, and I could scroll 300px down to see the bottom edge. When the box was 200px tall, the box was in the center (its top edge at 200px) even with safe.

Below are the results for a 900px tall box in a 600px tall screen, for each way of writing the code. The top edge of the box is its position after scrolling all the way to the top.

How it is written Top edge of the box Is it cut off?
.overlay (flexbox with align-items: center) -150px Yes
.overlay with .overlay-safe added (align-items: safe center) 0px No
.overlay without align-items or justify-content, and margin: auto on the inner box 0px No
Overlay written with display: grid and place-items: center 0px No (the grid row grows to 900px to fit the box. The computed value of grid-template-rows is 900px.)
.center-grid or .center-flex (written with min-height) 0px No (body grows to 900px to fit the box)
body as flexbox centered, with height: 100dvh -150px Yes

The top edge was cut off only when I used flexbox and set the height of the parent to the screen height. It was not cut off when the parent or the grid row grew to fit the content, or when I used safe or margin: auto.

​Posted by 86