Centering a Div with Flexbox
Flexbox makes it simple to center a div horizontally and vertically by aligning items along both the main and cross axes.
Set the parent container's display to flex.
Use justify-content: center to center items along the main axis (horizontal by default).
Use align-items: center to center items along the cross axis (vertical by default).
Optionally, use height on the container to ensure vertical centering works if the container has limited height.
In this example, the .box div is perfectly centered both horizontally and vertically within the .container using Flexbox properties.
How would you center a login card inside a full-height page using Flexbox?
What happens if you forget to set height: 100% on the parent container when trying to center vertically?
A modal popup is centered on desktop but shifts off-screen on mobile — what could be breaking the Flexbox centering, and how would you debug it?
Your team’s design system uses Flexbox to center buttons in card headers, but sometimes they’re misaligned after a font size change — why might that happen?
You’re building a responsive dashboard layout where components must stay centered regardless of viewport size or content height — how would you design this to avoid layout thrashing or overflow issues?
How would you handle centering a div that contains both text and an image with different aspect ratios, ensuring visual balance across devices without hardcoding dimensions?
Your company is migrating from a legacy absolute-positioning centering system to Flexbox — what cross-team coordination, testing, and fallback strategies would you propose to avoid breaking hundreds of existing components?
When designing a global UI component library, how do you balance the simplicity of Flexbox centering against accessibility, screen reader behavior, and legacy browser support requirements?