Skip to the workspace
Pebellslearn by building
Styling  ›  Lesson

Layout with Flexbox

Layouts & Flexbox 14 minEvery View is a flex container whose main axis is a column by default; justifyContent places children along the main axis, alignItems across it, and flex: 1 fills the remaining space.

react native / Layout with Flexbox

Explore each scene at your own pace.

01 · The rule

The rule

Layout with Flexbox

Sprout's plant card has a photo on the left, the name and next watering date in the middle, and a "Watered" button on the right. Every layout like this in React Native is Flexbox. If you know Flexbox from the web, you are most of the way there, but a few defaults are flipped.

The rule

Column by default; justify along the main axis, align across it.

Every View is a flex container. The key properties:

  • flexDirection sets the main axis. The default is 'column' (top to bottom). On the web it is row. Use 'row' for side-by-side items.
  • justifyContent places children along the main axis: 'flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'.
  • alignItems places children across the main axis: 'stretch' (the default), 'flex-start', 'center', 'flex-end'.
  • flex: 1 tells a child to take the remaining space along the main axis. In React Native, flex takes a single number.
  • gap adds space between children without margins on each one.
const styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 12,
    padding: 12,
  },
  photo: { width: 56, height: 56, borderRadius: 8 },
  info: { flex: 1 },
});

<View style={styles.row}>
  <Image source={{ uri: plant.photoUrl }} style={styles.photo} />
  <View style={styles.info}>
    <Text>{plant.name}</Text>
    <Text>Next water: tomorrow</Text>
  </View>
  <Button title="Watered" onPress={markWatered} />
</View>

The photo has a fixed size, the button takes the width it needs, and info with flex: 1 fills whatever is left. alignItems: 'center' lines all three up vertically.

A screen's outer View usually needs flex: 1 too, so that it fills the whole screen instead of shrinking to its content.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.