Tailwind CSS Directives and Functions
Tailwind CSS provides some very useful functions and directives that help developers better manage styles and optimize code.
Through functions and directives, Tailwind can give you more control and flexibility, especially in customizing styles, handling responsive design, and extending functionality.
Tailwind CSS provides a variety of directives and functions to help developers manage and customize styles:
- Use
@tailwindImport Tailwind's base styles, component styles, and utility classes. @applyCombine multiple utility classes into a custom class to avoid style duplication.@layerAllows you to organize and define different layers of styles in custom CSS.- Using built-in functions for colors, spacing, opacity, border radius, and so on, you can dynamically compute and generate styles.
@tailwind Directive
@tailwind The directive is used to import Tailwind's three core layers into a CSS file:base, components and utilities。
- @tailwind base: Import Tailwind's base styles (such as browser reset styles).
- @tailwind components: Import component styles, used to define base styles for buttons, cards, forms, etc.
- @tailwind utilities: Import utility classes, the core part of the Tailwind style library, providing low-level utility classes for layout, colors, spacing, borders, etc.
These layers define all the base styles, component styles, and utility classes you need when using Tailwind.
/* 自定义样式中引入 Tailwind 样式 */
@tailwind base; /* 引入基础样式 */
@tailwind components; /* 引入组件样式 */
@tailwind utilities; /* 引入工具类 */
@apply Directive
@apply The directive is used to apply Tailwind utility classes in custom CSS classes. This allows you to combine commonly used styles together while maintaining Tailwind's maintainability and flexibility.
.my-class {
@apply ... ;
}
Example
/* Custom button class */
.btn
{
@apply px-4 py-2 bg-blue-500 text-white rounded-lg;
}
/* Custom warning button class */
.btn-warning
{
@apply px-4 py-2 bg-yellow-500 text-black rounded-lg;
}
By using @apply, you can combine commonly used classes into a custom CSS class so that it can be reused.
@layer Directive
@layer The directive is used to define layers in Tailwind, mainly for organizing custom extension styles. It is commonly used to create custom components, utility classes, and so on, to keep the CSS file structure clear.
- @layer components: Define custom components.
- @layer utilities: Define custom utility classes.
- @layer base: Define base styles.
Syntax format:
@layer components {
/* Define custom component class here */
}
@layer utilities {
/* Define custom utility class here */
}
Example
/* Define a custom button component */
@layer components {
.btn
{
@apply px-4 py-2 bg-blue-500 text-white rounded-lg;
}
}
/* Define custom utility classes */
@layer utilities {
.no-scrollbar
{
@apply overflow-hidden;
}
}
@import Directive
In Tailwind CSS, you can also use the @import directive to import other CSS files. Although Tailwind itself does not recommend using it frequently, @importit still supports this feature.
Syntax format:
@import '';
Tailwind CSS Functions
Functions in Tailwind CSS are mainly used to dynamically generate styles, such as computing colors, opacity, sizes, and so on. They let you do more computation and control in configuration files or custom styles.
Color Functions
Tailwind provides some built-in color functions that can be used to generate different variants of a color (such as darkening, lightening, and so on).
- darken(): Darken a color.
- lighten(): Lighten a color.
- opacity(): Set the opacity of a color.
Example
/* Use the lighten function to brighten a color */
.bg-custom-lighten
{
background-color
:
lighten
(
#3490dc
,
10%
)
;
}
/* Use the darken function to darken a color */
.bg-custom-darken
{
background-color
:
darken
(
#3490dc
,
10%
)
;
}
/* Use the opacity function to set transparency */
.bg-custom-opacity
{
background-color
:
rgba
(
52
,
144
,
220
,
0.8
)
;
}
Spacing Functions
Tailwind provides rem, px, em and other unit functions. You can use them to compute dynamic size and spacing values.
Example
/* Use the rem function to set spacing based on the root font size */
.margin-4
{
margin
:
rem
(
4
)
;
}
/* Use the px function to set spacing */
.padding-10px
{
padding
:
px
(
10
)
;
}
Border Radius Functions
Functions can also be used in Tailwind CSS to set border radius. For example, using radius() to compute different border radii.
Example
/* Use the radius function to set border radius */
.rounded-custom
{
border-radius
:
radius
(
8px
)
;
}
Custom Functions
Tailwind CSS allows you to define and use custom functions. You can tailwind.config.js Using it in theme functions to access and manipulate Tailwind's design system. For example, you can dynamically generate colors, spacing, fonts, etc.
Example
// tailwind.config.js
module.
exports
=
{
theme
:
{
extend
:
{
colors
:
{
'custom-green'
:
'#10b981'
,
// Use the color code directly
}
,
spacing
:
{
'1/3'
:
'33.333333%'
,
// Custom spacing unit
}
,
}
,
}
,
}
;
Commonly Used Built-in Functions of Tailwind CSS
Tailwind CSS has many built-in functions to help you do calculations when building custom styles.
The following are some common built-in functions:
| function | Description | Example |
|---|---|---|
rem() |
Convert the specified pixel value to rem units, based on the root font size (default 16px). |
rem(16) → 1rem |
em() |
Convert the specified pixel value to em units, based on the current element's font size. |
em(16) → 1em |
px() |
Convert the specified numeric value to px units. |
px(16) → 16px |
calc() |
Use the CSS calc() function in Tailwind CSS for dynamic computation. |
calc(100% - 20px) |
min() |
Take the smaller of two values. | min(10px, 5vw) → 5vw |
max() |
Take the larger of two values. | max(10px, 5vw) → 10px |
clamp() |
Generate a clamp() function, commonly used to set responsive font sizes and spacing. |
clamp(1rem, 5vw, 3rem) → Dynamically generate responsive font sizes |
theme() |
Access theme values in Tailwind's configuration (such as colors, spacing, fonts, etc.). | theme('colors.blue.500') → #3b82f6 |
screen() |
Return the appropriate screen size breakpoint for responsive design. | screen('md') → @media (min-width: 768px) |
rem() Example:
/* 将数字 16 转换为 rem 单位 */
.my-class {
font-size: rem(16); /* 结果为 1rem */
}
em() Example:
/* 将数字 16 转换为 em 单位 */
.my-class {
padding: em(16); /* 结果为 1em */
}
px() Example:
/* 使用 px() 函数生成固定像素 */
.my-class {
margin: px(16); /* 结果为 16px */
}
calc() Example:
/* 使用 calc() 函数动态计算 */
.my-class {
width: calc(100% - px(20)); /* 结果为 100% - 20px */
}
theme() Example:
/* 使用 theme() 函数访问 Tailwind 主题配置 */
.my-class {
background-color: theme('colors.green.500'); /* 结果为 #10b981 */
}
clamp() Example:
/* 使用 clamp() 设置字体大小,基于视口宽度变化 */
.my-class {
font-size: clamp(1rem, 5vw, 3rem); /* 最小 1rem,最大 3rem,视口宽度介于其间时动态变化 */
}
Using Conditional Operators and Function Combinations
In Tailwind, many features can be used with conditional operators. Combined with responsive design and state classes (such as hover:, focus:), you can apply these styles dynamically.
For example, you can combine @apply and responsive design to achieve certain features:
Example
/* Custom styles combining responsive and state classes */
.button
{
@apply py-2 px-4 bg-blue-500 text-white rounded-md;
}
/* On medium screens, the button background color turns green */
@media (min-width: 768px) {
.button
:hover
{
background-color
:
#48bb78
;
/* Use Tailwind's green */
}
}