--- title: Stacking overlapping views with zIndex in Expo and React Native apps sidebar_title: Stacking views with zIndex --- import ImageSpotlight from '~/components/plugins/ImageSpotlight'; import SnackEmbed from '~/components/plugins/SnackEmbed'; import { Collapsible } from '~/ui/components/Collapsible'; `zIndex` is the Expo and React Native analog of [CSS's `z-index` property](https://developer.mozilla.org/en-US/docs/Web/CSS/z-index) which lets the developer control the order in which components are displayed over one another. ## Default `zIndex` behavior Without specifying an explicit `zIndex` or `position`, components that occur later in the tree have a higher z-order. {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( {/* zIndex: 0 */} {/* zIndex: 1 */} {/* zIndex: 2 */} ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `} This is illustrated more clearly when the components visually intersect each other. {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( {/* zIndex: 0 */} {/* zIndex: 1 */} {/* zIndex: 2 */} ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `} ## Changing the `zIndex` of an element If you want to change how a component stacks without changing the order in which it occurs in the component tree, use `zIndex`: {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `} ## Manually positioning your component Along with specifying how the component will stack, you can break out of the default layout set by the component's parent by changing the `position` property on the child component to `'absolute'` and specifying the distance it should be from its parent with the style properties `top`, `right`, `bottom`, and `left`. {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `} You can even make the component extend outside of the parent's visual bounds. {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `} While a `position: 'absolute'` component may seem like it operates independently, it must still respect the `zIndex` of its parent. {` import React from "react"; import { StyleSheet, View } from "react-native";\n export default function App() { return ( ); }\n const styles = StyleSheet.create({ root: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', }, container: { height: 200, width: 200, borderRadius: 16, padding: 16, borderWidth: 8, borderColor: 'rgba(0,0,0,0.2)', }, item: { borderWidth: 4, borderColor: 'rgba(0,0,0,0.2)', height: 48, width: 48, borderRadius: 8, }, }); `}