DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

Center a Div with Flexbox: Horizontal and Vertical CSS Alignment

Use three CSS rules on the parent to center a child with Flexbox, and give the parent enough space for vertical centering to show.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To center a child element horizontally and vertically with Flexbox, apply display: flex, justify-content: center, and align-items: center to its parent. In the default row direction, these center items on the horizontal and vertical axes, respectively.

Use Flexbox to center the child

Flexbox alignment is set on the parent container, not on the child you want to move. For a typical horizontal row layout, use:

As an Amazon Associate I earn from qualifying purchases.

.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Place the element to be centered inside an element with the container class. display: flex makes that parent a flex container; justify-content centers its items along the main axis, and align-items centers them along the cross axis. MDN’s centering example and guide to aligning items in a flex container demonstrate this approach.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Give the parent space for vertical centering

Centering happens within the space available inside the parent. If the parent is only as tall as its contents, there may be no extra vertical room for the child to move, so the result may not look vertically centered in a larger area.

#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

For example, to center within at least the viewport height, add a minimum height to the container:

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

min-height: 100vh is appropriate when the intended area is at least as tall as the viewport. For other layouts, give the parent the height or available space that suits the design. MDN’s centering example uses an explicit container height to make the vertical alignment visible.

How Flexbox axes affect alignment

The alignment properties refer to Flexbox’s main and cross axes, not permanently to horizontal and vertical directions. In the default flex-direction: row, for a top-to-bottom writing mode, the main axis runs horizontally and the cross axis vertically. With flex-direction: column, those directions swap: justify-content acts vertically and align-items horizontally. MDN explains this in its guides to Flexbox’s basic concepts and box alignment in Flexbox.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If you change the direction and the child no longer appears centered on the screen’s horizontal and vertical axes, check which axis each property now controls. The properties still center along the main and cross axes; the axes have changed direction.

Center one item differently from the group

Use align-self: center on an individual child when it should have different cross-axis alignment from the other items in its flex container. Main-axis distribution is handled for the flex items as a group; justify-self is not the way to position one flex item independently on that axis. See MDN’s flex alignment guide.

Avoid the justify-items mistake

justify-items: center does not center an item in Flexbox. MDN notes that justify-items is ignored for flex items. For the main axis, use justify-content: center; for the cross axis, use align-items: center. The MDN Flexbox learning guide covers the distinction.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When Grid is a better fit

If you are choosing a layout method rather than specifically using Flexbox, CSS Grid offers another concise way to center an item: MDN’s centering guide demonstrates display: grid with place-items: center. For a Flexbox layout, keep the parent’s justify-content and align-items rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.