Bird
Raised Fist0
React Nativemobile~20 mins

Position (relative, absolute) in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Positioning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
How does relative positioning affect layout?
Consider a React Native View with position: 'relative' and top: 20. What happens to the view's position on the screen?
React Native
import React from 'react';
import { View, StyleSheet } from 'react-native';

export default function App() {
  return <View style={styles.box} />;
}

const styles = StyleSheet.create({
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'blue',
    position: 'relative',
    top: 20
  }
});
AThe box moves down 20 units and the space it originally occupied collapses.
BThe box moves down 20 units from its original place but still takes up its original space.
CThe box stays in the original place and ignores the top value.
DThe box moves up 20 units from its original place.
Attempts:
2 left
💡 Hint
Relative positioning shifts the element visually but does not remove it from the layout flow.
❓ ui_behavior
intermediate
2:00remaining
What happens with absolute positioning in React Native?
If a View has position: 'absolute' and top: 30, where will it appear relative to?
React Native
import React from 'react';
import { View, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.absoluteBox} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    width: 200,
    height: 200,
    backgroundColor: 'yellow',
    position: 'relative'
  },
  absoluteBox: {
    width: 50,
    height: 50,
    backgroundColor: 'red',
    position: 'absolute',
    top: 30
  }
});
AThe red box appears 30 units from the bottom of the yellow container.
BThe red box appears 30 units from the top of the entire screen.
CThe red box ignores the top value and stays at the container's top-left corner.
DThe red box appears 30 units from the top of the yellow container.
Attempts:
2 left
💡 Hint
Absolute positioning is relative to the nearest positioned ancestor.
❓ lifecycle
advanced
2:00remaining
How does changing position style affect layout reflow?
In React Native, if you change a component's position style from relative to absolute dynamically, what happens to the layout?
React Native
import React, { useState } from 'react';
import { View, Button, StyleSheet } from 'react-native';

export default function App() {
  const [pos, setPos] = useState('relative');
  return (
    <>
      <Button title="Toggle Position" onPress={() => setPos(pos === 'relative' ? 'absolute' : 'relative')} />
      <View style={[styles.box, { position: pos, top: pos === 'absolute' ? 50 : 0 }]} />
    </>
  );
}

const styles = StyleSheet.create({
  box: {
    width: 100,
    height: 100,
    backgroundColor: 'green'
  }
});
AThe box disappears when position changes to absolute.
BThe box visually moves and the layout reflows to fill the space when position changes to absolute.
CThe box moves visually but the layout does not reflow, leaving empty space when absolute.
DThe box stays fixed and the layout reflows around it regardless of position.
Attempts:
2 left
💡 Hint
Absolute positioned elements are removed from normal layout flow.
📝 Syntax
advanced
2:00remaining
Which style object correctly applies absolute positioning with bottom and right offsets?
Choose the correct React Native style object to position a box absolutely 10 units from the bottom and 15 units from the right.
A{ position: 'absolute', bottom: 10, right: 15 }
B{ position: 'absolute', bottom: '10', right: '15' }
C{ position: 'relative', bottom: 10, right: 15 }
D{ position: 'absolute', top: 10, left: 15 }
Attempts:
2 left
💡 Hint
Position values must be numbers, and position must be 'absolute' for offsets to work.
🔧 Debug
expert
3:00remaining
Why does this absolutely positioned box not appear inside its container?
Given this code, why is the red box not visible inside the yellow container?
React Native
import React from 'react';
import { View, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.absoluteBox} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    width: 200,
    height: 200,
    backgroundColor: 'yellow',
    position: 'relative'
  },
  absoluteBox: {
    width: 50,
    height: 50,
    backgroundColor: 'red',
    position: 'absolute',
    top: 20,
    left: 20
  }
});
AThe container has no position set, so the absolute box is positioned relative to the screen, not the container.
BThe absoluteBox has invalid width and height values, so it is not visible.
CThe container's background color hides the absoluteBox behind it.
DThe absoluteBox is positioned outside the container because top and left are too large.
Attempts:
2 left
💡 Hint
Absolute positioning depends on the nearest positioned ancestor.

Practice

(1/5)
1. In React Native, what does position: 'relative' do to a component?
easy
A. Makes the component invisible but still takes space.
B. Moves the component relative to its normal position without removing it from the layout flow.
C. Removes the component from the layout flow and hides it.
D. Places the component at a fixed position on the screen ignoring other elements.

Solution

  1. Step 1: Understand relative positioning

    Relative positioning moves the component from where it normally appears but keeps it in the layout flow.
  2. Step 2: Compare with absolute positioning

    Absolute positioning removes the component from the flow and places it exactly, unlike relative.
  3. Final Answer:

    Moves the component relative to its normal position without removing it from the layout flow. -> Option B
  4. Quick Check:

    Relative = moves from normal spot [OK]
Hint: Relative moves from normal spot, absolute places exactly [OK]
Common Mistakes:
  • Confusing relative with absolute positioning
  • Thinking relative removes element from layout
  • Assuming relative hides the component
2. Which of the following is the correct way to set a component's position to absolute in React Native style?
easy
A. { pos: 'absolute' }
B. { position: 'fixed' }
C. { position: 'absolute' }
D. { position: absolute }

Solution

  1. Step 1: Check React Native style syntax

    Position must be a string value, so it should be 'absolute' in quotes.
  2. 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.
  3. Final Answer:

    { position: 'absolute' } -> Option C
  4. Quick Check:

    Correct syntax uses quotes and 'position' key [OK]
Hint: Use quotes for string values in style objects [OK]
Common Mistakes:
  • Using 'fixed' instead of 'absolute'
  • Missing quotes around 'absolute'
  • Using wrong property name like 'pos'
3. What will be the position of the blue box in this code snippet?
{`
  
`}
medium
A. The blue box will be 10 units from the top and left inside the red box.
B. The blue box will be at the top-left corner of the screen.
C. The blue box will be below the red box.
D. The blue box will overlap the red box but at bottom-right.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The blue box will be 10 units from the top and left inside the red box. -> Option A
  4. Quick Check:

    Absolute inside parent = offset from parent edges [OK]
Hint: Absolute inside parent uses parent's edges as reference [OK]
Common Mistakes:
  • Thinking absolute positions relative to screen always
  • Ignoring top and left values
  • Assuming blue box is outside red box
4. Identify the error in this React Native style code:
{`const styles = StyleSheet.create({
  box: {
    position: 'absolute',
    top: 20,
    left: 20,
    width: 100,
    height: 100,
    backgroundColor: 'green',
    position: 'relative'
  }
});`}
medium
A. The backgroundColor value should be uppercase.
B. The width and height must be strings with units.
C. The top and left properties cannot be used with relative position.
D. The style has two position properties; the last one overrides the first.

Solution

  1. Step 1: Check for duplicate properties

    The style object has two 'position' keys; the second 'relative' overrides the first 'absolute'.
  2. Step 2: Understand effect of duplicate keys

    Only the last 'position' value is used, so the box is positioned relative, not absolute as intended.
  3. Final Answer:

    The style has two position properties; the last one overrides the first. -> Option D
  4. Quick Check:

    Duplicate keys overwrite earlier ones [OK]
Hint: Avoid duplicate keys; last one wins [OK]
Common Mistakes:
  • Thinking both position values apply
  • Using units like '100px' instead of numbers
  • Assuming backgroundColor case matters
5. You want to place a small badge at the top-right corner of a profile picture in React Native. The profile picture is 100x100. Which style for the badge will correctly position it overlapping the top-right corner inside the picture?
hard
A. { position: 'absolute', top: 0, right: 0, width: 20, height: 20, backgroundColor: 'red' }
B. { position: 'relative', top: -10, right: -10, width: 20, height: 20, backgroundColor: 'red' }
C. { position: 'absolute', top: -10, right: -10, width: 20, height: 20, backgroundColor: 'red' }
D. { position: 'absolute', bottom: 0, left: 0, width: 20, height: 20, backgroundColor: 'red' }

Solution

  1. Step 1: Understand absolute positioning inside parent

    The badge must be absolutely positioned relative to the profile picture container.
  2. Step 2: Position at top-right corner

    Setting top: 0 and right: 0 places the badge exactly at the top-right inside the picture.
  3. Step 3: Avoid negative offsets unless intentional

    Negative values move outside the parent; here we want it inside overlapping, so 0 is correct.
  4. Final Answer:

    { position: 'absolute', top: 0, right: 0, width: 20, height: 20, backgroundColor: 'red' } -> Option A
  5. Quick Check:

    Absolute top-right corner = top:0, right:0 [OK]
Hint: Use top:0 and right:0 for top-right inside parent [OK]
Common Mistakes:
  • Using relative position which won't overlap correctly
  • Using negative top/right pushing badge outside
  • Placing badge at bottom-left by mistake
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard