Course contentsShow
← React Native
Lesson 251 of 1,5554. Flexbox and Native StylingFree lesson

Aligning Items on the Cross Axis

Utilize alignment properties to manage centering and stretching across disparate screen sizes.

Aligning Items on the Cross Axis

In this lesson, you will learn how to use the alignItems property to precisely position elements perpendicular to your primary layout direction.

Understanding the Cross Axis

When you arrange items in a container, you are working with two axes. You already know the main axis (controlled by flexDirection), which is the path your items naturally follow. The cross axis runs perpendicular to that path. If your flexDirection is row (horizontal), the cross axis is vertical. If your flexDirection is column (vertical), the cross axis is horizontal.

To control how items behave along this secondary axis, we use the alignItems property on the parent container. This is essential for ensuring your layout looks consistent whether it's viewed on a compact phone or a large tablet.

Controlling Alignment

There are four common values for alignItems that help you manage space:

  • stretch (default): Items expand to fill the entire cross axis of the container.
  • flex-start: Items align to the beginning of the cross axis.
  • flex-end: Items align to the end of the cross axis.
  • center: Items are positioned exactly in the middle of the cross axis.

Consider this layout for a button container:

<View style={{ 
  flexDirection: 'row', 
  alignItems: 'center', // Centers items vertically
  height: 100 
}}>
  <Text>Label</Text>
  <Button title="Click Me" />
</View>

In this example, even if the Text component is shorter than the Button, setting alignItems: 'center' ensures both elements sit perfectly level along the vertical line. Without this, they might default to stretch, causing the text to look awkwardly tall or misaligned.

By mastering the cross axis, you stop relying on fixed margins or padding that break on different screen sizes. Instead, you create a layout that "breathes" by letting the container calculate the perfect position for its children automatically.

Key Takeaway: Use alignItems to control the cross axis, allowing your components to stay perfectly positioned regardless of the screen size or the content inside them.