On the web you write sizes in CSS units, and flex is a shorthand. React Native drops both: a size is a unitless number or a percentage, and flex is one number. Flex children share the space their parent has left, so a parent with no size of its own can leave them 0 points tall.
Key terms
- Point: the unit of every dimension in React Native, a density-independent pixel written as a number with no unit.
flex: a style that takes one number and, when the number is positive, grows the component into its parent's leftover space in proportion to it.boxSizing: the style that sets whetherwidthandheightinclude the padding and border, which they do with the default,'border-box'.
Sizes are unitless points
height: 56 means 56 points. There is no universal mapping from points to physical units, so a component with fixed dimensions might not have the same physical size on every device.
You give a size as a number of points or as a percentage string such as '50%', which is a percentage of the parent's width or height inside its padding and border. Ems and other units are not supported. TypeScript won't catch one, because the type for width and height accepts any string. At runtime, a string that is not a plain number, a percentage or a keyword such as 'auto' (for example '120px') leaves the size undefined.
Sharing leftover space with flex
Siblings that all have flex: 1 share the space evenly. A child with flex: 2 takes twice the space of one with flex: 1, and three siblings with flex values of 1, 2 and 3 get 1/6, 2/6 and 3/6 of it.
Only the space left over is shared. A child without flex keeps its own size, and each flex child gets a share of what remains after the non-flex children, padding and gaps, in proportion to its flex value.
Padding shrinks that space without changing the container's size, because the default boxSizing counts it inside the width and height you set. A row with width: 260 and padding: 20 stays 260 points wide, with 20 points of padding on each side, so its two flex: 1 children share the 220 points inside the padding and get 110 each, not 130.
When the parent has no height
A screen with a header and a content area meant to fill the rest of it:
tsximport {View} from 'react-native'; export default function App() { return ( <View> <View style={{height: 56, backgroundColor: '#1c1c1e'}} /> <View style={{flex: 1, backgroundColor: '#f2f2f7'}} /> </View> ); }
The dark header shows up, 56 points tall. The gray content area never appears, because it ends up 0 points tall.
A flex child can only grow into space its parent has left over on the main axis. The outer View here has no height and no flex, and its parent stretches it across the screen with the default alignItems: 'stretch'. Stretched that way, a column View with no height and no flex is only as tall as its non-flex children, here the 56-point header. That leaves the content area a share of nothing, and if every child used flex, the outer View would be 0 points tall. Either of these changes to the outer View fixes it:
flex: 1makes the outer View fill its own parent. React Native wraps your root component in a View styledflex: 1, so a top-level View withflex: 1has a parent with a size to fill.- With a fixed height such as
height: 300, the content area gets the 244 points left after the 56-point header.
The StorageScreen example below the lesson follows the first fix. Its screen style has flex: 1, so it fills the root wrapper, and fileList takes the height left after the 56-point header and the 24-point bar. The bar mixes fixed and flex children on a row:
tsxusageBar: {flexDirection: 'row', width: 360, height: 24}, system: {width: 40, backgroundColor: '#8e8e93'}, photos: {flex: 1, backgroundColor: '#0a84ff'}, videos: {flex: 3, backgroundColor: '#30d158'},
system keeps its 40 points, and photos and videos split the other 320 in a 1 to 3 ratio, into 80 and 240.
Spacing children with gap
Use columnGap for horizontal gaps, rowGap for vertical gaps, or gap for both. The gap goes only between children, never between a child and the edges of its parent.
A gap is a number of points or a percentage string such as '10%'. A percentage columnGap is a share of the container's width inside its padding and border, and a percentage rowGap is a share of its height, so columnGap: '10%' in a row 300 points wide with no padding comes to 30 points.
Gaps come out of the space flex children share. Every child on a line except the first gets the gap in front of it, so three children in a row with gap: 10 use 20 points before any flex child grows.
In the ShareSheet example, the column's gap: 12 puts 12 points between the title, the message and the button row, and none above the title. In the 328-point button row, columnGap: 8 takes 8 points first, and the two flex: 1 buttons split the remaining 320 into 160 points each.
Code Examples
import {StyleSheet, Text, View} from 'react-native';
export default function StorageScreen() {
return (
<View style={styles.screen}>
<View style={styles.header}>
<Text style={styles.title}>Storage</Text>
</View>
<View style={styles.usageBar}>
<View style={styles.system} />
<View style={styles.photos} />
<View style={styles.videos} />
</View>
<View style={styles.fileList} />
</View>
);
}
const styles = StyleSheet.create({
screen: {flex: 1},
header: {height: 56, justifyContent: 'center'},
title: {fontSize: 20, fontWeight: 'bold'},
usageBar: {flexDirection: 'row', width: 360, height: 24},
system: {width: 40, backgroundColor: '#8e8e93'},
photos: {flex: 1, backgroundColor: '#0a84ff'},
videos: {flex: 3, backgroundColor: '#30d158'},
fileList: {flex: 1, backgroundColor: '#f2f2f7'},
});import {StyleSheet, Text, View} from 'react-native';
export default function ShareSheet() {
return (
<View style={styles.sheet}>
<Text style={styles.title}>Share photo</Text>
<Text>Choose where to send it.</Text>
<View style={styles.buttons}>
<View style={styles.cancel} />
<View style={styles.send} />
</View>
</View>
);
}
const styles = StyleSheet.create({
sheet: {height: 200, gap: 12, backgroundColor: '#ffffff'},
title: {fontSize: 20, fontWeight: 'bold'},
buttons: {flexDirection: 'row', width: 328, columnGap: 8},
cancel: {flex: 1, height: 44, backgroundColor: '#e5e5ea'},
send: {flex: 1, height: 44, backgroundColor: '#0a84ff'},
});