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.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsGive 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
- 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.
Rank #2
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.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11If 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.
Rank #4
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.
Recommended Free Tools
Quick Recap
Best Value
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.




