---
name: css-flexbox-layout-optimizer
description: "Delivers a mobile-first Flexbox column grid that holds the column count you choose on wide screens and collapses cleanly on narrow ones, with an optional float fallback for legacy browsers. Use when a responsive multi-column grid needs mobile-first Flexbox CSS with a chosen column count, gap, alignment, and an optional float fallback. Do not use for single-row component layouts, CSS Grid layouts, or fixing an existing grid that breaks. Related topics: coding, css."
---

# CSS Flexbox Layout Optimizer

Delivers a mobile-first Flexbox column grid that holds the column count you choose on wide screens and collapses cleanly on narrow ones, with an optional float fallback for legacy browsers.

## When to use

Use this skill when a responsive multi-column grid needs mobile-first Flexbox CSS with a chosen column count, gap, alignment, and an optional float fallback. Do not use it for single-row component layouts, CSS Grid layouts, or fixing an existing grid that breaks.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Write the mobile-first base rule (single column) and the media queries that step the column count up to the chosen number across the container range.
3. Compute flex-basis from the column count and gap with calc so gutters stay constant, and apply the alignment mode to its correct axis.
4. Handle the wrap setting: add a min-width on items when nowrap is chosen and state what overflows.
5. Add the float fallback behind @supports not (display: flex) with a clearfix when requested, matching the column widths.
6. Comment every rule and build the settings table, noting any value chosen on the user's behalf.

## Inputs

- **Maximum container width (px)** (number from 320 to 1920)
- **Number of columns at full width** (choose one: 1, 2, 3, 4, 5, 6)
- **Gap size** (free text, e.g. "1rem or 16px")
- **Alignment mode** (choose one: stretch, center, start, end, space-between, space-around)
- **Wrap behaviour** (choose one: wrap, nowrap)
- **Include float-based fallback** (choose one: Yes, No)

## Output

A fenced css block with the commented, mobile-first grid rules (plus the fallback when requested), followed by a Markdown table listing each setting and the value used.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### CSS Flexbox Layout Optimizer

Produce a production-ready responsive column grid built on Flexbox.

- Maximum container width (px): {{Maximum container width (px)}}
- Number of columns at full width: {{Number of columns at full width}}
- Gap size: {{Gap size}}
- Alignment mode: {{Alignment mode}}
- Wrap behaviour: {{Wrap behaviour}}
- Include float-based fallback: {{Include float-based fallback}}

Write the CSS mobile-first: a single column by default, then media queries that step up the column count at sensible widths until the full count is reached at the maximum container width. Size columns with flex-basis computed from the column count and the gap (calc with the gap subtracted) rather than fixed widths, so the grid keeps its gutters when the container resizes. Use the gap property for gutters and note the minimum browser versions it needs on flex containers. Apply the alignment mode to the axis it belongs to and say which one. When wrap is off, add a min-width on items so they do not collapse below readable width, and say what overflows instead. When the fallback is requested, add a float-based version behind @supports not (display: flex) with a clearfix, kept to the same column widths. Comment each rule with what it does. After the code, give a table with one row per setting and the value used, plus any value you had to choose on the user's behalf.

## Pitfalls

- Using percentage widths without subtracting the gap, so the last column wraps on wide screens.
- Writing desktop-first media queries, which makes the mobile layout the exception and inflates the stylesheet.
- Shipping a float fallback without @supports, so modern browsers apply both and columns double up.
