Top / Right / Bottom / Left

Utilities for controlling the placement of positioned elements.

Class reference

ClassProperties
.inset-0top: 0;right: 0;bottom: 0;left: 0;
.inset-y-0top: 0;bottom: 0;
.inset-x-0right: 0;left: 0;
.top-0top: 0;
.right-0right: 0;
.bottom-0bottom: 0;
.left-0left: 0;
.inset-autotop: auto;right: auto;bottom: auto;left: auto;
.inset-y-autotop: auto;bottom: auto;
.inset-x-autoleft: auto;right: auto;
.top-autotop: auto;
.bottom-autobottom: auto;
.left-autoleft: auto;
.right-autoright: auto;

Usage

Use the .{top|right|bottom|left|inset}-0 utilities to anchor absolutely positioned elements against any of the edges of the nearest positioned parent.

Combined with Tailwind’s padding and margin utilities, you’ll probably find that these are all you need to precisely control absolutely positioned elements.

Top / Right / Bottom / Left - 图1

  1. <!-- Span top edge -->
  2. <div class="relative h-24 w-24 bg-gray-400">
  3. <div class="absolute inset-x-0 top-0 h-8 bg-gray-700"></div>
  4. </div>
  5. <!-- Span right edge -->
  6. <div class="relative h-24 w-24 bg-gray-400">
  7. <div class="absolute inset-y-0 right-0 w-8 bg-gray-700"></div>
  8. </div>
  9. <!-- Span bottom edge -->
  10. <div class="relative h-24 w-24 bg-gray-400">
  11. <div class="absolute inset-x-0 bottom-0 h-8 bg-gray-700"></div>
  12. </div>
  13. <!-- Span left edge -->
  14. <div class="relative h-24 w-24 bg-gray-400">
  15. <div class="absolute inset-y-0 left-0 bg-gray-700"></div>
  16. </div>
  17. <!-- Fill entire parent -->
  18. <div class="relative h-24 w-24 bg-gray-400">
  19. <div class="absolute inset-0 bg-gray-700"></div>
  20. </div>
  21. <!-- Pin to top left corner -->
  22. <div class="relative h-24 w-24 bg-gray-400">
  23. <div class="absolute left-0 top-0 h-8 w-8 bg-gray-700"></div>
  24. </div>
  25. <!-- Pin to top right corner -->
  26. <div class="relative h-24 w-24 bg-gray-400">
  27. <div class="absolute top-0 right-0 h-8 w-8 bg-gray-700"></div>
  28. </div>
  29. <!-- Pin to bottom right corner -->
  30. <div class="relative h-24 w-24 bg-gray-400">
  31. <div class="absolute bottom-0 right-0 h-8 w-8 bg-gray-700"></div>
  32. </div>
  33. <!-- Pin to bottom left corner -->
  34. <div class="relative h-24 w-24 bg-gray-400">
  35. <div class="absolute bottom-0 left-0 h-8 w-8 bg-gray-700"></div>
  36. </div>

Responsive

To position an element only at a specific breakpoint, add a {screen}: prefix to any existing positioning utility. For example, adding the class md:inset-y-0 to an element would apply the inset-y-0 utility at medium screen sizes and above.

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

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

Top / Right / Bottom / Left - 图2

Customizing

Top / Right / Bottom / Left scale

By default Tailwind only provides 0 and auto top/right/bottom/left/inset utilities. You can change, add, or remove these by editing the theme.inset section of your tailwind.config.js file.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. inset: {
  5. '0': 0,
  6. - auto: 'auto',
  7. + '1/2': '50%',
  8. }
  9. }
  10. }

Negative values

If you’d like to add any negative top/right/bottom/left classes that take the same form as Tailwind’s negative margin classes, prefix the keys in your config file with a dash:

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. margin: {
  5. + '-16': '-4rem',
  6. }
  7. }
  8. }

Tailwind is smart enough to generate classes like -top-16 when it sees the leading dash, not top—16 like you might expect.

Responsive and pseudo-class variants

By default, only responsive variants are generated for top, right, bottom, left, and inset utilities.

You can control which variants are generated for the top, right, bottom, left, and inset utilities by modifying the inset 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. - inset: ['responsive'],
  6. + inset: ['responsive', 'hover', 'focus'],
  7. }
  8. }

Disabling

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

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