Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 71 ยท Styling and Layout

Flexbox in React Native

Master Flexbox in React Native: the column default, flexDirection, justifyContent, alignItems, gap, flex, flexGrow and flexBasis, with a live playground.

Every View in React Native is a Flexbox container. There is no display: block, no floats and no CSS grid: all layout, from a simple row of buttons to a complete screen, comes from Flexbox. If you already know Flexbox from CSS, you are 90 percent there, and this lesson focuses on the 10 percent that is different. If you do not, you will learn it here from scratch, because every later lesson depends on it.

The layout itself is computed by Yoga, a small C++ layout engine that ships inside React Native. On this site Yoga is not available, so the playground below uses the browser's own CSS Flexbox with React Native's defaults applied. For the properties in this lesson the two agree closely, but remember the browser is a stand-in.

The two axes

Flexbox lays children out along a main axis and positions them on a cross axis at right angles to it. flexDirection picks the main axis:

  • column (the default in React Native): children stack top to bottom.
  • row: children sit left to right.
  • column-reverse and row-reverse flip the order.

This is the first big difference from the web, where the default is row. A bare View with three children therefore stacks them vertically, which matches how most phone screens are built.

TSX
<View style={{ flexDirection: 'row' }}>
  <Text>One</Text>
  <Text>Two</Text>
  <Text>Three</Text>
</View>

Three properties then control alignment:

Property Axis Common values
justifyContent main flex-start, center, flex-end, space-between, space-around, space-evenly
alignItems cross stretch (default), flex-start, center, flex-end, baseline
alignSelf cross, one child same as alignItems, plus auto

The mnemonic: justifyContent works along the direction of travel, alignItems works across it. Switch flexDirection from column to row and the two swap their physical meaning (vertical becomes horizontal) without you changing them.

Pick an axis

column The React Native default.
row Children side by side.

flexDirection chooses the main axis. Everything else is relative to it.

1 / 2

A live playground

Change the controls and watch the three boxes move. Each box is content-sized, so stretch has something visible to do. The line under the preview is the matching React Native style.

Stand-in: a Flexbox playground

Try this: set flexDirection to row, justifyContent to space-between, and alignItems to center. The boxes spread across the width and line up on their vertical centers. Now set alignItems back to stretch: all three boxes grow to the full height of the container, because none of them has a fixed height. That is the rule to remember: stretch never changes a child that has an explicit size on the cross axis.

gap

gap puts space between children without margins on the first or last item. React Native supports gap, rowGap and columnGap (since version 0.71), and they work with flexWrap: 'wrap' too. Prefer them to per-child margins: a list you reorder never leaves a stray margin at the end.

TSX
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: 12 }}>
  {tags.map((t) => <Tag key={t} label={t} />)}
</View>

flex, flexGrow, flexShrink and flexBasis

These properties decide how children share the leftover space along the main axis.

  • flexBasis: the starting size along the main axis (like width in a row).
  • flexGrow: how many shares of the free space the child receives.
  • flexShrink: how willing it is to shrink when there is not enough space.
  • flex: a shorthand. In React Native, flex: 1 means grow 1, shrink 1, basis 0, so the child takes an equal share of all the space.

The most important idiom in the whole framework is flex: 1 on a root container. A View is only as big as its content unless a parent gives it space, so a screen's outermost view nearly always carries flex: 1 to fill the screen.

TSX
<View style={{ flex: 1 }}>
  <View style={{ height: 56 }} />          {/* fixed header */}
  <View style={{ flex: 1 }} />             {/* fills the rest */}
  <View style={{ height: 64 }} />          {/* fixed footer */}
</View>

Differences from the web. React Native's default flexShrink is 0, while on the web it is 1. Content that is too big therefore overflows instead of shrinking. Also, alignContent defaults to flex-start, so wrapped lines pack at the start instead of stretching.

The sharing rule is a short piece of arithmetic: subtract every child's basis (and the gaps) from the container size; that remainder is the free space; each child with flexGrow gets a slice proportional to its grow value. This stand-in does exactly that calculation (it ignores shrinking, which is off by default in React Native anyway):

JS Stand-in: how free space is shared

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the first call the free space is 300, split 1:2:1, giving 75, 150 and 75. In the second, the two 10-unit gaps leave 280 to share, so each box gets 93.3. In the third, free space is 360 minus 220, which is 140, so the first box gains 35 (to 95), the second stays at 60 and the third gains 105 (to 205).

Putting it together: a typical screen row

A very common pattern is an avatar, a growing text block and a trailing button, all vertically centered:

TSX
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 12, padding: 16 }}>
  <Image source={avatar} style={{ width: 48, height: 48, borderRadius: 24 }} />
  <View style={{ flex: 1 }}>
    <Text style={{ fontWeight: '700' }}>Maya Chen</Text>
    <Text numberOfLines={1}>Sent you a photo from the trip.</Text>
  </View>
  <Text>2m</Text>
</View>

The middle View has flex: 1, so it takes everything the avatar and the time label leave over, and long text truncates instead of pushing the time off screen. You will use this shape in nearly every list in the course; see FlatList Essentials.

Common mistakes

  • A blank screen. A parent without flex: 1 (or a height) collapses to zero height, and a child with flex: 1 inside it then has nothing to fill. Walk up the tree and check each level.
  • Mixing up the two alignment props. If justifyContent: 'center' centers vertically in your column, remember it will center horizontally once you switch to row.
  • Expecting wrapping. Children do not wrap unless you set flexWrap: 'wrap'.
  • Using margins instead of gap for spacing between siblings.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the default flexDirection of a View in React Native?

    React Native defaults to column, unlike CSS where the default is row. Children stack vertically unless you change it.
  2. 2. A View has flexDirection: 'row'. Which property centers its children vertically?

    In a row the cross axis is vertical, and alignItems works on the cross axis. justifyContent would center horizontally.
  3. 3. A container is 300 wide with three children that all have flex: 1 and no gap. How wide is each?

    flex: 1 means grow 1 with a basis of 0, so the 300 units of free space are split equally into three 100-unit shares.
  4. 4. Your screen's root View shows nothing and its flex: 1 child is invisible. What is the most likely cause?

    flex: 1 fills the space its parent has. If the parent has no size, there is nothing to fill. Give each ancestor flex: 1 or a height.

Summary

  • Every View is a Flexbox container; the default flexDirection is column.
  • justifyContent aligns along the main axis, alignItems and alignSelf across it; stretch only affects children without a fixed cross size.
  • gap, rowGap and columnGap replace most sibling margins.
  • flex: 1 means grow 1, shrink 1, basis 0; give the root view flex: 1 so the screen fills.
  • React Native's flexShrink default is 0, so oversized content overflows rather than shrinks.
  • The playground and the distribution function are stand-ins built on the browser's Flexbox; the real layout engine is Yoga.

Next: Sizing, Margins and Absolute Positioning