Layout

Position

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

Quick reference

Class
Properties
staticposition: static;
fixedposition: fixed;
absoluteposition: absolute;
relativeposition: relative;
stickyposition: sticky;

Basic usage

Statically positioning elements

Use 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.

Position - 图1

  1. <div class="static ...">
  2. <p>Static parent</p>
  3. <div class="absolute bottom-0 left-0 ...">
  4. <p>Absolute child</p>
  5. </div>
  6. </div>

Relatively positioning elements

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

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

Position - 图2

  1. <div class="relative ...">
  2. <p>Relative parent</p>
  3. <div class="absolute bottom-0 left-0 ...">
  4. <p>Absolute child</p>
  5. </div>
  6. </div>

Absolutely positioning elements

Use 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.

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.

Position - 图3

  1. <div class="static ...">
  2. <!-- Static parent -->
  3. <div class="static ..."><p>Static child</p></div>
  4. <div class="inline-block ..."><p>Static sibling</p></div>
  5. <!-- Static parent -->
  6. <div class="absolute ..."><p>Absolute child</p></div>
  7. <div class="inline-block ..."><p>Static sibling</p></div>
  8. </div>

Fixed positioning elements

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

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

Position - 图4

  1. <div class="relative">
  2. <div class="fixed top-0 left-0 right-0">Contacts</div>
  3. <div>
  4. <div>
  5. <img src="..." />
  6. <strong>Andrew Alfred</strong>
  7. </div>
  8. <div>
  9. <img src="..." />
  10. <strong>Debra Houston</strong>
  11. </div>
  12. <!-- ... -->
  13. </div>
  14. </div>

Sticky positioning elements

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

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

Position - 图5

  1. <div>
  2. <div>
  3. <div class="sticky top-0 ...">A</div>
  4. <div>
  5. <div>
  6. <img src="..." />
  7. <strong>Andrew Alfred</strong>
  8. </div>
  9. <div>
  10. <img src="..." />
  11. <strong>Aisha Houston</strong>
  12. </div>
  13. <!-- ... -->
  14. </div>
  15. </div>
  16. <div>
  17. <div class="sticky top-0">B</div>
  18. <div>
  19. <div>
  20. <img src="..." />
  21. <strong>Bob Alfred</strong>
  22. </div>
  23. <!-- ... -->
  24. </div>
  25. </div>
  26. <!-- ... -->
  27. </div>

Applying conditionally

Hover, focus, and other states

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:absolute to only apply the absolute utility on hover.

  1. <div class="relative hover:absolute">
  2. <!-- ... -->
  3. </div>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

Breakpoints and media queries

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:absolute to apply the absolute utility at only medium screen sizes and above.

  1. <div class="relative md:absolute">
  2. <!-- ... -->
  3. </div>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.