# Flex Switcher

> Items sit in a row until the container gets too narrow, then all stack at once. No media queries.

- Category: [CSS Layouts](https://copy-css.com/category/layouts)
- Tags: switcher, flexbox, responsive, no media query, columns, stack
- Live preview: https://copy-css.com/snippets/flex-switcher
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<div class="switcher">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
</div>
```

## CSS

```css
.switcher {
  --threshold: 30rem; /* below this width, everything stacks */
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

/* Wide: negative basis, so they share a row.
   Narrow: huge basis, so each takes a full row. */
.switcher > * {
  flex-grow: 1;
  flex-basis: calc((var(--threshold) - 100%) * 999);
}
```

## Notes

- This CSS Flex Switcher is 13 lines of pure CSS (0.3 KB) with no JavaScript and no dependencies. It uses Flexbox and custom properties.
- Customize with: `--threshold`.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
