Flexbox
React Native uses Flexbox for layout. Note: unlike CSS, flexDirection defaults to column (not row) in React Native.
Key Properties
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column', // 'row' | 'column' (default: column!)
justifyContent: 'center', // main axis alignment
alignItems: 'center', // cross axis alignment
flexWrap: 'wrap', // wrap items
}
})
justifyContent values
flex-start— pack at start (default)flex-end— pack at endcenter— centerspace-between— equal gaps between itemsspace-around— equal gaps around itemsspace-evenly— equal gaps everywhere
alignItems values
flex-start,flex-end,center,stretch
Example: Centered Layout
const CenteredScreen = () => (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>I am centered!</Text>
</View>
)
💡 Practice
Try Flexbox Froggy to practice flexbox concepts interactively.