Positioning helps you place elements exactly where you want on the screen. It makes your app look neat and organized.
Position (relative, absolute) in React Native
style={{ position: 'relative' | 'absolute', top?: number, left?: number, right?: number, bottom?: number }}relative means the element moves relative to its normal place.
absolute means the element moves relative to its closest positioned parent or the screen.
style={{ position: 'relative', top: 10, left: 20 }}style={{ position: 'absolute', top: 0, right: 0 }}style={{ position: 'absolute', bottom: 10, left: 10 }}This example shows two boxes inside a container. The blue box uses relative positioning and moves 20 units down and right from its normal place. The red box uses absolute positioning and is placed 50 units from the top and left of the container, overlapping the blue box.
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; export default function PositionExample() { return ( <View style={styles.container}> <View style={styles.boxRelative}> <Text>Relative</Text> </View> <View style={styles.boxAbsolute}> <Text>Absolute</Text> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#eee', justifyContent: 'center', alignItems: 'center', position: 'relative' }, boxRelative: { width: 100, height: 100, backgroundColor: 'skyblue', position: 'relative', top: 20, left: 20, justifyContent: 'center', alignItems: 'center' }, boxAbsolute: { width: 100, height: 100, backgroundColor: 'salmon', position: 'absolute', top: 50, left: 50, justifyContent: 'center', alignItems: 'center' } });
Absolute positioned elements ignore the normal layout flow and can overlap other elements.
Relative positioning moves the element but keeps its original space reserved.
Always set the parent container's position if you want absolute children to be positioned relative to it.
Positioning controls where elements appear on the screen.
Relative moves elements from their normal spot.
Absolute places elements exactly inside their parent or screen.
Practice
position: 'relative' do to a component?Solution
Step 1: Understand relative positioning
Relative positioning moves the component from where it normally appears but keeps it in the layout flow.Step 2: Compare with absolute positioning
Absolute positioning removes the component from the flow and places it exactly, unlike relative.Final Answer:
Moves the component relative to its normal position without removing it from the layout flow. -> Option BQuick Check:
Relative = moves from normal spot [OK]
- Confusing relative with absolute positioning
- Thinking relative removes element from layout
- Assuming relative hides the component
Solution
Step 1: Check React Native style syntax
Position must be a string value, so it should be 'absolute' in quotes.Step 2: Identify correct property name and value
The property is 'position' and the value is 'absolute' in quotes. 'fixed' is invalid, 'pos' is the wrong property name.Final Answer:
{ position: 'absolute' } -> Option CQuick Check:
Correct syntax uses quotes and 'position' key [OK]
- Using 'fixed' instead of 'absolute'
- Missing quotes around 'absolute'
- Using wrong property name like 'pos'
{`
`}Solution
Step 1: Understand absolute positioning inside parent
Absolute positions are relative to the nearest positioned parent. The red box is the parent with default relative position.Step 2: Apply top and left values
The blue box is placed 10 units from top and left inside the red box, so it appears inside near top-left corner.Final Answer:
The blue box will be 10 units from the top and left inside the red box. -> Option AQuick Check:
Absolute inside parent = offset from parent edges [OK]
- Thinking absolute positions relative to screen always
- Ignoring top and left values
- Assuming blue box is outside red box
{`const styles = StyleSheet.create({
box: {
position: 'absolute',
top: 20,
left: 20,
width: 100,
height: 100,
backgroundColor: 'green',
position: 'relative'
}
});`}Solution
Step 1: Check for duplicate properties
The style object has two 'position' keys; the second 'relative' overrides the first 'absolute'.Step 2: Understand effect of duplicate keys
Only the last 'position' value is used, so the box is positioned relative, not absolute as intended.Final Answer:
The style has two position properties; the last one overrides the first. -> Option DQuick Check:
Duplicate keys overwrite earlier ones [OK]
- Thinking both position values apply
- Using units like '100px' instead of numbers
- Assuming backgroundColor case matters
Solution
Step 1: Understand absolute positioning inside parent
The badge must be absolutely positioned relative to the profile picture container.Step 2: Position at top-right corner
Setting top: 0 and right: 0 places the badge exactly at the top-right inside the picture.Step 3: Avoid negative offsets unless intentional
Negative values move outside the parent; here we want it inside overlapping, so 0 is correct.Final Answer:
{ position: 'absolute', top: 0, right: 0, width: 20, height: 20, backgroundColor: 'red' } -> Option AQuick Check:
Absolute top-right corner = top:0, right:0 [OK]
- Using relative position which won't overlap correctly
- Using negative top/right pushing badge outside
- Placing badge at bottom-left by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
