功能类优先

使用功能类来构建复杂的css,而不是编写新的css。
好处:使用传统方法,每次添加新功能时 CSS 文件都会变大。使用功能类,所有内容都是可重用的,因此您几乎不需要编写新的CSS。

响应式

Tailwind 中的每个功能类都可以有条件的应用于不同的断点,这使得您可以轻松的构建复杂的响应式界面而不用离开 HTML。
根据常用的设备分辨率方案,默认内置了 5 个断点:

断点前缀 最小宽度 CSS
sm 640px @media (min-width: 640px) { … }
md 768px @media (min-width: 768px) { … }
lg 1024px @media (min-width: 1024px) { … }
xl 1280px @media (min-width: 1280px) { … }
2xl 1536px @media (min-width: 1536px) { … }

要添加一个仅在特定断点生效的功能类,只需要在该功能类前加上断点名称,后面跟 : 字符。

  1. <!-- Width of 16 by default, 32 on medium screens, and 48 on large screens -->
  2. <img class="w-16 md:w-32 lg:w-48" src="...">

悬停、焦点和其它状态

悬停、焦点和其它状态 参考文档

Hover

添加 hover: 前缀,以在 hover 状态时应用一个功能类。
image.png
自定义hover:除一些默认情况(如backgroundColor,textColor)等默认情况下,可以使用自定义hover,来为某些插件启用hover变体。
可以在 tailwind.config.js 文件中的 variants 部分控制是否为某个插件启用 hover 变体:

// tailwind.config.js
module.exports = {
    // ...
    variants: {
        extend: {
            padding: ['hover'], // 为padding开启hover变体
        }
    },
}

Group-hover

如果您需要当鼠标悬停在一个指定的父元素上时对其子元素设置样式,给父元素添加 group 类,并且为子元素的功能类添加 group-hover: 前缀。
image.png
同上,在 tailwind.config.js 文件中的 variants 部分控制是否为某个插件启用 group-hover 变体:

// tailwind.config.js
module.exports = {
  // ...
  variants: {
    extend: {
            //给分割线颜色(用于控制元素之间边框颜色的功能类。)开启 group-hover变体
      divideColor: ['group-hover'],
    }
  },
}

Focus

添加 focus: 前缀,以在 focus 状态时应用一个功能类。
image.png
同上,除默认情况下,可以在tailwind.config.js 文件中的 variants 部分控制是否为某个插件启用 focus 变体:

// tailwind.config.js
module.exports = {
  // ...
  variants: {
    extend: {
      maxHeight: ['focus'],//开启maxHeight的focus变体
    }
  },
}

Group-focus

image.png
默认情况下,所有核心插件都没有启用该 group-focus 变体。
可以在 tailwind.config.js 文件中的 variants 部分控制是否为某个插件启用 group-focus 变体:

// tailwind.config.js
module.exports = {
  // ...
  variants: {
    extend: {
      backgroundColor: ['group-focus'],
    }
  },
}

Active

添加 active: 前缀,以在元素处于 active 状态时应用某个功能。
image.png
同上

// tailwind.config.js
module.exports = {
  // ...
  variants: {
    extend: {
      backgroundColor: ['active'], //开启backgroundColor的active变体
    }
  },
}

等等其余状态见官方文档

组件提取

创建模板片断或者 JavaScript 组件

通常最好将 UI 的可重用部分提取到模板片断或 JavaScript 组件中,而不是编写自定义 CSS 类。
通过为模板创建一个单一源,您可以继续使用功能类,而不会因为在多个地方复制相同的类而造成任何的维护负担。

使用 Tailwind 的 @apply 功能创建抽象的 CSS 类。

image.png

<template>
<!-- Using utilities -->
    <button class="py-2 px-4 font-semibold rounded-lg shadow-md text-white bg-green-500 hover:bg-green-700">
        Click me
    </button>

    <br/><br/>

<!-- Extracting classes using @apply -->
    <button class="btn btn-green">
        Button
        <span class="ddd">ddd</span>
    </button>
</template>
<style>
    .btn {
    @apply py-2 px-4 font-semibold rounded-lg shadow-md;
  }
  .btn-green {
    @apply text-white bg-green-500 hover:bg-green-700;
    color:red;
  }
  .btn-green .ddd{
    @apply   text-purple-600
  }
</style>

添加基础样式

如果您只想对 html 或者 body 元素应用某种全局样式,只需将现有类添加到 HTML 中的那些元素,而不是编写新的 CSS 。
如果要将某些基本样式应用于特定元素,最容易的方法是将其简单地添加到 CSS 中。
通过使用 @layer 指令,Tailwind 将自动将这些样式移到 @tailwind base 的同一位置,以避免出现一些意外问题。
image.png

<template>
  <div id="app">
    <h1>sdds </h1>
    <h2>sdds </h2>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
  /* 
  @layer base 用于更新基础样式
  通过使用 @layer 指令,Tailwind 将自动将这些样式移到 @tailwind base 的同一位置,以避免出现一些意外问题。
  (最好使用 @apply 或者 theme() 来定义这些样式,以避免意外偏离您的设计系统。如下)
  */
  @layer base {
    h1 {
      @apply text-purple-600;
            font-size:20px;
    }
  }
</style>

函数与指令

指令

@tailwind

使用 @tailwind 指令向您的 CSS 添加 Tailwind 的 base, components, utilities 和 screens 样式。

/**
    通过这个插件注入 Tailwind 的基础样式和注册的任何基础样式。
 */
@tailwind base;

/**
 通过这个插件注册 Tailwind 的组件类和任何组件类。
 */
@tailwind components;

/**
 通过这个插件注入 Tailwind 的实用程序类和注册的任何实用程序类。
 */
@tailwind utilities;

/**
 使用此指令控制 Tailwind 在何处注入响应每个公用效果的变化。
 如果省略,Tailwind 将把这些类追加到默认情况下的样式表。
 */
@tailwind screens;

@apply

使用 @apply 将任何现存的功能类内联到您的自定义 CSS 中。
当想要提取到一个通用的功能class时,这非常有用。

.btn {
  @apply py-2;
}
//可以将 @apply 声明与常规 CSS 声明混合使用
.btn2 {
  @apply font-bold py-2 px-4 rounded;
    transform: translateY(-1px);
}
//可以使用多个 @apply 语句,输出将根据@apply的顺序输出结果。
.btn-blue {
  @apply bg-blue-500 hover:bg-blue-700 text-white;
    @apply p-4;
}

@layer

使用 @layer 指令告诉 Tailwind 一组自定义样式应该属于哪个 “bucket”。可用的层有 base, components 和 utilities。
Tailwind会自动将 @layer 指令中的所有 CSS 移至与相应 @tailwind 规则相同的位置,因此您不必担心以特定顺序编写 CSS 来避免特定性问题。

@tailwind base;
@tailwind components;
@tailwind utilities;

//下面的 h1 h2 的样式将归于 base模块。
@layer base {
  h1 {
    @apply text-2xl;
  }
  h2 {
    @apply text-xl;
  }
}
//下面的 .btn-blue 的样式将归于 components模块。
@layer components {
  .btn-blue {
    @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
  }
}

@variants

您可以通过在 @variants 指令中声明自己的功能类来生成他们的 responsive, hover, focus, active 及其它 变体。

//通过@variants指令给.rotate-0 .rotate-90增加focus hover 变体
@variants focus, hover {
  .rotate-0 {
    transform: rotate(0deg);
  }
  .rotate-90 {
    transform: rotate(90deg);
  }
}

如上将生成如下的css

.rotate-0 {
  transform: rotate(0deg);
}
.rotate-90 {
  transform: rotate(90deg);
}

.focus\:rotate-0:focus {
  transform: rotate(0deg);
}
.focus\:rotate-90:focus {
  transform: rotate(90deg);
}

.hover\:rotate-0:hover {
  transform: rotate(0deg);
}
.hover\:rotate-90:hover {
  transform: rotate(90deg);
}

函数

theme()

使用 theme() 函数可以通过点符号来获取 Tailwind 配置的值。

//使用theme函数获取colors-blue-500对应的css值。
.btn-blue {
  background-color: theme('colors.blue.500');
}