Skip to content

Top / Right / Bottom / Left

控制定位元素放置的实用工具。

ClassProperties

基本用法

放置定位元素

使用 {top|right|bottom|left|inset}-{size} 实用工具来设置定位元素的水平或垂直位置。

01
02
03
04
05
06
07
08
09
html
<!-- Pin to top left corner -->
<div class="relative h-32 w-32 ...">
  <div class="absolute left-0 top-0 h-16 w-16 ...">01</div>
</div>

<!-- Span top edge -->
<div class="relative h-32 w-32 ...">
  <div class="absolute inset-x-0 top-0 h-16 ...">02</div>
</div>

<!-- Pin to top right corner -->
<div class="relative h-32 w-32 ...">
  <div class="absolute top-0 right-0 h-16 w-16 ...">03</div>
</div>

<!-- Span left edge -->
<div class="relative h-32 w-32 ...">
  <div class="absolute inset-y-0 left-0 w-16 ...">04</div>
</div>

<!-- Fill entire parent -->
<div class="relative h-32 w-32 ...">
  <div class="absolute inset-0 ...">05</div>
</div>

<!-- Span right edge -->
<div class="relative h-32 w-32 ...">
  <div class="absolute inset-y-0 right-0 w-16 ...">06</div>
</div>

<!-- Pin to bottom left corner -->
<div class="relative h-32 w-32 ...">
  <div class="absolute bottom-0 left-0 h-16 w-16 ...">07</div>
</div>

<!-- Span bottom edge -->
<div class="relative h-32 w-32 ...">
  <div class="absolute inset-x-0 bottom-0 h-16 ...">08</div>
</div>

<!-- Pin to bottom right corner -->
<div class="relative h-32 w-32 ...">
  <div class="absolute bottom-0 right-0 h-16 w-16 ...">09</div>
</div>

使用负值

要使用负的上/右/下/左值,需要在类名前加上破折号以将其转换为负值。

html
<div class="relative h-32 w-32 ...">
  <div class="absolute h-14 w-14 -left-4 -top-4 ..."></div>
</div>

使用逻辑属性

使用 start-*end-* 实用工具来设置 inset-inline-startinset-inline-end 逻辑属性,这些属性根据文本方向映射到左侧或右侧。

Left-to-right

Right-to-left

html
<div dir="ltr">
  <div class="relative h-32 w-32 ...">
    <div class="absolute h-14 w-14 top-0 start-0 ..."></div>
  </div>
<div>

<div dir="rtl">
  <div class="relative h-32 w-32 ...">
    <div class="absolute h-14 w-14 top-0 start-0 ..."></div>
  </div>
<div>

为了更好地控制,您还可以使用 LTR 和 RTL 修饰符,根据当前文本方向有条件地应用特定样式。

有条件地应用

悬停,焦点和其他状态

Tailwind 允许您使用变体修饰符在不同状态下有条件地应用实用程序类。例如,使用 hover:top-6 只在悬停时应用 top-6 实用程序。

html
<div class="top-4 hover:top-6">
  <!-- ... -->
</div>

要查看所有可用状态修饰符的完整列表,请查看悬停,焦点和其他状态文档。

断点和媒体查询

您还可以使用变体修饰符来定位媒体查询,如响应式断点、暗模式、偏好减少动作等。例如,使用 md:top-6 仅在中等屏幕尺寸及以上应用 top-6 实用程序。

html
<div class="top-4 md:top-6">
  <!-- ... -->
</div>

要了解更多信息,请查看有关响应式设计深色模式其他媒体查询修改器的文档。

使用自定义值

自定义您的主题

默认情况下,Tailwind 为默认间距比例尺提供了顶部/右侧/底部/左侧/插入工具, autofull 以及一些额外的分数数值的组合。

您可以通过编辑 tailwind.config.js 文件中的 theme.spacingtheme.extend.spacing 来自定义您的间距比例。

js
module.exports = {
  theme: {
    extend: {
      spacing: {		
        '3px': '3px',	
      }					
    }
  }
}

或者,您可以通过编辑您的 tailwind.config.js 文件中的 theme.insettheme.extend.inset 来自定义顶部/右侧/底部/左侧/插入比例。

js
module.exports = {
  theme: {
    extend: {
      inset: {			
        '3px': '3px',	
      }					
    }
  }
}

了解有关在主题自定义文档中自定义默认主题的更多信息。

任意值

如果您需要使用一次性 top/right/bottom/left 值,而这个值在您的主题中没有意义,可以使用方括号来使用任意值动态生成属性。

html
<div class="top-[3px]">
  <!-- ... -->
</div>

了解有关任意值支持的更多信息,请参阅任意值文档。

Released under the MIT License.