指令

你可以通过指令与 CSS 相结合的方式来使用可用的工具类 (utilities)。

@apply

@apply 使用在你 style 块中同一行的、一些已存在的工具类上。

当你在 HTML 中发现一个通用工具类组合时, 这个指令非常有用。你会喜欢把这些通用的工具类组合提取到一个新的组件中。

Generated CSS
.btn {
  border-radius: 0.25rem;
  font-weight: 700;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  padding-left: 1rem;
  padding-right: 1rem;
}
.btn-blue {
  --tw-bg-opacity: 1;
  background-color: rgba(59, 130, 246, var(--tw-bg-opacity));
  --tw-text-opacity: 1;
  color: rgba(255, 255, 255, var(--tw-text-opacity));
  padding-top: 1rem;
}
.btn-blue:hover {
  --tw-bg-opacity: 1;
  background-color: rgba(29, 78, 216, var(--tw-bg-opacity));
}

如果你希望将 apply 使用在一个已存在的 class 上,并且使它 !important,只需要将 !important 加到声明的末尾即可:

Generated CSS
.btn {
  border-radius: 0.25rem !important;
  font-weight: 700 !important;
  padding-top: 0.5rem !important;
  padding-bottom: 0.5rem !important;
  padding-left: 1rem !important;
  padding-right: 1rem !important;
}

@variants

你可以通过把自己的工具类定义包装在 @variants中,以此来生成带有 屏幕可变修饰,状态可变修饰,主题可变修饰 的工具类。

Generated CSS
.rotate-0:focus {
  transform: rotate(0deg);
}
.rotate-90:focus {
  transform: rotate(90deg);
}
.rotate-0:hover {
  transform: rotate(0deg);
}
.rotate-90:hover {
  transform: rotate(90deg);
}
.dark .bg-color {
  background-color: #1c1c1e;
}

@screen

@screen 指令允许你创建媒体查询,通过名称来引用断点,以此来取代通过复制你 CSS 里面的值来实现。

Generated CSS
@media (min-width: 640px) {
  .custom {
    font-size: 1.125rem;
    line-height: 1.75rem;
  }
}

@layer

@layer 指令用来确认每个 class 的排序。合法的层级为 基础 (base), 组件 (components)工具类 (utilities)

Generated CSS
.components {
  --tw-bg-opacity: 1;
  background-color: rgba(239, 68, 68, var(--tw-bg-opacity));
}
.utilities {
  max-width: 768px;
}
base {
  margin-left: auto;
}
.normal {
  margin-right: auto;
}

theme()

theme() 函数允许你通过 . 运算符来获取你设置的值。

Generated CSS
.btn-blue {
  background-color: #3b82f6;
}