CSS Flexbox Generator

Master Flexbox layouts with visual controls

Container Properties
Live Preview
1
2
3
4
5
display: flex; ...

The Ultimate Guide to CSS Flexbox: Layouts Made Easy

Introduction: Goodbye Floats

For more than a decade, web developers struggled with layout. We used `

` layouts in the 90s, then we moved to `float: left` with messy `clearfix` hacks in the 2000s, and then `display: inline-block` which suffered from weird whitespace issues. All of these were hacks—misusing properties meant for text or images to create page structures.

Enter Flexbox (Flexible Box Module). Introduced in 2009 and stable by 2015, it is a one-dimensional layout method specifically designed for user interfaces. It excels at distributing space between items and aligning them, even when their size is unknown or dynamic. Today, it is the backbone of almost every modern website, often working in tandem with CSS Grid.

The Two Axes

To master Flexbox, you must understand its geometry. Unlike block layouts (vertical) or inline layouts (horizontal), Flexbox follows axes that can change:

  • Main Axis: The direction items flow. This is defined by `flex-direction`. If direction is `row` (default), main axis is horizontal (Left to Right). If `column`, main axis is vertical (Top to Bottom).
  • Cross Axis: Perpendicular to the main axis. If main is `row`, cross is vertical. If main is `column`, cross is horizontal.

Parent Properties (The Container)

The magic happens on the parent container when you set `display: flex;`.

  • flex-direction: Defines the main axis. `row` is standard. `column` is great for mobile views. `row-reverse` swaps the order visually without changing the DOM.
  • justify-content: Aligns items along the Main Axis. Used to push items to start, end, or space them apart evenly (`space-between`).
  • align-items: Aligns items along the Cross Axis. This is how you finally vertically center things easily!
  • flex-wrap: By default (`nowrap`), flexbox forces items to stay on one line, shrinking them if necessary. Set this to `wrap` to let items flow to new lines.
  • gap: The newest superpower. Instead of using `margin-right: 10px` on every item (and removing it from the last one), just set `gap: 10px`. It works between rows and columns automatically.

Child Properties (The Items)

You can also control individual items within the flex container:

  • flex-grow: A unitless number (default 0). If set to 1, the item will grow to fill remaining space. If multiple items have `flex-grow: 1`, they share the space equally.
  • flex-shrink: A number (default 1). If set to 0, the item will strictly refuse to shrink, even if it overflows the container. Critical for preventing icons or logos from getting squashed.
  • align-self: Overrides the parent's `align-items` for this specific child. Great for making one specific button sit at the bottom of a card while others are at the top.

Real-World Use Cases

1. Perfect Centering (The Holy Grail)

For years, centering distinct elements vertically was a nightmare. With Flexbox, it's three lines:

display: flex; justify-content: center; align-items: center;

2. Navigation Bars

Space out your logo and links instantly. Set the container to `display: flex; justify-content: space-between; align-items: center;`. The logo sticks to the left, links to the right, and everything is vertically centered.

3. The Sticky Footer

Set your body to `display: flex; flex-direction: column; min-height: 100vh;`. Then set your main content area to `flex-grow: 1`. This forces the footer to the bottom of the viewport even if the page has little content!

Frequently Asked Questions (FAQ)

Flexbox vs CSS Grid?

Flexbox is 1D (Row OR Column). Grid is 2D (Rows AND Columns). Use Flexbox for smaller components (navbars, card internals, buttons). Use Grid for the main page layout or complex galleries.

Does gap work in Flexbox?

Yes! It used to be Grid-only, but all modern browsers (Chrome 84+, Safari 14.1+) now support `gap` in Flexbox. It effectively replaces the need for "lobotomized owl" margin hacks.

Why are my items squished?

By default, `flex-wrap` is `nowrap`. If the total width of items exceeds the container, Flexbox shrinks them to fit. To fix, set `flex-wrap: wrap` to allow wrapping, or set `flex-shrink: 0` on specific items.

How do I push one item to the far right?

Use `margin-left: auto` on that item. In a flex container, auto margins absorb all available free space, effectively pushing the item to the end of the axis. This is a classic trick for navbars.

What is basics vs grow/shrink?

`flex-basis` is the ideal size before distributing space (like `width`). `grow` adds space, `shrink` removes space. The shorthand `flex: 1` actually sets `grow: 1, shrink: 1, basis: 0%`.

Is it supported in mobile browsers?

Yes, 100%. Flexbox has been safe to use for over 5-7 years. You rarely even need vendor prefixes like `-webkit-` anymore unless you are supporting extremely ancient devices (like Android kitkat).

Why isn't align-items working?

The parent container needs a defined `height`. If the container is only as tall as its content, "centering" vertically doesn't look like anything. Add `height: 100vh` or a fixed height to see the effect.