Centering elements in CSS has an unusually long history of workarounds compared to most layout problems: negative margins paired with absolute positioning, table-cell display tricks, line-height hacks for single-line text, and transform translate offsets that required JavaScript to maintain. Flexbox replaced all of them with a consistent three-property solution that works reliably at any container size and any child dimension. FixTools generates the exact centering ruleset you need, whether you are centering a single child element inside a hero banner or stacking multiple children vertically in the middle of a modal dialog. The output is copy-ready CSS that drops onto any container element and produces predictable centring behaviour the first time, without trial and error.
Generates the three-property centering ruleset instantly
Handles single-child and multi-child centering separately
Live preview confirms centering at any container size
CSS Tool
All processing happens in your browser — your files are never uploaded to any server.
🚀Open Grid Flexbox Builder100% Free · No account · Works on any device
Before Flexbox arrived as a mainstream layout system, vertical centering in CSS required one of several hacks chosen based on the context of the element being centred. Absolute positioning with top: 50% paired with a negative margin equal to half the element height worked only for elements with fixed, known heights, which limited it to icons and small widgets. The table-cell approach required wrapping elements inside display: table and display: table-cell containers, which polluted the HTML and broke semantic markup for screen readers. The line-height trick worked only for single-line text and broke the moment the content wrapped to a second line. None of these approaches adapted gracefully to dynamic content where the height of the centred element was unknown at design time. Developers frequently had to choose the technique that was least broken for the situation rather than the one that was actually correct, and the lack of a first-class centering mechanism was one of the most commonly cited frustrations with CSS layout for well over a decade.
Flexbox solves both horizontal and vertical centering with exactly three properties applied to the parent container. Setting display: flex activates the Flexbox layout model on that element. Setting justify-content: center distributes children along the main axis, which is horizontal in a default row-direction container. Setting align-items: center positions children along the cross axis, which is vertical in a row-direction container. Together, these three properties centre any child element regardless of its dimensions, with no knowledge of the child size required and no media query overrides for different content lengths. The container does not need a fixed height: the centering works whether the container is 100px tall or 100vh, whether it stretches to fill the viewport or shrinks to wrap a paragraph of text. This works in every modern browser without vendor prefixes and has worked that way since around 2015.
Centering a single child element and centering multiple children stacked vertically require slightly different approaches in Flexbox, though the building blocks are the same. For a single element, the three properties above work directly: the lone child sits precisely in the middle of the container at any size. For multiple children that should stack vertically in the centre, add flex-direction: column to the container. This switches the main axis to vertical, so justify-content: center now centres the stack vertically and align-items: center centres each individual item horizontally within the container. Adding a gap value between items controls spacing in the stack without resorting to margin hacks. This four-property combination is the standard pattern for centred hero sections, modal dialog content, empty state messages, sign-in forms, loading screens, and any vertically stacked content that needs to sit in the middle of its container.
The mental model that makes Flexbox centring stick is the axis swap that happens when flex-direction changes. In row direction, justify-content controls horizontal and align-items controls vertical; in column direction, the two properties swap roles. Internalising this lets you reason about centring without consulting documentation: identify the main axis, pick the appropriate distribution and alignment values for that axis, and the layout falls into place. The generator reinforces the model by labelling each control with its current effect, so you do not have to remember which axis is which while you build. Once the centring works in the preview, you can copy the ruleset and trust it to behave the same way in production, which removes a category of layout bugs that used to consume disproportionate amounts of debugging time.
Open the Flexbox Generator, set justify-content to center and align-items to center. For stacked children, also set flex-direction to column. Click Copy CSS and paste onto your container element.
Step-by-step guide to css flexbox centering:
Select the container element
Identify the parent element that should contain the centered content, such as a section, div, or article that wraps the elements you want to centre. The Flexbox properties go on this container, not on the child being centred, which is the most common mistake when people first encounter Flexbox centring. Make sure the container has an explicit height or min-height before continuing, since vertical centring needs vertical space to work within.
Set justify-content and align-items
In the generator, set justify-content to center and align-items to center. The live preview immediately shows children sitting in the middle of the container both horizontally and vertically. These two property values are the heart of Flexbox centring and they work together by controlling the main axis and cross axis respectively. Confirm the preview matches your expectation before continuing, especially for containers with constrained heights.
Add flex-direction column for stacked children
If you are centering multiple children that should stack vertically rather than sit on one row, set flex-direction to column. The preview updates to show the children stacked and centred both horizontally and vertically. This step is unnecessary when centring a single child element. Adding a gap value at the same time controls spacing between the stacked items without needing to add margin to each individual element, which produces cleaner CSS.
Copy and paste the ruleset
Click Copy CSS to copy the complete container ruleset, including display flex, the alignment values, the flex-direction if you set one, and any gap value. Paste it onto your container element in your stylesheet. The output is plain CSS with no vendor prefixes required for modern browsers, and it slots into any framework or styling solution without modification. Verify the result in your actual page after pasting to confirm everything is centred as expected.
Common situations where this approach makes a real difference:
Hero section with heading and button
A landing page hero contains an H1, a subheading, and a CTA button stacked vertically and centred inside a full-viewport-height section. Setting flex-direction: column with justify-content: center and align-items: center on the hero stacks the three elements vertically and centres the entire stack within the full-height hero container. Adding gap: 24px gives consistent spacing between items without applying margin to individual elements, which keeps the CSS shorter and easier to maintain. The result works at any viewport size from a 320px phone to a 4K monitor without any media query intervention.
Modal dialog content
A confirmation modal needs its icon, headline message, and button row centred inside the dialog box. A column-direction Flexbox container with both justify-content and align-items set to center places all three elements precisely in the middle of the modal at any dialog size. The centring adapts automatically if the message text wraps to multiple lines on a narrower modal width or in a translated language with longer strings. No JavaScript is needed to recalculate positions after a content change, which makes the modal robust against the kinds of dynamic content updates that broke older centring approaches.
Empty state placeholder
A data table that can have zero rows shows an icon and a friendly message when there is no data to display. Wrapping the empty state in a Flexbox container with justify-content: center, align-items: center, and flex-direction: column centres the placeholder at any table height, whether the table is 200px tall on mobile or 600px tall on desktop. The same pattern applies to empty search results, empty inboxes, zero-state dashboards, and any feature that occasionally has nothing to show. The centring is robust against the wide range of container heights a real product encounters.
Loading spinner overlay
A full-screen loading overlay needs its spinner centred both horizontally and vertically while the page fetches data in the background. Setting the overlay to display: flex with justify-content: center and align-items: center centres the spinner at any viewport size on any device, with no fixed positioning maths and no JavaScript-driven repositioning required. The three-property approach works equally well whether the overlay covers the full viewport using position: fixed or covers just a single panel using position: absolute inside a relatively positioned wrapper, which makes it the standard pattern for spinners across modern applications.
Use this when you need to center an element both horizontally and vertically inside its container using Flexbox, without calculating offsets or using positioning hacks.
Get better results with these expert suggestions:
Give the container an explicit height for vertical centering to work
The align-items: center declaration centres items along the cross axis only when the container is taller than the natural content height. If the container has no height set, it shrinks to fit the content exactly and the centring has no visible effect because there is no extra vertical space to distribute. Add min-height: 100vh for full-viewport hero sections, height: 400px for fixed-height components, or another explicit height value to the container before expecting vertical centring to appear in the browser.
Use align-self to override centering on individual children
The align-items: center declaration applies uniformly to every child in the container. To push one specific child to the top or bottom while keeping its siblings centred, add align-self: flex-start or align-self: flex-end to that single child element. The align-self property overrides the parent align-items value for that one element only, without affecting siblings or requiring a separate wrapper. This is the cleanest way to handle the common case of a header element that should sit at the top while the rest of the content stays centred.
Prefer gap over margin for spacing stacked centered items
When centring stacked children inside a column-direction Flexbox container, use the gap property on the container instead of margin-bottom on each child. The gap property does not add space after the last item, which prevents an extra strip of whitespace at the bottom of the container that would visually offset the centred stack and make it look slightly higher than it should. Gap spacing is also cleaner to maintain because adding or removing children does not require any margin adjustments.
Test centering at both small and large container heights
A hero section might be 100vh on desktop but only 300px on mobile, and the same centring properties produce visibly different layouts at those two heights. Check that justify-content: center looks correct at both ends of the range your project supports. At very small container heights, tightly packed centred items can look cluttered and overflow the container, so consider reducing the gap value or hiding non-essential items at narrow breakpoints to keep the centred stack from spilling out.
More use-case guides for the same tool:
Open the full Grid Flexbox Builder — free, no account needed, works on any device.
Open Grid Flexbox Builder →Free · No account needed · Works on any device