React Native flexbox: a visual guide with code

React Native lays out every screen with flexbox, and a few defaults differ from CSS. See each property with a diagram, tested code, and fixes for common bugs.

Brian LuerssenBrian Luerssen· Updated
React NativeMobile App DevelopmentProgramming
React Native flexbox: a visual guide with code

Flexbox is the layout system React Native uses for every screen. A container, usually a View, sets which direction its children run in, how they’re spaced along that direction, and how they line up across it. Most layouts come down to three properties: flexDirection, justifyContent, and alignItems.

This guide is for anyone laying out a React Native screen, and especially for people who already know flexbox from CSS, because a few defaults are different and those are the ones behind the confusing bugs. We ran every code sample here on React Native 0.88.1 with Expo SDK 58 in an iOS simulator in November 2026. The diagrams are drawn from the positions and sizes we read back from the running app, not from what we expected to see.

A complete screen to start from

Here’s a full screen that uses all three properties. If you paste it into App.tsx in a new Expo project (we made ours with npx create-expo-app@latest flexbox-test --template blank-typescript), you’ll get three squares in a row, spaced evenly and centered on the screen.

import { StyleSheet, View } from "react-native";

export default function App() {
  return (
    <View style={styles.row}>
      <View style={[styles.box, { backgroundColor: "#5A45FF" }]} />
      <View style={[styles.box, { backgroundColor: "#2684FF" }]} />
      <View style={[styles.box, { backgroundColor: "#22C55E" }]} />
    </View>
  );
}

const styles = StyleSheet.create({
  row: {
    flex: 1,
    flexDirection: "row",
    justifyContent: "space-evenly",
    alignItems: "center",
  },
  box: { width: 64, height: 64, borderRadius: 8 },
});
An iPhone simulator showing a purple, a blue, and a green square in a row, evenly spaced and centered vertically on a white screen
That code running in Expo Go. The gear in the corner is Expo Go's own developer menu.

Most of the samples below change only the row style, so you can keep this file open and try each one as you read.

How it differs from CSS

If you’ve used flexbox on the web, almost all of it carries over. React Native’s documentation lists the main exceptions, and the table adds the others that matter for layout. We checked each row in the simulator, apart from the three-value flex shorthand, which we took from the docs.

PropertyOn the webIn React Native
flexDirectionrowcolumn, so children stack from top to bottom unless you say otherwise
alignContentstretchflex-start, so wrapped lines pack together at the start instead of spreading out
flexShrink10, so children keep their size and overflow instead of shrinking to fit
flexUp to three valuesA single number. flex: 1 works, and the three-value shorthand doesn’t
displayblock or inline for most elementsflex, so every View is already a flex container
positionstaticrelative, so top and left move an element without any other setup
boxSizingcontent-boxborder-box, so padding and borders are counted inside the width you set
Unitspx, rem, %, and othersUnitless numbers and % strings. width: 40 means 40 density-independent pixels, which we call points

The defaults for display, position, and boxSizing come from React Native’s layout props reference, and the units from its height and width page.

Main axis and cross axis

Every flexbox property works along one of two axes, so it helps to get these straight first. The main axis is the direction children are laid out in, which is what flexDirection sets, and the cross axis is the one perpendicular to it. justifyContent positions children along the main axis, and alignItems positions them along the cross axis. So when you change flexDirection, those two properties swap directions.

Two containers with three numbered children. With flexDirection column the main axis runs top to bottom and the cross axis runs left to right. With flexDirection row the main axis runs left to right and the cross axis runs top to bottom. column 1 2 3 row 1 2 3 main axis cross axis main axis cross axis
The main axis is the direction children are laid out in. The cross axis is the other one.

flexDirection: which way children run

flexDirection sets the main axis. The default is column, so children stack from the top down, and row lines them up from the left. The two -reverse values start from the opposite edge, which also reverses the order the children appear in.

row: {
  flexDirection: "row-reverse",
},
Four containers showing the flexDirection values. Column stacks children 1, 2, 3 from the top. Row lines them up from the left. Column-reverse stacks them from the bottom up. Row-reverse lines them up from the right. column (default) 1 2 3 row 1 2 3 column-reverse 1 2 3 row-reverse 1 2 3
The four flexDirection values, with three 40-point children in a 150-point container.

We couldn’t find a reason for the column default in React Native’s documentation or in the documentation for Yoga, the layout engine underneath it. Our read is that it suits a phone, where the screen is taller than it is wide and most content is a vertical stack, but that’s our inference and not something the docs state. One more thing the docs do say is that in a right-to-left language, row runs from right to left, so “left” in this guide really means “start”.

justifyContent: spacing along the main axis

justifyContent controls what happens to the space that’s left over on the main axis once the children have their sizes. The default, flex-start, packs them at the start, and there are five other values.

row: {
  flexDirection: "row",
  justifyContent: "space-between",
},
Six rows showing the justifyContent values in a row container. Flex-start packs the three children at the left, center in the middle, flex-end at the right. Space-between puts the first and last child at the edges. Space-around leaves half-size gaps at the edges. Space-evenly makes every gap the same. flex-start (default) 1 2 3 center 1 2 3 flex-end 1 2 3 space-between 1 2 3 space-around 1 2 3 space-evenly 1 2 3
The six justifyContent values in a row that's 320 points wide.

The three space- values are the easiest to mix up, so here are the numbers from our test. In a 320-point row with three 40-point children there are 200 points to give away. space-between put 100 points between each pair of children and nothing at the edges. space-evenly made all four gaps 50 points. space-around gave each child the same space on both sides, so the gaps at the edges came out at 33.3 points and the ones in the middle at 66.7.

If justifyContent seems to do nothing, check that the container is bigger than its children on the main axis. A column needs a height or flex: 1 before there’s any space to distribute, since a View is only as tall as its content by default.

alignItems and alignSelf: lining up across the axis

alignItems positions children along the cross axis, and its default is stretch. That’s why a child with no width fills the width of a column, and a child with no height fills the height of a row.

row: {
  flexDirection: "row",
  alignItems: "center",
},
Six row containers showing alignItems. With stretch, children with no height fill the container's full height. Flex-start, center, and flex-end place 40-point children at the top, middle, and bottom. With stretch and a fixed height of 40, the children stay 40 points tall. In the last one, the second child has alignSelf flex-end and sits at the bottom while the others stay at the top. stretch (default) 1 2 3 flex-start 1 2 3 center 1 2 3 flex-end 1 2 3 stretch, height: 40 1 2 3 alignSelf: "flex-end" 1 2 3
alignItems in a row container, and alignSelf overriding it for child 2.

stretch only works on a child that doesn’t have a fixed size on the cross axis. In our 110-point-tall row, children with no height came out 110 points tall, and children with height: 40 stayed at 40 and sat at the top, exactly as they did with flex-start. There’s also a baseline value, which lines children up along a shared text baseline.

alignSelf takes the same values and goes on a single child, where it overrides the container’s alignItems for that child only.

flex, flexGrow, flexShrink, and flexBasis: sharing space

These four go on the children, not the container. Together they set how children share the main axis.

<View style={{ flexDirection: "row" }}>
  <View style={{ width: 40 }} />
  <View style={{ flex: 1 }} />
  <View style={{ width: 40 }} />
</View>
Five rows in a 240-point container, each child labelled with its measured width. Flex 1, 2, and 3 give widths of 40, 80, and 120. A flex 1 child between two 40-point children takes the remaining 160. Three 100-point children with the default flexShrink of 0 overflow the container by 60 points. With flexShrink 1 they shrink to 80 each. With a flexBasis of 60 and flexGrow 1 on the middle child, the widths are 60, 120, and 60. flex: 1, flex: 2, flex: 3 40 80 120 width: 40, flex: 1, width: 40 40 160 40 width: 100 each (flexShrink: 0, the default) 100 100 100 width: 100 each, flexShrink: 1 80 80 80 flexBasis: 60 each, flexGrow: 1 on the middle 60 120 60
How children share a 240-point row. Each number is the width React Native gave that child.
  • flex divides the space in proportion. Children with flex: 1, flex: 2, and flex: 3 in a 240-point row came out 40, 80, and 120 points wide. The pattern you’ll use most is the one in the sample above, where a single flex: 1 child takes whatever the fixed-size children leave.
  • flexShrink is the one to watch if you’re coming from CSS. Three 100-point children in a 240-point row overflowed it by 60 points, because the default is 0 and nothing shrinks unless you ask. With flexShrink: 1 on each child, they came out 80 points wide and fit.
  • flexBasis is a child’s starting size on the main axis before any growing or shrinking, and flexGrow hands out the space that’s left. With a flexBasis of 60 on all three children and flexGrow: 1 on the middle one, we got 60, 120, and 60.

The docs describe flex: 1 as shorthand for flexGrow: 1, flexShrink: 1, flexBasis: 0, and we measured identical widths written both ways.

flexWrap, alignContent, and gap

By default, children stay on one line and overflow the container when they don’t fit. flexWrap: "wrap" lets them continue on a new line instead.

row: {
  flexDirection: "row",
  flexWrap: "wrap",
  gap: 8,
},
Seven containers holding five children. With nowrap, the fifth child overflows the right edge. With wrap, it moves to a second line that sits directly under the first. AlignContent center moves both lines to the middle of the container, space-between pushes them to the top and bottom, and stretch makes each line half the container's height. With a gap of 8, only three children fit on a line and the lines are 8 points apart. With rowGap 16 and columnGap 4, the lines are 16 points apart and the children 4 points apart. nowrap (default) 1 2 3 4 5 wrap 1 2 3 4 5 alignContent: center 1 2 3 4 5 space-between 1 2 3 4 5 stretch 1 2 3 4 5 gap: 8 1 2 3 4 5 rowGap 16, columnGap 4 1 2 3 4 5
Five 60-point children in a 240-point container, with flexWrap, alignContent, and gap.

alignContent only does anything once there’s more than one line, because it positions the lines themselves along the cross axis. With the default, flex-start, the second line sits directly under the first. This is the other default that differs from CSS, where the lines would spread out to fill the container. You can get that behavior back. Set alignContent: "stretch" and leave the cross-axis size off the children, which in our 100-point-tall container made each of the two lines 50 points tall.

gap puts space between children without adding margins to each one, and rowGap and columnGap set the two directions separately. Gaps count toward the line, so with gap: 8 only three of our 60-point children fit on a 240-point line (three children and two gaps come to 196 points, and a fourth child would need 264). The docs note that percentage gaps, such as gap: "10%", need the New Architecture, which has been the only architecture since React Native 0.82.

Fixing common React Native layout problems

Here are six layout problems we reproduced in the same iOS simulator, with the smallest fix for each. To measure them, we put each layout inside a 298-point-tall frame standing in for a screen, and read back the sizes React Native gave every view before and after the fix.

Why isn’t justifyContent: “center” centering my content?

A container without a height or flex is only as tall as its children, so there’s no extra space for justifyContent to center anything in. Our container holding a single 40-point box was 40 points tall, and the box sat at the top. With flex: 1 on the container, it filled the 298-point frame and the box moved to the middle, 129 points from the top.

<View style={{ flex: 1, justifyContent: "center" }}>
  <View style={{ width: 40, height: 40 }} />
</View>

Why doesn’t flex: 1 fill the screen?

flex: 1 fills the space a view’s parent has, and a parent with no height of its own has none to share. A child with flex: 1 inside a plain View came out 0 points tall in our test. Adding flex: 1 to the parent made the child fill all 298 points, so when a flex: 1 view collapses, check every parent between it and the screen for flex: 1 or a height.

Why does long text push a button off the screen?

Children in React Native don’t shrink by default, because flexShrink is 0 (the difference from CSS in the table above), so a long Text in a row keeps its full width and pushes whatever comes after it past the edge. In a 402-point row on an iPhone 17 simulator, a long name took 370 points, and the Follow button after it started at 454 points, off the screen. With flexShrink: 1 on the Text, the name wrapped onto two lines and the button came back at 308 points.

Two rows from an iPhone simulator. Before: a purple avatar and the name Alexandria Montgomery-Whitfield, product designer run to the right edge, and the Follow button is missing. After, with flexShrink: 1 on the text: the name wraps onto two lines and a purple Follow button appears on the right
The same row before and after adding flexShrink: 1 to the name, on an iPhone 17 simulator.
<View style={{ flexDirection: "row", alignItems: "center", gap: 12 }}>
  <Avatar />
  <Text style={{ flexShrink: 1 }}>{name}</Text>
  <FollowButton />
</View>

If you’d rather cut the name off than wrap it, add numberOfLines={1} to the Text as well, which truncates it with an ellipsis.

Why did alignItems: “center” stop my views from stretching?

stretch is the default for alignItems, and it’s what makes a child with no width fill the container. Change it to center and that child shrinks to fit its content, so an empty 40-point-tall View we used as a banner went from 198 points wide to 0 and disappeared. To center some children and still stretch one, keep alignItems: "center" on the container and give the one that should stretch alignSelf: "stretch", which brought our banner back to the full 198 points.

Why won’t my ScrollView scroll?

One cause is flex: 1 in the ScrollView’s contentContainerStyle, which caps the content at the ScrollView’s own height, so anything below that has nowhere to scroll to. With 20 rows, 800 points of content, in a 298-point ScrollView, flex: 1 held the content container to 298 points. Use flexGrow: 1 instead. It let the content be all 800 points, and with only two rows it still filled the frame, so with justifyContent: "space-between" a bar we put last landed at the bottom.

<ScrollView contentContainerStyle={{ flexGrow: 1 }}>

Alignment for a ScrollView’s children also belongs in contentContainerStyle. If you put alignItems or justifyContent in its style, React Native stops in development with an error saying they “must be applied through the contentContainerStyle prop.”

How do I keep a button at the bottom, below scrolling content?

Put the button next to the ScrollView, not inside it. Inside the ScrollView, our 50-point button came after 800 points of rows and scrolled away with them. As a sibling in a column with flex: 1, and with flex: 1 on the ScrollView, the ScrollView took 248 points and the button stayed in the last 50.

<View style={{ flex: 1 }}>
  <ScrollView style={{ flex: 1 }}>{rows}</ScrollView>
  <View style={{ height: 50 }}>{button}</View>
</View>

On a real phone, that button also needs to stay clear of the home indicator. Expo’s safe areas guide recommends react-native-safe-area-context, whose SafeAreaView and useSafeAreaInsets give you the padding to add.

Flexbox in Draftbit

All of the code that Draftbit produces for mobile apps is based on React Native and Expo, so everything above applies inside a Draftbit app too. To see what that looks like, we created a new mobile app in Draftbit and gave the agent this prompt:

On the home screen, add a row with three colored boxes. Space them evenly across the row and center them vertically.

The agent added a View with three smaller ones inside it, which is the same layout as the screen at the top of this guide.

The app preview in Draftbit with the new row selected. Purple, blue, and green squares sit inside an outlined row, and a small toolbar above it shows direction, justify, and align buttons
The row the agent built, selected in the preview.

The mobile template we started from is styled with NativeWind, which brings Tailwind’s class names to React Native, so the row is written as className="mt-8 h-24 w-full flex-row items-center justify-evenly" and not as a style object. The last three classes are flexDirection: "row", alignItems: "center", and justifyContent: "space-evenly" under shorter names.

If you select the row and open the Styles tab, the same three settings show up as controls under Layout, next to Gap and Flex.

Draftbit's Styles tab for the selected View. Its classes include flex-row, items-center, and justify-evenly, and under Layout the Direction, Justify Content, and Align Items controls show row, space-evenly, and center selected
The Styles tab for that row. Direction, Justify Content, and Align Items are the three properties from the start of this guide.

Those controls edit the code. When we switched Justify Content to its space-between option, justify-evenly in the file became justify-between. So if you’re still learning which property does what, it’s a quick way to try each value on a real screen and watch the class name change.

Where to go next

React Native’s flexbox page has an interactive example for every property, and the layout props reference lists the ones we didn’t cover, such as position, zIndex, and aspectRatio.

If you’d rather explore flexbox on finished screens, you can open one of our mobile app templates in Draftbit, select any row or card, and look at the Styles tab to see which of these properties it uses.