Position

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

Class reference

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

Static

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 bg-gray-600">
  2. Static parent
  3. <div class="absolute bottom-0 left-0 bg-gray-800">
  4. Absolute child
  5. </div>
  6. </div>

Relative

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

Position - 图2

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

Absolute

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.

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="relative bg-gray-400">
  2. Relative parent
  3. <div class="static bg-gray-600">
  4. Static parent
  5. <div class="absolute top-0 right-0 bg-gray-800">
  6. Absolute child
  7. </div>
  8. <div class="bg-gray-400 inline-block">
  9. Static sibling
  10. </div>
  11. </div>
  12. </div>

Fixed

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

Position - 图4

  1. <div class="bg-gray-400 pt-16">
  2. <div class="fixed bg-gray-600">
  3. Fixed child
  4. <div class="absolute top-0 right-0 bg-gray-800">
  5. Absolute child
  6. </div>
  7. </div>
  8. Scroll me!
  9. Lorem ipsum...
  10. </div>

Sticky

Position - 图5

Note that sticky positioning is not supported in IE11.

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.

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

Position - 图6

  1. <div>
  2. <div class="sticky top-0 ...">Sticky Heading 1</div>
  3. <p class="py-4">Quisque cursus...</p>
  4. </div>
  5. <div>
  6. <div class="sticky top-0 ...">Sticky Heading 2</div>
  7. <p class="py-4">Integer lacinia...</p>
  8. </div>
  9. <div>
  10. <div class="sticky top-0 ...">Sticky Heading 3</div>
  11. <p class="py-4">Nullam mauris...</p>
  12. </div>
  13. <!-- etc. -->

Responsive

To change how an element is positioned only at a specific breakpoint, add a {screen}: prefix to any existing position utility. For example, adding the class md:absolute to an element would apply the absolute utility at medium screen sizes and above.

For more information about Tailwind’s responsive design features, check out the Responsive Design documentation.

Position - 图7

all

Position - 图8

sm

Position - 图9

md

Position - 图10

lg

Position - 图11

xl

  1. <div class="relative h-32 bg-gray-400 p-4">
  2. <div class="inset-x-0 bottom-0 relative sm:absolute md:fixed lg:absolute xl:relative"></div>
  3. </div>

Responsive element

Customizing

Responsive and pseudo-class variants

By default, only responsive variants are generated for position utilities.

You can control which variants are generated for the position utilities by modifying the position property in the variants section of your tailwind.config.js file.

For example, this config will also generate hover and focus variants:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - position: ['responsive'],
  6. + position: ['responsive', 'hover', 'focus'],
  7. }
  8. }

Disabling

If you don’t plan to use the position utilities in your project, you can disable them entirely by setting the position property to false in the corePlugins section of your config file:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + position: false,
  6. }
  7. }

← Overscroll Behavior   Top / Right / Bottom / Left →