Position

Utilities for controlling how an element is positioned in the DOM.

React propsCSS Properties
position={keyword}position: {keyword};

Static

Use position="static" to position an element according to the normal flow of the document.

Any offsets will be ignored and the element will not act as a position reference for absolutely positioned children.

<> <template preview> <x.div position="relative" bg="fuchsia-200" p={6}> <x.div position="static" h={32} bg="fuchsia-300" p={4} color="fuchsia-600" fontWeight="medium" > <p>Static parent</p> <x.div position="absolute" bottom={0} left={0} bg="fuchsia-500" p={4} color="fuchsia-50" > <p>Absolute child</p> </x.div> </x.div> </x.div> </template> <x.div position="static"> <p>Static parent</p> <x.div position="absolute" bottom={0} left={0}> <p>Absolute child</p> </x.div> </x.div> </>

Relative

Use position="relative" to position an element according to the normal flow of the document.

Offsets are calculated relative to the element's normal position and the element will act as a position reference for absolutely positioned children.

<> <template preview> <x.div position="relative" bg="red-200" p={6}> <x.div position="relative" h={32} bg="red-300" p={4} color="red-600" fontWeight="medium" > <p>Relative parent</p> <x.div position="absolute" bottom={0} left={0} bg="red-500" p={4} color="red-50" > <p>Absolute child</p> </x.div> </x.div> </x.div> </template> <x.div position="relative"> <p>Static parent</p> <x.div position="absolute" bottom={0} left={0}> <p>Absolute child</p> </x.div> </x.div> </>

Absolute

Use position="absolute" to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn't exist.

Offsets are calculated relative to the nearest parent that has a position other than position="static", and the element will act as a position reference for other absolutely positioned children.

<> <template preview> <x.div spaceY={6}> <div> <x.p fontSize="sm" color="indigo-900" mb={2}> With static positioning </x.p> <x.div position="relative" bg="indigo-200" px={6} pt={2} pb={6} fontWeight="medium" > <x.p mb={2} color="indigo-600"> Relative parent </x.p> <x.div position="static" bg="indigo-300" p={4} color="indigo-700"> <x.p mb={2}>Static parent</x.p> <x.div position="static" bottom={0} left={0} bg="indigo-500" p={4} color="indigo-50" display="inline-block" > <p>Static child</p> </x.div> <x.div bg="indigo-200" p={4} color="indigo-600" display="inline-block" > <p>Static sibling</p> </x.div> </x.div> </x.div> </div> <div> <x.p fontSize="sm" color="indigo-900" mb={2}> With absolute positioning </x.p> <x.div position="relative" bg="indigo-200" px={6} pt={2} pb={6} fontWeight="medium" > <x.p mb={2} color="indigo-600"> Relative parent </x.p> <x.div position="static" bg="indigo-300" p={4} color="indigo-700"> <x.p mb={2}>Static parent</x.p> <x.div position="absolute" top={0} right={0} bg="indigo-500" p={4} color="indigo-50" display="inline-block" > <p>Absolute child</p> </x.div> <x.div bg="indigo-200" p={4} color="indigo-600" display="inline-block" > <p>Static sibling</p> </x.div> </x.div> </x.div> </div> </x.div> </template> <x.div position="static"> {/* Static parent */} <x.div position="static"> <p>Static child</p> </x.div> <x.div display="inline-block"> <p>Static sibling</p> </x.div> {/* Static parent */} <x.div position="absolute"> <p>Absolute child</p> </x.div> <x.div display="inline-block"> <p>Static sibling</p> </x.div> </x.div> </>

Fixed

Use position="fixed" to position an element relative to the browser window.

Offsets are calculated relative to the viewport and the element will act as a position reference for absolutely positioned children.

<> <template preview> <x.div overflow="hidden" maxWidth="md" mx="auto" my={4} borderRadius="xl"> <x.div borderRadius="xl 0 0" bg="amber-200" display="flex" px={4} py={3}> <x.div mr={6}> <x.div display="inline-block" borderRadius="full" bg="amber-400" h={3} w={3} mr={1} /> <x.div display="inline-block" borderRadius="full" bg="amber-400" h={3} w={3} mr={1} /> <x.div display="inline-block" borderRadius="full" bg="amber-400" h={3} w={3} /> </x.div> <x.div flex="1 1 0" bg="amber-100" borderRadius mr={4} /> </x.div> <x.div position="relative" bg="amber-100" color="amber-600" h={64} fontWeight="medium" > {/* Hey fellow nerd! Yes, we're using position absolute instead of position fixed in the demo; it's the best way we could come up with to demonstrate how position fixed works without totally jacking up the layout of the entire documentation site. Forgive us! 😄 */} <x.div position="absolute" left={0} right={0} top={0} bg="amber-300" color="amber-700" zIndex={10} px={4} py={3} > Fixed child </x.div> <x.div position="absolute" top={0} right={0} bottom={0} left={0} overflow="auto" pt={16} px={6} pb={4} spaceY={4} fontFamily="Flow" > <x.p fontFamily="sans">Scroll me!</x.p> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero.{' '} </p> <p> Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.{' '} </p> <p> Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna.{' '} </p> <p> Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi.{' '} </p> <p> Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. Aenean lectus elit, fermentum non, convallis id, sagittis at, neque.{' '} </p> <p> Nullam mauris orci, aliquet et, iaculis et, viverra vitae, ligula. Nulla ut felis in purus aliquam imperdiet. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.{' '} </p> <p> Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet.{' '} </p> <p> Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam.{' '} </p> </x.div> </x.div> </x.div> </template> <div> <x.div position="fixed">Fixed child</div> Scroll me! Lorem ipsum... </div> </>

Sticky

Use position="sticky" to position an element as relative until it crosses a specified threshold, then treat it as fixed until its parent is off screen.

Offsets are calculated relative to the element's normal position and the element will act as a position reference for absolutely positioned children.

<> <template preview> <x.div h={64} overflow="auto"> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 1 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi.{' '} </x.p> </div> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 2 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. Aenean lectus elit, fermentum non, convallis id, sagittis at, neque.{' '} </x.p> </div> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 3 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Nullam mauris orci, aliquet et, iaculis et, viverra vitae, ligula. Nulla ut felis in purus aliquam imperdiet. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.{' '} </x.p> </div> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 4 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi.{' '} </x.p> </div> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 5 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi. Quisque nisl felis, venenatis tristique, dignissim in, ultrices sit amet, augue. Proin sodales libero eget ante. Nulla quam. Aenean laoreet. Vestibulum nisi lectus, commodo ac, facilisis ac, ultricies eu, pede. Ut orci risus, accumsan porttitor, cursus quis, aliquet eget, justo. Sed pretium blandit orci. Ut eu diam at pede suscipit sodales. Aenean lectus elit, fermentum non, convallis id, sagittis at, neque.{' '} </x.p> </div> <div> <x.div position="sticky" top={0} bg="emerald-200" px={2} py={1} fontWeight="semibold" color="emerald-600" borderRadius="md" > Sticky Heading 6 </x.div> <x.p py={4} fontWeight="medium" color="emerald-500"> Nullam mauris orci, aliquet et, iaculis et, viverra vitae, ligula. Nulla ut felis in purus aliquam imperdiet. Maecenas aliquet mollis lectus. Vivamus consectetuer risus et tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa.{' '} </x.p> </div> </x.div> </template> <div> <x.div position="sticky" top={0}> Sticky Heading 1 </x.div> <x.p py={4}>Quisque cursus...</x.p> </div> <div> <x.div position="sticky" top={0}> Sticky Heading 2 </x.div> <x.p py={4}>Integer lacinia...</x.p> </div> <div> <x.div position="sticky" top={0}> Sticky Heading 3 </x.div> <x.p py={4}>Nullam mauris...</x.p> </div> </>

Responsive

To change how an element is positioned only at a specific breakpoint, use responsive object notation. For example, adding the property position={{ md: "absolute" }} to an element would apply the position="absolute" utility at medium screen sizes and above.

<x.div position={{ md: 'absolute' }} />

For more information about xstyled's responsive design features, check out Responsive Design documentation.

Edit this page on GitHub