1. 边框
  2. 边框宽度

Quick reference

Class
Properties
border-0border-width: 0px;
border-2border-width: 2px;
border-4border-width: 4px;
border-8border-width: 8px;
borderborder-width: 1px;
border-x-0border-left-width: 0px; border-right-width: 0px;
border-x-2border-left-width: 2px; border-right-width: 2px;
border-x-4border-left-width: 4px; border-right-width: 4px;
border-x-8border-left-width: 8px; border-right-width: 8px;
border-xborder-left-width: 1px; border-right-width: 1px;
border-y-0border-top-width: 0px; border-bottom-width: 0px;
border-y-2border-top-width: 2px; border-bottom-width: 2px;
border-y-4border-top-width: 4px; border-bottom-width: 4px;
border-y-8border-top-width: 8px; border-bottom-width: 8px;
border-yborder-top-width: 1px; border-bottom-width: 1px;
border-s-0border-inline-start-width: 0px;
border-s-2border-inline-start-width: 2px;
border-s-4border-inline-start-width: 4px;
border-s-8border-inline-start-width: 8px;
border-sborder-inline-start-width: 1px;
border-e-0border-inline-end-width: 0px;
border-e-2border-inline-end-width: 2px;
border-e-4border-inline-end-width: 4px;
border-e-8border-inline-end-width: 8px;
border-eborder-inline-end-width: 1px;
border-t-0border-top-width: 0px;
border-t-2border-top-width: 2px;
border-t-4border-top-width: 4px;
border-t-8border-top-width: 8px;
border-tborder-top-width: 1px;
border-r-0border-right-width: 0px;
border-r-2border-right-width: 2px;
border-r-4border-right-width: 4px;
border-r-8border-right-width: 8px;
border-rborder-right-width: 1px;
border-b-0border-bottom-width: 0px;
border-b-2border-bottom-width: 2px;
border-b-4border-bottom-width: 4px;
border-b-8border-bottom-width: 8px;
border-bborder-bottom-width: 1px;
border-l-0border-left-width: 0px;
border-l-2border-left-width: 2px;
border-l-4border-left-width: 4px;
border-l-8border-left-width: 8px;
border-lborder-left-width: 1px;

基本用法

¥Basic usage

所有面

¥All sides

使用 borderborder-0border-2border-4border-8 工具设置元素所有边的边框宽度。

¥Use the border, border-0, border-2, border-4, or border-8 utilities to set the border width for all sides of an element.

border

border-2

border-4

border-8

<div class="border border-indigo-600 ..."></div>
<div class="border-2 border-indigo-600 ..."></div>
<div class="border-4 border-indigo-600 ..."></div>
<div class="border-8 border-indigo-600 ..."></div>

个别面

¥Individual sides

使用 border-*border-*-0border-*-2border-*-4border-*-8 工具设置元素一侧的边框宽度。

¥Use the border-*, border-*-0, border-*-2, border-*-4, or border-*-8 utilities to set the border width for one side of an element.

border-t-4

border-r-4

border-b-4

border-l-4

<div class="border-t-4 border-indigo-500 ..."></div>
<div class="border-r-4 border-indigo-500 ..."></div>
<div class="border-b-4 border-indigo-500 ..."></div>
<div class="border-l-4 border-indigo-500 ..."></div>

水平端和垂直端

¥Horizontal and vertical sides

使用 border-x-*border-y-* 工具同时设置元素两侧的边框宽度。

¥Use the border-x-* and border-y-* utilities to set the border width on two sides of an element at the same time.

border-x-4

border-y-4

<div class="border-x-4 border-indigo-500 ..."></div>
<div class="border-y-4 border-indigo-500 ..."></div>

元素之间

¥Between elements

你还可以使用 divide-x-*divide-y-* 宽度工具以及 divide-* 颜色工具在子元素之间添加边框。

¥You can also add borders between child elements using the divide-x-* and divide-y-* width utilities along with the divide-* color utilities.

01
02
03
<div class="divide-y divide-slate-200 ...">
  <div>01</div>
  <div>02</div>
  <div>03</div>
</div>

分割宽度分割颜色 文档中了解更多信息。

¥Learn more in the Divide Width and Divide Color documentation.

使用逻辑属性

¥Using logical properties

使用 border-s-*border-e-* 工具设置 border-inline-start-widthborder-inline-end-width 逻辑属性,它们根据文本方向映射到左边框或右边框。

¥Use the border-s-* and border-e-* utilities to set the border-inline-start-width and border-inline-end-width logical properties, which map to either the left or right border based on the text direction.

Left-to-right

Right-to-left

<div dir="ltr">
  <div class="border-s-4 ..."></div>
<div>

<div dir="rtl">
  <div class="border-s-4 ..."></div>
<div>

要获得更多控制,你还可以使用 LTR 和 RTL 修饰符 根据当前文本方向有条件地应用特定样式。

¥For more control, you can also use the LTR and RTL modifiers to conditionally apply specific styles depending on the current text direction.

不使用预检

¥Using without Preflight

如果你的项目中有 禁用预检,则每次使用 border-width 工具之一时,你都需要包含一个 边框样式 工具以使边框实际生效:

¥If you’ve disabled Preflight in your project, you’ll need to include a border style utility any time you use one of the border-width utilities for the border to actually take effect:

<div class="border-4 border-indigo-500 ...">
<div class="border-4 border-solid border-indigo-500 ...">
  <!-- ... -->
</div>

这是因为浏览器默认将大多数元素的 border-style 设置为 none,因此仅添加 border-width 不足以渲染边框。

¥This is because browsers set the border-style of most elements to none by default, so adding a border-width on its own isn’t enough to render a border.

Preflight 应用全局 边框重置,将 border-style 设置为 solid,将 border-width 设置为 0,这使得在使用 Preflight 的项目中仅使用 border-width 工具就可以为元素添加边框。

¥Preflight applies a global border reset that sets border-style to solid and border-width to 0, which is what makes it possible to add a border to an element using just a border-width utility in projects using Preflight.


有条件地应用

悬停、聚焦和其他状态

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

<div class="border-2 hover:border-t-4">
  <!-- ... -->
</div>

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

断点和媒体查询

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

<div class="border-2 md:border-t-4">
  <!-- ... -->
</div>

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


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 提供五个 border-width 工具,并且每侧(水平、垂直、顶部、右侧、底部和左侧)提供相同数量的工具。你可以通过编辑 Tailwind 配置的 theme.borderWidth 部分来更改、添加或删除它们。

¥By default, Tailwind provides five border-width utilities, and the same number of utilities per side (horizontal, vertical, top, right, bottom, and left). You change, add, or remove these by editing the theme.borderWidth section of your Tailwind config.

tailwind.config.js
module.exports = {
  theme: {
    borderWidth: {
      DEFAULT: '1px',
      '0': '0',
      '2': '2px',
      '3': '3px',
      '4': '4px',
      '6': '6px',
      '8': '8px',
    }
  }
}

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

¥Learn more about customizing the default theme in the theme customization documentation.

任意值

¥Arbitrary values

If you need to use a one-off border-width value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<div class="border-t-[3px]">
  <!-- ... -->
</div>

Learn more about arbitrary value support in the arbitrary values documentation.