0
0
React Nativemobile~20 mins

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

Choose your learning style9 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.