View with position: 'relative' and top: 20. What happens to the view's position on the screen?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 } });
With position: 'relative', the element moves visually by the offset values but still occupies its original space in the layout. So the box moves down 20 units but the space it originally took remains reserved.
View has position: 'absolute' and top: 30, where will it appear relative to?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 } });
With position: 'absolute', the element is positioned relative to its nearest ancestor that has a position other than 'static' (default). Here, the yellow container has position: 'relative', so the red box is placed 30 units from the top inside that container.
position style from relative to absolute dynamically, what happens to the layout?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' } });
When the box changes to position: 'absolute', it is removed from the normal layout flow. It moves visually but the space it originally occupied remains empty, so the layout does not reflow to fill that space.
Option A uses numeric values for bottom and right with position: 'absolute', which is correct. Option A uses strings instead of numbers, which is invalid. Option A uses position: 'relative', so bottom and right offsets won't work as expected. Option A uses top and left instead of bottom and right.
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 } });
Because the container does not have a position style (defaults to 'static'), the absolutely positioned box is placed relative to the screen, not inside the container. To fix this, set position: 'relative' on the container.