你不知道的css框架-Tailwind CSS

2,139 阅读17分钟

一.什么是Tailwind CSS

Tailwind CSS是一种实用工具-基于低级别的CSS框架。其他CSS框架(例如Bootstrap,Foundation,Bulma等)为你提供了各种预定义的组件(例如模式,按钮,警报,卡片)。但TailwindCSS可以做你自己组件,或者根据你的项目模型设计,换一种说法,你实际上是拥有自己开发组件的能力,然后可以利用自己选择的任何组件上的自定义功能,这意味着不再需要与该框架作斗争,而试图找出需要重写哪些类才能获得最初目标的结果。

二.为什么选择Tailwind CSS

2.1 大多数 CSS 框架包揽了太多工作

大多数css框架在需要定制化的时候,会不太友好,最常见是要写deep,或者important,费劲心力去覆盖内建的样式;而Tailwind是提供更基础的工具类,可以让我们在HTML源码上构建一个完全定制化的设计。

2.2 天生就支持响应式布局

Tailwind的每一个工具类都支持响应式布局,只需要使用更直观的类似 {screen}: 的命名前缀,这样可以很容易地注意到哪些是响应式类,同时还能保持原始类名的可识别性和完整性。

<div class="justify-start sm:justify-center md:justify-end lg:justify-between xl:justify-around ...">
  <!-- ... -->
</div>

2.3 组件友好

Tailwind提供了从重复模式中提取组件的工具,使得修改一处代码能够容易地更新多个实例

2.4 天生可定制

基本样式比如颜色、边框尺寸、字体粗细、间距、断电、阴影等等都可以定制。Tailwind 是基于 PostCSS(是一款使用插件去转换CSS的工具) 开发的,通过 JavaScript代码进行配置,这意味着你可以完全发挥真正的编程语言的能力。

2.5 控制文件体积

若使用默认配置,生成的 CSS 文件保持体积小并且功能完整,体积如下:

UncompressedMinifiedGzipBrotli
1996.1kb1599.8kb144.6kb37.6kb

2.6 浏览器支持

它的定位是一个高度可定制的基础层CSS框架,其实没有兼容性问题,根据不同浏览器支持的样式去写就可。

三.安装及配置

3.1 安装

# Using npm
npm install tailwindcss

# Using Yarn
yarn add tailwindcss

CDN的方式:

<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">

如果没有将Tailwind整合到构建过程中(使用CDN引入的方式),许多让Tailwind CSS变得很好的特性是不可用的。不能自定义Tailwind的默认主题,也不能使用任何像@apply这样的指令等

3.2 将 Tailwind 添加到你的 CSS 代码中

@tailwind base;

@tailwind components;

@tailwind utilities;

如果你正在使用postcss-import(或者一个在后台使用它的工具,比如Rails的Webpacker),使用我们的import而不是@tailwind指令来避免在导入任何你自己的附加文件时出现问题

@import "tailwindcss/base";

@import "tailwindcss/components";

@import "tailwindcss/utilities";

3.3 生成Tailwind CSS的配置文件

如果你想定制你的Tailwind安装,你可以在安装tailwindcss npm包时使用Tailwind CLI工具为你的项目生成一个配置文件

npx tailwindcss init

这将在项目的根目录中创建一个最小的tailwind.config.js文件

module.exports = {
  future: {
    // removeDeprecatedGapUtilities: true,
    // purgeLayersByDefault: true,
  },
  purge: [],
  theme: {
    extend: {},
  },
  variants: {},
  plugins: [],
}

可以在这个文件中修改一些默认配置(Theme、Variants、Plugins、Prefix、Important、Separator、corePlugins)例如:

theme: {
    screens: {
      'sm': '640px',
      'md': '768px',
      'lg': '1024px',
      'xl': '1280px',
    },
    fontFamily: {
      display: ['Gilroy', 'sans-serif'],
      body: ['Graphik', 'sans-serif'],
    },
    extend: {
      colors: {
        cyan: '#9cdbff',
      },
      margin: {
        '96': '24rem',
        '128': '32rem',
      },
    }
  },
  variants: {
    opacity: ['responsive', 'hover']
  },
  prefix: 'tw-', // 前缀

在使用prefix时需要注意的是,这个前缀是添加到每个实用程序名的开头,而不是添加到整个类名的,理解这一点很重要。这意味着具有响应性或状态前缀(如sm:或hover:)的类将仍然将响应性或状态前缀放在前面,而您自定义的前缀出现在冒号之后,例如:

<div class="tw-text-lg md:tw-text-xl tw-bg-red-500 hover:tw-bg-blue-500">
  <!-- -->
</div>

并且prefix前缀只添加到Tailwind生成的类中;不会在自己的自定义类中添加任何前缀。

未修改的配置见 默认配置

一般情况下,配置文件应尽可能少,只需要指定想定制的东西即可。如果在一些情况下需要构建一个完整的配置文件,其中包括所有的Tailwind默认配置,可使用以下命令:

npx tailwindcss init --full

3.4 用 tailwind 处理 css

以VUE Cli3.*为例,在postcss.config.js中配置:

module.exports = {
  plugins: [require("tailwindcss"), require("autoprefixer")],
};

其他环境下的配置参考官方文档Tailwind 安装

四.使用

4.1 函数与指令

4.1.1 @tailwind

使用@tailwind指令将Tailwind的基础(base)、组件(components)、工具(utilities)和屏幕(screens)样式插入到项目的CSS中

@tailwind base;

@tailwind components;

@tailwind utilities;

@tailwind screens;

4.1.2 @apply

使用@apply将任何现有的实用程序类内联到自己的定制CSS中。当需要在HTML中找到要提取到新组件的通用实用程序模式时,这非常有用。

其实就是整合tailwind提供的样式,提取为自己项目中需要用的样式。

.btn {
  @apply font-bold py-2 px-4 rounded;
}
.btn-blue {
  @apply bg-blue-500 text-white;
}
.btn-blue:hover {
  @apply bg-blue-700;
}

4.1.3 @variants

可以通过在@variant指令中包装它们的定义来生成hover, focus, active等样式

@variants focus, hover {
  .rotate-0 {
    transform: rotate(0deg);
  }
  .rotate-90 {
    transform: rotate(90deg);
  }
}

4.1.4 @responsive

可以生成一些响应式如渐变之类的样式

@responsive {
  .bg-gradient-brand {
    background-image: linear-gradient(blue, green);
  }
}

4.1.5 @screen

媒体查询

@media (min-width: 640px) {
  /* ... */
}

@screen sm {
  /* ... */
}

4.1.6 theme()

使用theme()函数来使用点符号访问Tailwind的配置值

.bg-blue {
  background-color: theme('colors.blue.500');
}

4.2 编写插件

在tailwind.config.js中引入plugin

// tailwind.config.js
const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addUtilities, addComponents, e, prefix, config }) {
      // Add your custom styles here
    }),
  ]
}

4.2.1 addUtilities() 注册新的utility样式

addUtilities函数允许在@tailwind实用程序指令处注册要输出的新样式(以及内置实用程序)。插件实用程序是按照注册的顺序输出的,在内置实用程序之后,所以如果一个插件的目标是与内置实用程序相同的任何属性,那么插件实用程序将优先。

// tailwind.config.js
const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addUtilities }) {
      const newUtilities = {
        '.skew-10deg': {
          transform: 'skewY(-10deg)',
        },
        '.skew-15deg': {
          transform: 'skewY(-15deg)',
        },
      }

      addUtilities(newUtilities)
    })
  ]
}

4.2.2 addComponents() 注册新的component样式

addComponents函数允许在@tailwind组件指令中注册输出的新样式。使用它来添加更多是在其他框架中经常看到的那种需要用实际开发过程中覆盖的预构建组件复杂类,如按钮、表单控件、警报等。

// tailwind.config.js
const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addComponents }) {
      const buttons = {
        '.btn': {
          padding: '.5rem 1rem',
          borderRadius: '.25rem',
          fontWeight: '600',
        },
        '.btn-blue': {
          backgroundColor: '#3490dc',
          color: '#fff',
          '&:hover': {
            backgroundColor: '#2779bd'
          },
        },
        '.btn-red': {
          backgroundColor: '#e3342f',
          color: '#fff',
          '&:hover': {
            backgroundColor: '#cc1f1a'
          },
        },
      }

      addComponents(buttons)
    })
  ]
}

4.2.3 addBase() 注册新的base样式

addBase函数允许在@tailwind基础指令中注册输出的新样式。使用它来添加基本字体样式、重置全局样式或者@font-face规则。

// tailwind.config.js
const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addBase, config }) {
      addBase({
        'h1': { fontSize: config('theme.fontSize.2xl') },
        'h2': { fontSize: config('theme.fontSize.xl') },
        'h3': { fontSize: config('theme.fontSize.lg') },
      })
    })
  ]
}

4.2.4 addVariant() 注册custom variants

addVariant函数允许您注册自己的自定义变体,可以像内置的hover, focus, active等变体一样使用。要添加新变体,请调用addVariant函数,传入自定义变体的名称和一个回调,该回调根据需要修改受影响的CSS规则。要添加新变体时,调用addVariant函数,传入自定义变体的名称和一个回调,该回调根据需要修改受影响的CSS规则。

const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addVariant, e }) {
      addVariant('disabled', ({ modifySelectors, separator }) => {
        modifySelectors(({ className }) => {
          return `.${e(`disabled${separator}${className}`)}:disabled`
        })
      })
    })
  ]
}

4.2.5 e() 用于转义要在类名中使用的字符串

如果所需插件生成的类包含用户提供的字符串,可以使用e函数来转义这些类名,以确保自动正确处理非标准字符。

例如,这个插件生成一组.rotate-{angle}工具,其中{angle}是用户提供的字符串。

// tailwind.config.js
const _ = require('lodash')
const plugin = require('tailwindcss/plugin')

module.exports = {
  theme: {
    rotate: {
      '1/4': '90deg',
      '1/2': '180deg',
      '3/4': '270deg',
    }
  },
  plugins: [
    plugin(function({ addUtilities, config, e }) {
      const rotateUtilities = _.map(config('theme.rotate'), (value, key) => {
        return {
          [`.${e(`rotate-${key}`)}`]: {
            transform: `rotate(${value})`
          }
        }
      })

      addUtilities(rotateUtilities)
    })
  ]
}

在使用时可直接用类名.rotate-1/4或rotate-1/2 等

4.2.6 prefix() 用于将用户配置的前缀手动应用到选择器的各个部分

如果只希望为某些类添加前缀,那么可以使用prefix函数来细粒度地控制何时配置前缀。

// tailwind.config.js
const plugin = require('tailwindcss/plugin')

module.exports = {
  prefix: 'tw-', // 前缀
  plugins: [
    plugin(function({ addComponents, prefix }) {
      addComponents({
        [`.existing-class > ${prefix('.new-class')}`]: {
          backgroundColor: '#fff',
        }
      })
    })
  ]
}

这段代码会生成的样式为

.existing-class > .tw-new-class {
  background-color: #fff;
}

4.2.7 theme()、variants()、config()

theme() - 用于查找用户主题配置中的值

variants() - 用于查找用户variants配置中的值

config() - 用于查找用户Tailwind配置中的值

config、theme和variant函数允您使用点符号从用户的Tailwind配置中请求一个值,如果该路径不存在,则提供一个默认值。

// tailwind.config.js
const _ = require('lodash')
const plugin = require('tailwindcss/plugin')

module.exports = {
  plugins: [
    plugin(function({ addComponents, theme }) {
      const screens = theme('screens', {})

      const mediaQueries = _.map(screens, width => {
        return {
          [`@media (min-width: ${width})`]: {
            '.container': {
              'max-width': width,
            },
          },
        }
      })

      addComponents([
        { '.container': { width: '100%' } },
        ...mediaQueries,
      ])
    })
  ]
}

这几个函数只是提供了一个快捷方式去访问用户的配置。例如config('theme.screens')theme('screens')是等价的。

4.2.8 postcss 用于直接使用PostCSS做一些操作

如果需要做的事情不仅仅是修改选择器(比如更改实际的规则声明,或者将规则包装在另一个at-rule中),那么需要使用容器实例。通过使用容器实例,可以遍历给定模块或@variant块中的所有规则,并使用标准的PostCSS API以喜欢的方式操作它们。

更多直接使用PostCSS的方法详见PostCss API

4.3 基础样式

1)标题、引号、段落等元素的所有默认边距为0;

2)所有标题元素都是完全无样式的,并且与普通文本具有相同的字体大小和字体粗细;

3)ol,ul的默认边距为0;

4)图像和其他被替换的元素(如svg、video、canvas和其他)默认为display: block;

5)所有元素的默认边框样式为:

*,
*::before,
*::after {
  border-width: 0;
  border-style: solid;
  border-color: theme('borderColor.default', currentColor);
}

可通过@apply改变这些默认样式,更多默认样式详见基础样式

4.4 常用样式

4.4.1 布局

1)container

ClassBreakpointProperties
.containerNonewidth: 100%;
.containersm (640px)max-width: 640px;
.containermd (768px)max-width: 768px;
.containerlg (1024px)max-width: 1024px;
.containerxl (1280px)max-width: 1280px;

container类将元素的最大宽度设置为与当前断点的最小宽度匹配。如果要使容器居中,使用.mx-auto

// 如现在是1440的屏幕,这个样式的效果就为1280的容器居中
<div class="container mx-auto">
  <!-- ... -->
</div>

2)Box Sizing

ClassProperties
.box-borderbox-sizing: border-box;
.box-contentbox-sizing: content-box;

3) Display

ClassProperties
.hiddendisplay: none;
.blockdisplay: block;
.flow-rootdisplay: flow-root;
.inline-blockdisplay: inline-block;
.inlinedisplay: inline;
.flexdisplay: flex;
.inline-flexdisplay: inline-flex;
.griddisplay: grid;
.inline-griddisplay: inline-grid;
.tabledisplay: table;
.table-captiondisplay: table-caption;
.table-celldisplay: table-cell;
.table-columndisplay: table-column;
.table-column-groupdisplay: table-column-group;
.table-footer-groupdisplay: table-footer-group;
.table-header-groupdisplay: table-header-group;
.table-row-groupdisplay: table-row-group;
.table-rowdisplay: table-row;

4)Floats

ClassProperties
.float-rightfloat: right;
.float-leftfloat: left;
.float-nonefloat: none;
.clearfix&::after {content: "";display:table;clear: both;}

5)Clear

ClassProperties
.clear-leftclear: left;
.clear-rightclear: right;
.clear-bothclear: both;
.clear-noneclear: none;

6)Overflow

ClassProperties
.overflow-autooverflow: auto;
.overflow-hiddenoverflow: hidden;
.overflow-visibleoverflow: visible;
.overflow-scrolloverflow: scroll;
.overflow-x-autooverflow-x: auto;
.overflow-y-autooverflow-y: auto;
.overflow-x-hiddenoverflow-x: hidden;
.overflow-y-hiddenoverflow-y: hidden;
.overflow-x-visibleoverflow-x: visible;
.overflow-y-visibleoverflow-y: visible;
.overflow-x-scrolloverflow-x: scroll;
.overflow-y-scrolloverflow-y: scroll;
.scrolling-touch-webkit-overflow-scrolling: touch;
.scrolling-auto-webkit-overflow-scrolling: auto;

7)Position

ClassProperties
.staticposition: static;
.fixedposition: fixed;
.absoluteposition: absolute;
.relativeposition: relative;
.stickyposition: sticky;

8)Top / Right / Bottom / Left

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;

9)Visibility

ClassProperties
.visiblevisibility: visible;
.invisiblevisibility: hidden;

10)Z-Index

ClassProperties
.z-0z-index: 0;
.z-10z-index: 10;
.z-20z-index: 20;
.z-30z-index: 30;
.z-40z-index: 40;
.z-50z-index: 50;
.z-autoz-index: auto;

默认情况下,Tailwind提供了6个数字z-index实用程序和一个自动实用程序。可以在配置中修改。

// tailwind.config.js
module.exports = {
  theme: {
    zIndex: {
      '0': 0,
-     '10': 10,
-     '20': 20,
-     '30': 30,
-     '40': 40,
-     '50': 50,
+     '25': 25,
+     '50': 50,
+     '75': 75,
+     '100': 100,
      'auto': 'auto',
    }
  }
}

4.4.2 FLEXBOX

1)Flex Direction

ClassProperties
.flex-rowflex-direction: row;
.flex-row-reverseflex-direction: row-reverse;
.flex-colflex-direction: column;
.flex-col-reverseflex-direction: column-reverse;

2)Flex Wrap

ClassProperties
.flex-no-wrapflex-wrap: nowrap;
.flex-wrapflex-wrap: wrap;
.flex-wrap-reverseflex-wrap: wrap-reverse;

3)Align Items

ClassProperties
.items-stretchalign-items: stretch;
.items-startalign-items: flex-start;
.items-centeralign-items: center;
.items-endalign-items: flex-end;
.items-baselinealign-items: baseline;

4)Align Content

ClassProperties
.content-startalign-content: flex-start;
.content-centeralign-content: center;
.content-endalign-content: flex-end;
.content-betweenalign-content: space-between;
.content-aroundalign-content: space-around;

5)Align Self

ClassProperties
.self-autoalign-self: auto;
.self-startalign-self: flex-start;
.self-centeralign-self: center;
.self-endalign-self: flex-end;
.self-stretchalign-self: stretch;

6)Justify Content

ClassProperties
.justify-startjustify-content: flex-start;
.justify-centerjustify-content: center;
.justify-endjustify-content: flex-end;
.justify-betweenjustify-content: space-between;
.justify-aroundjustify-content: space-around;

7)Flex

ClassProperties
.flex-initialflex: 0 1 auto;
.flex-1flex: 1 1 0%;
.flex-autoflex: 1 1 auto;
.flex-noneflex: none;

4.4.3 SPACING

1)Padding

ClassProperties
.p-0padding: 0;
.p-1padding: 0.25rem;
.p-2padding: 0.5rem;
.p-pxpadding: 1px;
.py-0padding-top: 0;padding-bottom: 0;
.py-1padding-top: 0.25rem;padding-bottom: 0.25rem;
.py-2padding-top: 0.5rem;padding-bottom: 0.5rem;
.py-pxpadding-top: 1px;padding-bottom: 1px;
.px-0padding-right: 0;padding-left: 0;
.px-1padding-right: 0.25rem;padding-left: 0.25rem;
.px-2padding-right: 0.5rem;padding-left: 0.5rem;
.px-pxpadding-right: 1px;padding-left: 1px;
.pt-0padding-top: 0;
.pt-1padding-top: 0.25rem;
.pt-2padding-top: 0.5rem;
.pt-pxpadding-top: 1px;
.pr-0padding-right: 0;
.pr-1padding-right: 0.25rem;
.pr-2padding-right: 0.5rem;
.pr-pxpadding-right: 1px;
.pb-0padding-bottom: 0;
.pb-1padding-bottom: 0.25rem;
.pb-2padding-bottom: 0.5rem;
.pb-pxpadding-bottom: 1px;
.pl-0padding-left: 0;
.pl-1padding-left: 0.25rem;
.pl-pxpadding-left: 1px;

2)Margin

ClassProperties
.m-0margin: 0;
.m-1margin: 0.25rem;
.m-2margin: 0.5rem;
.m-automargin: auto;
.m-pxmargin: 1px;
.-m-1margin: -0.25rem;
.-m-2margin: -0.5rem;
.-m-pxmargin: -1px;
.my-0margin-top: 0;margin-bottom: 0;
.my-1margin-top: 0.25rem;margin-bottom: 0.25rem;
.my-2margin-top: 0.5rem;margin-bottom: 0.5rem;
.my-automargin-top: auto;margin-bottom: auto;
.my-pxmargin-top: 1px;margin-bottom: 1px;
.-my-1margin-top: -0.25rem;margin-bottom: -0.25rem;
.-my-2margin-top: -0.5rem;margin-bottom: -0.5rem;
.-my-pxmargin-top: -1px;margin-bottom: -1px;
.mx-0margin-right: 0;margin-left: 0;
.mx-1margin-right: 0.25rem;margin-left: 0.25rem;
.mx-2margin-right: 0.5rem;margin-left: 0.5rem;
.mx-automargin-right: auto;margin-left: auto;
.mx-pxmargin-right: 1px;margin-left: 1px;
.-mx-1margin-right: -0.25rem;margin-left: -0.25rem;
.-mx-2margin-right: -0.5rem;margin-left: -0.5rem;
.-mx-pxmargin-right: -1px;margin-left: -1px;
.mt-0margin-top: 0;
.mt-1margin-top: 0.25rem;
.mt-2margin-top: 0.5rem;
.mt-automargin-top: auto;
.mt-pxmargin-top: 1px;
.-mt-1margin-top: -0.25rem;
.-mt-2margin-top: -0.5rem;
.-mt-pxmargin-top: -1px;
.mr-0margin-right: 0;
.mr-1margin-right: 0.25rem;
.mr-2margin-right: 0.5rem;
.mr-automargin-right: auto;
.mr-pxmargin-right: 1px;
.-mr-1margin-right: -0.25rem;
.-mr-2margin-right: -0.5rem;
.-mr-pxmargin-right: -1px;
.mb-0margin-bottom: 0;
.mb-1margin-bottom: 0.25rem;
.mb-2margin-bottom: 0.5rem;
.mb-automargin-bottom: auto;
.mb-pxmargin-bottom: 1px;
.-mb-1margin-bottom: -0.25rem;
.-mb-2margin-bottom: -0.5rem;
.-mb-pxmargin-bottom: -1px;
.ml-0margin-left: 0;
.ml-1margin-left: 0.25rem;
.ml-2margin-left: 0.5rem;
.ml-automargin-left: auto;
.ml-pxmargin-left: 1px;
.-ml-1margin-left: -0.25rem;
.-ml-2margin-left: -0.5rem;
.-ml-pxmargin-left: -1px;

4.4.4 SIZING

1)Width

ClassProperties
.w-0width: 0;
.w-1width: 0.25rem;
.w-2width: 0.5rem;
.w-autowidth: auto;
.w-pxwidth: 1px;
.w-1/2width: 50%;
.w-1/3width: 33.333333%;
.w-2/3width: 66.666667%;
.w-1/4width: 25%;
.w-fullwidth: 100%;
.w-screenwidth: 100vw;

2)Min-Width

ClassProperties
.min-w-0min-width: 0;
.min-w-fullmin-width: 100%;

3)Max-Width

ClassProperties
.max-w-xsmax-width: 20rem;
.max-w-smmax-width: 24rem;
.max-w-mdmax-width: 28rem;
.max-w-lgmax-width: 32rem;
.max-w-xlmax-width: 36rem;
.max-w-2xlmax-width: 42rem;
.max-w-fullmax-width: 100%;
.max-w-screen-smmax-width: 640px;
.max-w-screen-mdmax-width: 768px;
.max-w-screen-lgmax-width: 1024px;
.max-w-screen-xlmax-width: 1280px;
.max-w-nonemax-width: none;

4)Height同Width

5)Min-Height

ClassProperties
.min-h-0min-height: 0;
.min-h-fullmin-height: 100%;
.min-h-screenmin-height: 100vh;

6)Max-Height

ClassProperties
.max-h-fullmax-height: 100%;
.max-h-screenmax-height: 100vh;

4.4.5 排版

1)Font Size

ClassProperties
.text-xsfont-size: .75rem;
.text-smfont-size: .875rem;
.text-basefont-size: 1rem;
.text-lgfont-size: 1.125rem;
.text-xlfont-size: 1.25rem;
.text-2xlfont-size: 1.5rem;

2)Font Weight

ClassProperties
.font-hairlinefont-weight: 100;
.font-thinfont-weight: 200;
.font-lightfont-weight: 300;
.font-normalfont-weight: 400;
.font-mediumfont-weight: 500;
.font-semiboldfont-weight: 600;
.font-boldfont-weight: 700;
.font-extraboldfont-weight: 800;
.font-blackfont-weight: 900;

3)Text Alignment

ClassProperties
.text-lefttext-align: left;
.text-centertext-align: center;
.text-righttext-align: right;
.text-justifytext-align: justify;

4)Text Color

ClassProperties
.text-transparentcolor: transparent;
.text-currentcolor: currentColor;
.text-blackcolor: #000;
.text-whitecolor: #fff;
.text-gray-100color: #f7fafc;

5)Text Decoration

ClassProperties
.underlinetext-decoration: underline;
.line-throughtext-decoration: line-through;
.no-underlinetext-decoration: none;

6)Vertical Alignment

ClassProperties
.align-baselinevertical-align: baseline;
.align-topvertical-align: top;
.align-middlevertical-align: middle;
.align-bottomvertical-align: bottom;
.align-text-topvertical-align: text-top;
.align-text-bottomvertical-align: text-bottom;

7)Word Break

ClassProperties
.break-normalword-break: normal;overflow-wrap: normal
.break-wordsoverflow-wrap: break-word;
.break-allword-break: break-all;
.truncateoverflow: hidden;text-overflow: ellipsis;white-space: nowrap

4.4.6 背景

1)Background Color

ClassProperties
.bg-transparentbackground-color: transparent;
.bg-currentbackground-color: currentColor;
.bg-blackbackground-color: #000;
.bg-whitebackground-color: #fff;
.bg-gray-100background-color: #f7fafc;
.bg-gray-900background-color: #1a202c;
.bg-red-100background-color: #fff5f5;
.bg-red-200background-color: #fed7d7;

2)Background Opacity

ClassProperties
.bg-opacity-0--bg-opacity: 0;
.bg-opacity-25--bg-opacity: 0.25;
.bg-opacity-50--bg-opacity: 0.5;
.bg-opacity-75--bg-opacity: 0.75;
.bg-opacity-100--bg-opacity: 1;

3)Background Repeat

ClassProperties
.bg-repeatbackground-repeat: repeat;
.bg-no-repeatbackground-repeat: no-repeat;
.bg-repeat-xbackground-repeat: repeat-x;
.bg-repeat-ybackground-repeat: repeat-y;
.bg-repeat-roundbackground-repeat: round;
.bg-repeat-spacebackground-repeat: space;

4.4.7 BORDERS

1)Border Radius

ClassProperties
.rounded-noneborder-radius: 0;
.rounded-smborder-radius: 0.125rem;
.roundedborder-radius: 0.25rem;
.rounded-mdborder-radius: 0.375rem;
.rounded-lgborder-radius: 0.5rem;
.rounded-fullborder-radius: 9999px;
.rounded-t-smborder-top-left-radius: 0.125rem;border-top-right-radius: 0.125rem;
.rounded-r-smborder-top-right-radius: 0.125rem;border-bottom-right-radius: 0.125rem;

2)Border Width

ClassProperties
.borderborder-width: 1px;
.border-0border-width: 0;
.border-2border-width: 2px;
.border-tborder-top-width: 1px;
.border-rborder-right-width: 1px;
.border-bborder-bottom-width: 1px;
.border-lborder-left-width: 1px;
.border-t-0border-top-width: 0;

3)Border Color

ClassProperties
.border-transparentborder-color: transparent;
.border-currentborder-color: currentColor; .border-blackborder-color: #000;
.border-whiteborder-color: #fff;
.border-gray-100border-color: #f7fafc;

4)Border Style

ClassProperties
.border-solidborder-style: solid;
.border-dashedborder-style: dashed;
.border-dottedborder-style: dotted;
.border-doubleborder-style: double;
.border-noneborder-style: none;

4.4.8 特效

1)Box Shadow

ClassProperties
.shadow-xsbox-shadow: 0 0 0 1px rgba(0, 0, 0, 0.05);
.shadow-smbox-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
.shadowbox-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
.shadow-mdbox-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
.shadow-lgbox-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
.shadow-xlbox-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
.shadow-2xlbox-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
.shadow-innerbox-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
.shadow-outlinebox-shadow: 0 0 0 3px rgba(66, 153, 225, 0.5);
.shadow-nonebox-shadow: none;
  1. Opacity Class | Properties -------------| --- .opacity-100 | opacity: 1; .opacity-75 | opacity: .75; .opacity-50 | opacity: .5; .opacity-25 | opacity: .25; .opacity-0 | opacity: 0;

4.4.9 TRANSITIONS

ClassProperties
.transition-transformtransition-property: transform;
.duration-75transition-duration: 75ms;
.duration-100transition-duration: 100ms;
.ease-lineartransition-timing-function: linear;
.ease-intransition-timing-function: cubic-bezier(0.4, 0, 1, 1);
.ease-outtransition-timing-function: cubic-bezier(0, 0, 0.2, 1);
.ease-in-outtransition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
.delay-75transition-delay: 75ms;
.delay-100transition-delay: 100ms;
.delay-150transition-delay: 150ms;

4.4.10 TRANSFORMS

1)Scale

ClassProperties
.scale-0--transform-scale-x: 0;--transform-scale-y: 0;
.scale-50--transform-scale-x: .5;--transform-scale-y: .5;
.scale-75--transform-scale-x: .75;--transform-scale-y: .75;
.scale-x-0--transform-scale-x: 0;
.scale-x-50--transform-scale-x: .5;
.scale-x-75--transform-scale-x: .75;
.scale-y-0--transform-scale-y: 0;
.scale-y-50--transform-scale-y: .5;
.scale-y-75--transform-scale-y: .75;

2)Rotate

ClassProperties
.rotate-0--transform-rotate: 0;
.rotate-45--transform-rotate: 45deg;
.rotate-90--transform-rotate: 90deg;
.rotate-180--transform-rotate: 180deg;
.-rotate-180--transform-rotate: -180deg;
.-rotate-90--transform-rotate: -90deg;
.-rotate-45--transform-rotate: -45deg;

3)Translate

ClassProperties
.translate-x-0--transform-translate-x: 0;
.translate-x-1--transform-translate-x: 0.25rem;
.translate-x-2--transform-translate-x: 0.5rem;
.translate-x-px--transform-translate-x: 1px;
.-translate-x-1--transform-translate-x: -0.25rem;
.-translate-x-2--transform-translate-x: -0.5rem;
.-translate-x-px--transform-translate-x: -1px;
.-translate-x-full--transform-translate-x: -100%;
.-translate-x-1/2--transform-translate-x: -50%;
.translate-x-1/2--transform-translate-x: 50%;
.translate-x-full--transform-translate-x: 100%;
.translate-y-0--transform-translate-y: 0;
.translate-y-1--transform-translate-y: 0.25rem;
.translate-y-px--transform-translate-y: 1px;
.-translate-y-1--transform-translate-y: -0.25rem;
.-translate-y-2--transform-translate-y: -0.5rem;
.-translate-y-px--transform-translate-y: -1px;
.-translate-y-full--transform-translate-y: -100%;
.-translate-y-1/2--transform-translate-y: -50%;
.translate-y-1/2--transform-translate-y: 50%;
.translate-y-full--transform-translate-y: 100%;

4.4.11 INTERACTIVITY

1)Cursor

ClassProperties
.cursor-autocursor: auto;
.cursor-defaultcursor: default;
.cursor-pointercursor: pointer;
.cursor-waitcursor: wait;
.cursor-textcursor: text;
.cursor-movecursor: move;
.cursor-not-allowedcursor: not-allowed;

2)User Select

ClassProperties
.select-noneuser-select: none;
.select-textuser-select: text;
.select-alluser-select: all;
.select-autouser-select: auto;

五.小结

本文只是简单介绍了一下Tailwind CSS及常用的样式,要了解更多请参见官方文档哦~