Flexbox

Also called Flex layout

Updated

What is Flexbox in React Native?

Flexbox is the layout system React Native uses for every view. It matches CSS Flexbox closely, with a column default direction and a few React Native specific differences.

Child with flex: 1Child viewMain axisEvery React Native view is a flex container, laid out by Yoga. flexDirection picks the main axis (column by default, unlike the web's row), justifyContent distributes children along it and alignItems places them across it; stretch, the default, makes them fill the cross axis. flex: 1 tells a child to take all the remaining space along the main axis, which is why a screen's root view almost always has it.

Flexbox is how you lay out every screen in React Native. There is no grid, no float, and no absolute positioning by default — layout is flex containers all the way down, computed by the Yoga engine.

The three properties that do most of the work:

  • flexDirection — the main axis, column (default) or row
  • justifyContent — distribution along the main axis
  • alignItems — alignment across the main axis

Differences from web CSS that catch newcomers:

Web CSSReact Native
Default flexDirectionrowcolumn
Default alignContentstretchflex-start
flex valueshorthand stringa single number
Unitspx, rem, %unitless density-independent numbers

flex: 1 means "take all remaining space", which is why a root screen container almost always has it. If a view is invisible, the usual cause is a parent with no height rather than a styling mistake on the view itself.

See layout with Flexbox.