Learning Objectives
By the end of this module, you’ll understand:- Flexbox fundamentals and layout principles
- Flex direction and wrapping
- Justify content and align items
- Flex sizing (grow, shrink, basis)
- Absolute positioning
- Common layout patterns
Flexbox Fundamentals
Flexbox is the primary layout system in React Native. Unlike CSS on the web, all components in React Native use Flexbox by default — there is nodisplay: block or display: inline. Everything is a flex container.
Think of Flexbox like packing items into a suitcase. You decide the direction items are placed (left-to-right or top-to-bottom), how leftover space is distributed, and what happens when items do not fit.
Key difference from web CSS: In React Native, the default flexDirection is column (vertical), not row (horizontal). This matches mobile conventions where screens are taller than they are wide and content naturally flows top-to-bottom.
Flex Direction
Controls the main axis — the direction children are laid out. Once you set the main axis, the cross axis is always perpendicular to it. Every alignment property in Flexbox refers to one of these two axes, so understanding this distinction is critical.| Value | Description |
|---|---|
column | Vertical stack (default) |
row | Horizontal stack |
column-reverse | Vertical, reversed order |
row-reverse | Horizontal, reversed order |
Justify Content
Aligns children along the main axis:Justify Content Options:
flex-start(default) - Align to startflex-end- Align to endcenter- Center itemsspace-between- Even spacing, edges at container boundsspace-around- Even spacing including before first and after lastspace-evenly- Equal spacing between all items including edges
Align Items
Aligns children along the cross axis:Align Items Options:
stretch(default) - Stretch to fillflex-start- Align to cross-startflex-end- Align to cross-endcenter- Center on cross axisbaseline- Align text baselines
Align Self
Override alignItems for individual children:Flex Sizing
Flex sizing controls how available space is distributed among children. Think of it like splitting a restaurant bill:flex values are each person’s share. A child with flex: 2 gets twice the space of a sibling with flex: 1.
flex
Defines how a component grows relative to siblings:flexGrow
Similar toflex, but the key difference is that flexGrow distributes extra space after each child’s base size is accounted for, while flex ignores base size entirely. In practice, use flex for simple proportional layouts and flexGrow when children have a meaningful minimum width/height:
flexShrink
Controls how components shrink when space is limited:flexBasis
Sets the initial size before flexGrow/flexShrink:Flex Wrap
Controls wrapping of flex items:Flex Wrap Options:
nowrap(default) - Single linewrap- Multi-line, top to bottomwrap-reverse- Multi-line, bottom to top
Align Content
Aligns wrapped lines within container (only works with flexWrap):flex-start, flex-end, center, stretch, space-between, space-around
Positioning
React Native supports two positioning modes. Relative positioning is the default and rarely needs to be set explicitly. Absolute positioning removes an element from the normal layout flow — it no longer pushes other elements around, similar to pulling a sticky note off a stack and placing it anywhere on your desk.Relative Positioning (Default)
Elements positioned according to normal flow, with optional offsets from their natural position:Absolute Positioning
Removes element from document flow:Absolute Fill Shortcut
Z-Index
Controls stacking order of positioned elements:Common Layout Patterns
Center Content
Row with Space Between
Sticky Footer
Sidebar Layout
Grid Layout
Card with Overlay
Gap Property
Beforegap, you had to use margins on individual children to space them apart, which always created an unwanted margin on the first or last item. gap solves this cleanly — it adds space between items only, never at the edges. If you have used CSS Grid’s gap, this is the same concept.
Layout Props Reference
Container Props
| Prop | Type | Description |
|---|---|---|
flex | number | Grow factor |
flexDirection | string | Layout direction |
flexWrap | string | Wrapping behavior |
justifyContent | string | Main axis alignment |
alignItems | string | Cross axis alignment |
alignContent | string | Wrapped lines alignment |
Item Props
| Prop | Type | Description |
|---|---|---|
alignSelf | string | Override container alignItems |
flexGrow | number | Growth factor |
flexShrink | number | Shrink factor |
flexBasis | number/string | Initial size |
Position Props
| Prop | Type | Description |
|---|---|---|
position | string | 'relative' or 'absolute' |
top | number | Offset from top |
bottom | number | Offset from bottom |
left | number | Offset from left |
right | number | Offset from right |
zIndex | number | Stacking order |
Best Practices
- Use
flex: 1for components that should fill available space — this is the single most important Flexbox pattern in React Native. Forgetting it is the most common reason a screen appears blank. - Avoid fixed dimensions when possible — use
flexand percentages. Fixed pixel values that look perfect on your test device will break on a different screen size. - Center anything with
justifyContent: 'center'andalignItems: 'center'— this two-property combo works regardless offlexDirectionand is the universal centering recipe. - Understand the axes —
justifyContentcontrols the main axis (direction offlexDirection),alignItemscontrols the cross axis (perpendicular). This is the mental model that makes all of Flexbox click. - Use
gapinstead of margins for spacing between items — it is cleaner, avoids edge-margin bugs, and requires React Native 0.71+. - Test on extreme screen sizes — Flexbox is responsive by default, but edge cases like the iPhone SE (320pt wide) and iPad (768pt+ wide) often reveal layout assumptions. Run your app on both during development.