Flexbox in React Native follows the CSS rules you already know, but several defaults differ, and the first one you run into is direction: a container stacks its children in a column unless you ask for a row. justifyContent and alignItems still work relative to that direction, so alignment copied from a web stylesheet can leave the two properties doing each other's jobs.

Key terms

  • flexDirection: the container style that sets the main axis, which works like CSS flex-direction except that its default is 'column'.
  • Yoga: the layout engine React Native uses to compute Flexbox layout, whose default styles differ from Flexbox on the web.

Children stack in a column

Three avatars, written the way you would put them in a flex container on the web:

tsx
<View>
  <View style={styles.avatar} />
  <View style={styles.avatar} />
  <View style={styles.avatar} />
</View>

A web flex container would place them side by side. In React Native they stack from top to bottom, because the outer View has the default column direction. Add flexDirection: 'row' to it and the avatars line up from left to right.

That outer View never needed display: 'flex'. display defaults to 'flex' and accepts only 'flex', 'none' and 'contents', so every View lays out its children with Flexbox, and there is no 'block' or 'grid' value to switch to.

justifyContent still aligns children along the main axis and alignItems along the cross axis, with the same flex-start and stretch defaults as on the web. In the default column the main axis is vertical, so justifyContent: 'center' centers a child vertically and alignItems: 'center' centers it horizontally, the opposite of a web container with the default row. The stretch default is also why a child with no width is stretched to the full width of a column container, while a child with width: 48 keeps its 48 points.

Lining up a sent message

The SentMessageRow example below the lesson is a row holding a message bubble and its timestamp. Two entries in its StyleSheet set the layout:

tsx
row: {flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center', height: 72},
time: {fontSize: 12, marginLeft: 8, alignSelf: 'flex-end'},

The row spells out flexDirection: 'row', which makes its main axis run from left to right. justifyContent: 'flex-end' then moves both children to the right edge, and alignItems: 'center' centers them within the 72 points of height. alignSelf takes the same values as alignItems and works as it does in CSS. Its default, auto, follows the parent's alignItems, and the timestamp's 'flex-end' overrides that for the timestamp alone and drops it to the bottom of the row, while the bubble stays centered.

Where Yoga departs from CSS

flexShrink defaults to 0, where CSS uses 1. Put three Views with width: 120 in a row 300 points wide. On the web they would shrink to fit. In React Native each one keeps its 120 points, and together they overflow the row by 60. Set flexShrink: 1 on the children that should give up space.

alignContent defaults to flex-start, where CSS uses stretch. It positions the lines of a container whose flexWrap is 'wrap' or 'wrap-reverse', and flex-start packs them at the start of the cross axis instead of stretching them to fill it. The difference shows even when everything fits on one line. Take a row of tags that is 200 points tall and centers its children:

tsx
tags: {flexDirection: 'row', alignItems: 'center', height: 200},

The tags sit in the middle of the 200 points. Add flexWrap: 'wrap' and they move to the top. A wrapping row's line is only as tall as its tallest child, alignContent: 'flex-start' puts that line at the top of the row, and alignItems: 'center' centers the tags within the line. Adding alignContent: 'center' brings the line back to the middle.

flex takes a single number, with no CSS shorthand. The next lesson covers what that number does.

Code Examples

tsx
import {StyleSheet, Text, View} from 'react-native';

export default function SentMessageRow() {
  return (
    <View style={styles.row}>
      <Text style={styles.bubble}>On my way</Text>
      <Text style={styles.time}>9:41</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    justifyContent: 'flex-end',
    alignItems: 'center',
    height: 72,
  },
  bubble: {
    fontSize: 16,
    padding: 8,
    borderRadius: 12,
    backgroundColor: '#0a84ff',
    color: '#ffffff',
  },
  time: {
    fontSize: 12,
    marginLeft: 8,
    alignSelf: 'flex-end',
  },
});
✓ Completed