src/docs/position.mdx
import { ApiTable } from "@/components/api-table.tsx"; import { Example } from "@/components/example.tsx"; import { Figure } from "@/components/figure.tsx"; import { ResponsiveDesign } from "@/components/content.tsx";
export const title = "position"; export const description = "Utilities for controlling how an element is positioned in the document.";
<ApiTable rows={[ ["static", "position: static;"], ["fixed", "position: fixed;"], ["absolute", "position: absolute;"], ["relative", "position: relative;"], ["sticky", "position: sticky;"], ]} />
Use the static utility to position an element according to the normal flow of the document:
<!-- [!code classes:static] -->
<div class="static ...">
<p>Static parent</p>
<div class="absolute bottom-0 left-0 ...">
<p>Absolute child</p>
</div>
</div>
With statically positioned elements, any offsets will be ignored and the element will not act as a position reference for absolutely positioned children.
Use the relative utility to position an element according to the normal flow of the document:
<!-- [!code classes:relative] -->
<div class="relative ...">
<p>Relative parent</p>
<div class="absolute bottom-0 left-0 ...">
<p>Absolute child</p>
</div>
</div>
With relatively position elements, any offsets are calculated relative to the element's normal position and the element will act as a position reference for absolutely positioned children.
Use the absolute utility to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn't exist:
<!-- [!code classes:absolute] -->
<div class="static ...">
<!-- Static parent -->
<div class="static ..."><p>Static child</p></div>
<div class="inline-block ..."><p>Static sibling</p></div>
<!-- Static parent -->
<div class="absolute ..."><p>Absolute child</p></div>
<div class="inline-block ..."><p>Static sibling</p></div>
</div>
With absolutely positioned elements, any offsets are calculated relative to the nearest parent that has a position other than static, and the element will act as a position reference for other absolutely positioned children.
Use the fixed utility to position an element relative to the browser window:
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Andrew Alfred</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Debra Houston</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Jane White</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Ray Flint</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Mindy Albrect</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">David Arnold</strong>
</div>
</div>
</div>
</div>
} </Example>
<!-- [!code classes:fixed] -->
<div class="relative">
<div class="fixed top-0 right-0 left-0">Contacts</div>
<div>
<div>
<strong>Andrew Alfred</strong>
</div>
<div>
<strong>Debra Houston</strong>
</div>
<!-- ... -->
</div>
</div>
With fixed positioned elements, any offsets are calculated relative to the viewport and the element will act as a position reference for absolutely positioned children:
Use the sticky utility to position an element as relative until it crosses a specified threshold, then treat it as fixed until its parent is off screen:
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Andrew Alfred</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Aisha Houston</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Anna White</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Andy Flint</strong>
</div>
</div>
</div>
<div className="relative">
<div className="sticky top-0 flex items-center bg-gray-50/90 px-4 py-3 text-sm font-semibold text-gray-900 ring-1 ring-gray-900/10 backdrop-blur-sm dark:bg-gray-700/90 dark:text-gray-200 dark:ring-black/10">
B
</div>
<div className="divide-y divide-gray-200 dark:divide-gray-200/5">
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Bob Alfred</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Bianca Houston</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Brianna White</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Bert Flint</strong>
</div>
</div>
</div>
<div className="relative">
<div className="sticky top-0 flex items-center bg-gray-50/90 px-4 py-3 text-sm font-semibold text-gray-900 ring-1 ring-gray-900/10 backdrop-blur-sm dark:bg-gray-700/90 dark:text-gray-200 dark:ring-black/10">
C
</div>
<div className="divide-y divide-gray-200 dark:divide-gray-200/5">
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Colton Alfred</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Cynthia Houston</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Cheyenne White</strong>
</div>
<div className="flex items-center gap-4 p-4">
<strong className="text-sm font-medium text-gray-900 dark:text-gray-200">Charlie Flint</strong>
</div>
</div>
</div>
</div>
</div>
} </Example>
<!-- [!code classes:sticky,top-0] -->
<div>
<div>
<div class="sticky top-0 ...">A</div>
<div>
<div>
<strong>Andrew Alfred</strong>
</div>
<div>
<strong>Aisha Houston</strong>
</div>
<!-- ... -->
</div>
</div>
<div>
<div class="sticky top-0">B</div>
<div>
<div>
<strong>Bob Alfred</strong>
</div>
<!-- ... -->
</div>
</div>
<!-- ... -->
</div>
With sticky positioned elements, any offsets are calculated relative to the element's normal position and the element will act as a position reference for absolutely positioned children.