Width, height, and flex help you control how big or small your app parts look on the screen. They make your app look nice on different devices.
Width, height, and flex in React Native
Introduction
Syntax
React Native
const styles = StyleSheet.create({
box: {
width: 100, // fixed width in pixels
height: 50, // fixed height in pixels
flex: 1 // flexible size to fill space
}
});width and height set fixed sizes in pixels.
flex makes the item grow or shrink to fill available space.
Examples
React Native
const styles = StyleSheet.create({
box: {
width: 150,
height: 100
}
});React Native
const styles = StyleSheet.create({
box: {
flex: 1
}
});React Native
const styles = StyleSheet.create({
box: {
flex: 2
}
});Sample App
This app shows three colored boxes in a row. The first box is fixed size 100x100 pixels. The second and third boxes share the rest of the space. The third box is twice as wide as the second because it has flex 2.
React Native
import React from 'react'; import { View, StyleSheet, Text } from 'react-native'; export default function App() { return ( <View style={styles.container}> <View style={styles.box1}> <Text style={styles.text}>Fixed Size</Text> </View> <View style={styles.box2}> <Text style={styles.text}>Flex 1</Text> </View> <View style={styles.box3}> <Text style={styles.text}>Flex 2</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'row', padding: 10 }, box1: { width: 100, height: 100, backgroundColor: '#f28b82', justifyContent: 'center', alignItems: 'center', marginRight: 10 }, box2: { flex: 1, backgroundColor: '#fbbc04', justifyContent: 'center', alignItems: 'center', marginRight: 10 }, box3: { flex: 2, backgroundColor: '#34a853', justifyContent: 'center', alignItems: 'center' }, text: { color: 'white', fontWeight: 'bold' } });
Important Notes
Use flexDirection to decide if boxes line up in a row or column.
Fixed width and height override flex size.
Flex works best when container has a size (like flex: 1 on container).
Summary
Width and height set fixed sizes in pixels.
Flex lets items grow or shrink to fill space.
Use flex to make your app look good on different screen sizes.
Practice
1. What does the
flex property do in React Native layout?easy
Solution
Step 1: Understand the role of
Theflexflexproperty controls how a component grows or shrinks to fill space in its container.Step 2: Compare with fixed sizes
Unlike fixedwidthorheight,flexis flexible and adapts to screen size.Final Answer:
It makes the component grow or shrink to fill available space. -> Option CQuick Check:
Flex = flexible size [OK]
Hint: Flex means flexible size, not fixed pixels [OK]
Common Mistakes:
- Confusing flex with fixed width or height
- Thinking flex changes font size
- Assuming flex sets exact pixel size
2. Which of the following is the correct way to set a fixed width of 100 pixels in React Native style?
easy
Solution
Step 1: Check value type for width
In React Native, fixed sizes are numbers without quotes, representing pixels.Step 2: Identify correct syntax
Only{ width: 100 }uses a number correctly; strings or units like 'px' are invalid.Final Answer:
{ width: 100 } -> Option AQuick Check:
Fixed size = number without quotes [OK]
Hint: Use numbers, not strings, for fixed sizes [OK]
Common Mistakes:
- Using strings instead of numbers for width
- Adding 'px' units which React Native does not use
- Confusing flex with fixed width
3. What will be the height of the red box in this code?
import React from 'react';
import { View } from 'react-native';
export default function App() {
return (
);
}medium
Solution
Step 1: Analyze red box style
The red box hasheight: 100, so it is fixed at 100 pixels tall.Step 2: Understand flex on sibling
The blue box usesflex: 1to fill remaining space, but red box height stays fixed.Final Answer:
The red box will have height 100 pixels. -> Option DQuick Check:
Fixed height overrides flex for that box [OK]
Hint: Fixed height overrides flex size [OK]
Common Mistakes:
- Assuming flex affects fixed height components
- Thinking red box fills all space
- Confusing flex values with pixel sizes
4. Find the error in this React Native style code:
const styles = {
container: {
flex: '1',
width: 200
}
};medium
Solution
Step 1: Check
In React Native,flexvalue typeflexmust be a number, not a string.Step 2: Validate
widthusagewidth: 200is correct as a number; no 'px' needed.Final Answer:
flexshould be a number, not a string. -> Option AQuick Check:
flex = number, not string [OK]
Hint: flex and width values must be numbers, not strings [OK]
Common Mistakes:
- Using strings for flex or width
- Adding 'px' units to width
- Thinking flex and width cannot coexist
5. You want a container with two boxes: the first box fixed width 100 pixels, the second box fills the rest of the screen width. Which style setup achieves this?
hard
Solution
Step 1: Set parent direction to row
To place boxes side by side horizontally, useflexDirection: 'row'.Step 2: Assign fixed width and flexible width
Box1 gets fixedwidth: 100, Box2 usesflex: 1to fill remaining space.Final Answer:
Parent: { flexDirection: 'row' }, Box1: { width: 100 }, Box2: { flex: 1 } -> Option BQuick Check:
Row + fixed width + flex fill = Parent: { flexDirection: 'row' }, Box1: { width: 100 }, Box2: { flex: 1 } [OK]
Hint: Use row direction for horizontal layout with fixed + flex [OK]
Common Mistakes:
- Using column direction for horizontal layout
- Swapping flex and width on boxes
- Forgetting flexDirection property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
