MDN self-hosted library · e01.org

CSS flexible box layout

The CSS flexible box layout module defines a CSS box model optimized for user interface design, and the layout of items in one dimension. In the flex layout model, the children of a flex container can be laid out in any direction, and can "flex" their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent. Both horizontal and vertical alignment of the children can

no runnable cells css-module upstream source

The CSS flexible box layout module defines a CSS box model optimized for user interface design, and the layout of items in one dimension. In the flex layout model, the children of a flex container can be laid out in any direction, and can "flex" their sizes, either growing to fill unused space or shrinking to avoid overflowing the parent. Both horizontal and vertical alignment of the children can be easily manipulated.

Flexible box layout in action

In the following example, a container has been set to display: flex, which means that the three child items become flex items. The value of justify-content has been set to space-between in order to space the items out evenly on the main axis. An equal amount of space is placed between each item with the left and right items being flush with the edges of the flex container. You can also see that the items are stretching on the cross axis, due to the default value of align-items being stretch. The items stretch to the height of the flex container, making them each appear as tall as the tallest item.

```html live-sample___simple-example

One
Two
Three
has
extra
text

```css live-sample___simple-example
body {
  font-family: sans-serif;
}

.box {
  border: 2px dotted rgb(96 139 168);
  display: flex;
  justify-content: space-between;
}

.box > * {
  border: 2px solid rgb(96 139 168);
  border-radius: 5px;
  background-color: rgb(96 139 168 / 0.2);
  padding: 1em;
}

simple-example

Reference

Properties

  • align-content
  • align-items
  • align-self
  • flex
  • flex-basis
  • flex-direction
  • flex-flow
  • flex-grow
  • flex-line-count
  • flex-shrink
  • flex-wrap
  • justify-content

Glossary terms

  • Flexbox
  • Flex container
  • Flex item
  • Main axis
  • Cross axis
  • Flex

Guides

CSS display module

  • display
  • order

CSS box alignment module

  • align-content
  • align-items
  • align-self
  • justify-items
  • place-content
  • place-items

CSS gaps module

  • column-gap
  • column-rule
  • gap
  • row-gap
  • row-rule
  • rule
  • rule-color
  • rule-inset
  • rule-overlap
  • rule-style
  • rule-width

CSS box sizing module

  • aspect-ratio
  • max-content value
  • min-content value
  • fit-content value
  • intrinsic size glossary term

Specifications

See also