- 入门
- 核心概念
- 定制
- Base Styles
- 布局
- Flexbox & Grid
- Flex Basis
- Flex Direction
- Flex Wrap
- Flex
- Flex Grow
- Flex Shrink
- Order
- Grid Template Columns
- Grid Column Start / End
- Grid Template Rows
- Grid Row Start / End
- Grid Auto Flow
- Grid Auto Columns
- Grid Auto Rows
- Gap
- Justify Content
- Justify Items
- Justify Self
- Align Content
- Align Items
- Align Self
- Place Content
- Place Items
- Place Self
- 间隔
- 尺寸
- 排版
- Font Family
- Font Size
- Font Smoothing
- Font Style
- Font Weight
- Font Variant Numeric
- Letter Spacing
- Line Clamp
- Line Height
- List Style Image
- List Style Position
- List Style Type
- Text Align
- Text Color
- Text Decoration
- Text Decoration Color
- Text Decoration Style
- Text Decoration Thickness
- Text Underline Offset
- Text Transform
- Text Overflow
- Text Wrap
- Text Indent
- Vertical Align
- Whitespace
- Word Break
- Hyphens
- Content
- 背景
- 边框
- Effects
- Filters
- 表格 Tables
- Transitions & Animation
- Transforms
- Interactivity
- SVG
- Accessibility
- 其他
- Install Tailwind CSS using PostCSS
- Framework Guides
- Try Tailwind CSS using the Play CDN
- Install Tailwind CSS with Next.js
- Install Tailwind CSS with Laravel
- Install Tailwind CSS with Vite
- Install Tailwind CSS with Nuxt
- Install Tailwind CSS with Gatsby
- Install Tailwind CSS with SolidJS
- Install Tailwind CSS with SvelteKit
- Install Tailwind CSS with Angular
- Install Tailwind CSS with Ruby on Rails
- Install Tailwind CSS with Remix
- Install Tailwind CSS with Phoenix
- Install Tailwind CSS with Parcel
- Install Tailwind CSS with Symfony
- Install Tailwind CSS with Meteor
- Install Tailwind CSS with Create React App
- Install Tailwind CSS with Adonis
- Install Tailwind CSS With Ember.js
- Install Tailwind CSS with Astro
- Install Tailwind CSS with Qwik
- Install Tailwind CSS with Rspack
Functions & Directives
A reference for the custom functions and directives Tailwind exposes to your CSS.
Directives
Directives are custom Tailwind-specific at-rules you can use in your CSS that offer special functionality for Tailwind CSS projects.
@tailwind
Use the @tailwind
directive to insert Tailwind’s base
, components
, utilities
and variants
styles into your CSS.
/**
* This injects Tailwind's base styles and any base styles registered by
* plugins.
*/
@tailwind base;
/**
* This injects Tailwind's component classes and any component classes
* registered by plugins.
*/
@tailwind components;
/**
* This injects Tailwind's utility classes and any utility classes registered
* by plugins.
*/
@tailwind utilities;
/**
* Use this directive to control where Tailwind injects the hover, focus,
* responsive, dark mode, and other variants of each class.
*
* If omitted, Tailwind will append these classes to the very end of
* your stylesheet by default.
*/
@tailwind variants;
@layer
Use the @layer
directive to tell Tailwind which “bucket” a set of custom styles belong to. Valid layers are base
, components
, and utilities
.
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
h1 {
@apply text-2xl;
}
h2 {
@apply text-xl;
}
}
@layer components {
.btn-blue {
@apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded;
}
}
@layer utilities {
.filter-none {
filter: none;
}
.filter-grayscale {
filter: grayscale(100%);
}
}
Tailwind will automatically move the CSS within any @layer
directive to the same place as the corresponding @tailwind
rule, so you don’t have to worry about authoring your CSS in a specific order to avoid specificity issues.
Any custom CSS added to a layer will only be included in the final build if that CSS is actually used in your HTML, just like all of the classes built in to Tailwind by default.
Wrapping any custom CSS with @layer
also makes it possible to use modifiers with those rules, like hover:
and focus:
or responsive modifiers like md:
and lg:
.
@apply
Use @apply
to inline any existing utility classes into your own custom CSS.
This is useful when you need to write custom CSS (like to override the styles in a third-party library) but still want to work with your design tokens and use the same syntax you’re used to using in your HTML.
.select2-dropdown {
@apply rounded-b-lg shadow-md;
}
.select2-search {
@apply border border-gray-300 rounded;
}
.select2-results__group {
@apply text-lg font-bold text-gray-900;
}
Any rules inlined with @apply
will have !important
removed by default to avoid specificity issues:
/* Input */
.foo {
color: blue !important;
}
.bar {
@apply foo;
}
/* Output */
.foo {
color: blue !important;
}
.bar {
color: blue;
}
If you’d like to @apply
an existing class and make it !important
, simply add !important
to the end of the declaration:
/* Input */
.btn {
@apply font-bold py-2 px-4 rounded !important;
}
/* Output */
.btn {
font-weight: 700 !important;
padding-top: .5rem !important;
padding-bottom: .5rem !important;
padding-right: 1rem !important;
padding-left: 1rem !important;
border-radius: .25rem !important;
}
Note that if you’re using Sass/SCSS, you’ll need to use Sass’ interpolation feature to get this to work:
.btn {
@apply font-bold py-2 px-4 rounded #{!important};
}
Using @apply with per-component CSS
Component frameworks like Vue and Svelte support adding per-component styles within a <style>
block that lives in each component file.
If you try to @apply
a custom class you’ve defined in your global CSS in one of these per-component <style>
blocks, you’ll get an error about the class not existing:
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer components {
.card {
background-color: theme(colors.white);
border-radius: theme(borderRadius.lg);
padding: theme(spacing.6);
box-shadow: theme(boxShadow.xl);
}
}
Card.svelte<div>
<slot></slot>
</div>
<style>
div {
/* Won't work because this file and main.css are processed separately */
@apply card;
}
</style>
This is because under-the-hood, frameworks like Vue and Svelte are processing every single <style>
block independently, and running your PostCSS plugin chain against each one in isolation.
That means if you have 10 components that each have a <style>
block, Tailwind is being run 10 separate times, and each run has zero knowledge about the other runs. Because of this, when you try to @apply card
in Card.svelte
it fails, because Tailwind has no idea that the card
class exists since Svelte processed Card.svelte
and main.css
in total isolation from each other.
The solution to this problem is to define any custom styles you want to @apply
in your components using the plugin system instead:
const plugin = require('tailwindcss/plugin')
module.exports = {
// ...
plugins: [
plugin(function ({ addComponents, theme }) {
addComponents({
'.card': {
backgroundColor: theme('colors.white'),
borderRadius: theme('borderRadius.lg'),
padding: theme('spacing.6'),
boxShadow: theme('boxShadow.xl'),
}
})
})
]
}
This way any file processed by Tailwind that uses this config file will have access to those styles.
Honestly though the best solution is to just not do weird stuff like this at all. Use Tailwind’s utilities directly in your markup the way they are intended to be used, and don’t abuse the @apply
feature to do things like this and you will have a much better experience.
@config
Use the @config
directive to specify which config file Tailwind should use when compiling that CSS file. This is useful for projects that need to use different configuration files for different CSS entry points.
@config "./tailwind.site.config.js";
@tailwind base;
@tailwind components;
@tailwind utilities;
The path you provide to the @config
directive is relative to that CSS file, and will take precedence over a path defined in your PostCSS configuration or in the Tailwind CLI.
Note that if you’re using postcss-import
, your @import
statements need to come before @config
for things to work correctly, as postcss-import
is strict about following the CSS spec which requires @import
statements to precede any other rules in the file.
Don’t put @config
before your @import
statements
@config "./tailwind.admin.config.js";
@import "tailwindcss/base";
@import "./custom-base.css";
@import "tailwindcss/components";
@import "./custom-components.css";
@import "tailwindcss/utilities";
Put your @import
statements before the @config
directive
@import "tailwindcss/base";
@import "./custom-base.css";
@import "tailwindcss/components";
@import "./custom-components.css";
@import "tailwindcss/utilities";
@config "./tailwind.admin.config.js";
Functions
Tailwind adds a few custom functions you can use in your CSS to access Tailwind-specific values. These functions are evaluated at build-time, and are replaced by static values in your final CSS.
theme()
Use the theme()
function to access your Tailwind config values using dot notation.
.content-area {
height: calc(100vh - theme(spacing.12));
}
If you need to access a value that contains a dot (like the 2.5
value in the spacing scale), you can use square bracket notation:
.content-area {
height: calc(100vh - theme(spacing[2.5]));
}
Since Tailwind uses a nested object syntax to define its default color palette, make sure to use dot notation to access the nested colors.
Don’t use the dash syntax when accessing nested color values
.btn-blue {
background-color: theme(colors.blue-500);
}
Use dot notation to access nested color values
.btn-blue {
background-color: theme(colors.blue.500);
}
To adjust the opacity of a color retrieved with theme
, use a slash followed by the opacity value you want to use:
.btn-blue {
background-color: theme(colors.blue.500 / 75%);
}
screen()
The screen
function allows you to create media queries that reference your breakpoints by name instead of duplicating their values in your own CSS.
@media screen(sm) {
/* ... */
}
This will resolve to the underlying screen value at build-time, generating a regular media query that matches specified breakpoint:
@media (min-width: 640px) {
/* ... */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论