CSS-Flexbox-Generator

So wird es benutzt 3 Schritte
  1. Legen Sie flex-direction, justify-content und align-items fest.
  2. Legen Sie flex-wrap und den Abstand fest; die Vorschau zeigt das Layout sofort.
  3. Drücken Sie Flexbox-CSS kopieren und fügen Sie es in Ihr Stylesheet ein.
Box 1
Box 2
Box 3
Generiertes Flexbox-CSS

So funktioniert dieses Werkzeug

Flexbox ordnet Elemente entlang einer Achse an und richtet sie auf der anderen aus. Welche Achse welche ist, entscheidet allein flex-direction: In einer Zeile verläuft die Hauptachse waagerecht, justify-content verteilt also von links nach rechts und align-items positioniert senkrecht. Bei column tauschen die beiden. Nahezu jeder Moment der Verwirrung mit Flexbox geht auf diesen Tausch zurück.

justify-content verteilt entlang der Hauptachse, align-items positioniert quer dazu, flex-wrap entscheidet, ob nicht passende Elemente in eine neue Zeile rutschen oder gequetscht werden, und gap setzt den Abstand dazwischen. Die Vorschau bekommt im Spaltenmodus eine Höhe, denn in einem Container, der nur so hoch ist wie sein Inhalt, hat align-items nichts zu zeigen.

Welche Eigenschaft was tut

  • Die Achsen tauschen, wenn Sie die Richtung ändern: justify-content wirkt in einer Zeile waagerecht und in einer Spalte senkrecht. Tut eine Eigenschaft nicht mehr, was Sie erwarten, prüfen Sie zuerst die Richtung.
  • gap hat Außenabstände an den Kindern ersetzt: Eine Deklaration am Container statt eines Abstands an jedem Element, ohne führenden oder nachlaufenden Rest, den man wegräumen muss. Deshalb brauchen Flex-Layouts heute weit weniger Nacharbeit.
  • nowrap ist die Voreinstellung, und sie quetscht: Elemente, die nicht passen, werden zusammengedrückt statt umgebrochen. Zerdrückt eine Zeile auf schmalen Bildschirmen ihren Inhalt, ist wrap die Lösung.
  • Die umgekehrten Richtungen ändern die Reihenfolge optisch, nicht im Dokument: Tastatur und Screenreader folgen dem Markup. Ein rein optisches Umkehren lässt beide auseinanderlaufen, was für alle ohne Maus verwirrend ist.

The axes swap, and that is the whole confusion

Flexbox lays items out along one axis and aligns them across the other. Which is which is decided entirely by flex-direction.

In a row, the main axis runs horizontally: justify-content spaces items left to right and align-items positions them vertically. Switch to a column and the two swap over — justify-content now runs down the page and align-items across it.

Nearly every moment of confusion with flexbox traces back to that swap. If a property has stopped doing what you expect, check the direction before anything else. Seeing it happen in the [msm_tool slug="flexbox-generator"] tends to fix it permanently in a way that reading about it does not.

The values worth knowing

justify-content distributes along the main axis. flex-start, center and flex-end are self-explanatory. The three that get confused:

  • space-between — no space at the ends, so the first and last items touch the edges. This is the one you want for a header with a logo on one side and navigation on the other.
  • space-around — each item gets equal space on both sides, so the outer gaps end up half the size of the inner ones. It looks slightly wrong and is rarely what anyone meant.
  • space-evenly — every gap is equal, including the outer ones. Usually what people reach for space-around expecting.

align-items positions across the other axis. stretch is the default and is why a row of cards with different content lengths all end up the same height — useful, and occasionally surprising. center is the one that makes vertical centring trivial, which was famously not the case before flexbox existed.

Centring, finally

justify-content: center and align-items: center together centre on both axes, regardless of direction. That pair is the answer to a problem that consumed an unreasonable amount of collective effort for about fifteen years.

One caveat: the container needs a height for vertical centring to have anywhere to happen. A flex container that is only as tall as its contents centres them in exactly the space they already occupy, which looks like nothing happened.

nowrap is the default, and it squeezes

Items that do not fit are compressed rather than moved to a new line. If a row is crushing its contents on a narrow screen, flex-wrap: wrap is the fix, and it is the single most common omission in flex layouts that break on mobile.

Worth knowing what wrapping changes: once items wrap, align-items applies within each line, and align-content controls how the lines themselves are distributed. That second property only does anything on a wrapped, multi-line container, which is why it appears to be broken when tried on a single line.

gap replaced margins

One declaration on the container instead of a margin on every child, with no leading or trailing space to strip off and no :last-child rule. It is the reason modern flex layouts need far less tidying than they used to, and it is supported everywhere current.

The reverse directions have an accessibility cost

row-reverse and column-reverse change the order items appear in visually. They do not change the order in the document, which is what keyboard navigation and screen readers follow.

Reversing for visual effect leaves those two disagreeing: someone tabbing through the page moves in an order that does not match what they see. The same applies to the order property. Use them where the visual and document order genuinely should differ and you have thought about it — not as a quick way to move something to the right.

flex-grow, flex-shrink and flex-basis

These sit on the children rather than the container, and the shorthand catches people out. flex: 1 means grow to fill, do not resist shrinking, and start from zero — which is why flex: 1 on several items makes them equal width regardless of their content.

If you want items sized by their content but allowed to grow, flex: 1 1 auto is the form. The difference between a basis of 0 and auto accounts for most of the surprise when items come out a different size than expected.

Flexbox or grid

Use flexbox when the content decides the sizes: a navigation bar, a row of buttons, a card's internal layout. Use grid when the layout is defined before the content: a page skeleton, a gallery of equal tiles, anything two-dimensional.

They nest happily and most real pages use both — a grid for the page, flexbox inside each card.

Häufig gestellte Fragen

Speziell zum CSS-Flexbox-Generator. Allgemeinere Fragen zu dieser Kategorie werden auf der Kategorieseite beantwortet.

Die Richtung steht auf column, was die Achsen tauscht. In einer Spalte läuft justify-content nach unten und align-items quer.
justify-content: center und align-items: center zusammen. Dieses Paar zentriert auf beiden Achsen, unabhängig von der Richtung.
space-between lässt an den Enden keinen Abstand, das erste und letzte Element berühren also die Ränder. space-evenly macht alle Abstände gleich, auch die äußeren.
flex-wrap steht standardmäßig auf nowrap und drückt sie zusammen. Setzen Sie es auf wrap, damit sie in eine neue Zeile rutschen.
Flexbox für eine einzelne Zeile oder Spalte, in der der Inhalt die Größen bestimmt. Grid, wenn Sie Zeilen und Spalten zugleich brauchen oder das Layout vor dem Inhalt feststeht.

Abonniere Benachrichtigungen zu neuen Tools, Funktionen und Produktivitätstipps.