Tailwind CSS Configuration
Tailwind CSS's core configuration file is tailwind.config.js.
tailwind.config.js is the configuration file for Tailwind CSS, used to customize Tailwind's default configuration.
By modifying the tailwind.config.js file, developers can customize design systems such as colors, spacing, fonts, and breakpoints to meet specific project needs.
Create a Configuration File
Use Tailwind CSS's CLI tool to quickly generate a basic tailwind.config.js file.
Run the following command in the project root directory:
npx tailwindcss init
This will create a tailwind.config.js file with basic configuration.
Basic structure
The following is the structure of a typical tailwind.config.js file:
Example
/** @type {import('tailwindcss').Config} */
module.
exports
=
{
content
:
[
"./src/**/*.{html,js,vue}"
]
,
// Define template file paths to scan
theme
:
{
extend
:
{
}
,
// Custom extensions
}
,
plugins
:
[
]
,
// Configure plugins
}
The above code tells Tailwind CSS to scan all .html, .js, and .vue files in the src directory.
Main Configuration Options
1. Configure Template Paths: content
In the tailwind.config.js file, the content option specifies which files Tailwind CSS should scan to collect class names.
content: [
"./index.html", // 单文件路径
"./src/**/*.{html,js,ts,jsx,tsx}", // 包含多个文件类型的路径
],
This tells Tailwind CSS to scan all .html, .js, .ts.jsx, and .tsx files in the src directory.
The content field specifies Tailwind's template file paths. Tailwind scans these files to generate necessary styles, reducing the generated CSS file size by only generating styles used in template files and preventing unused styles from being compiled into the final output.
2. Customize Theme: theme
In the theme section, you can define colors, fonts, spacing, border sizes, and more.
For example, extend default colors or add new colors:
theme: {
extend: {
colors: {
cyan: '#9cdbff',
}
}
}
This adds a new color named cyan, and you can use class names like bg-cyan in your project.
| Configuration Item | Description | Example |
|---|---|---|
fontSize |
Custom Font Sizes | javascript fontSize: { 'xs': '0.75rem', '4xl': '2.5rem', }, |
screens |
Define Breakpoints (Responsive Design) | javascript screens: { 'sm': '640px', 'xl': '1280px', }, |
borderRadius |
Custom Rounded Corners | javascript borderRadius: { 'lg': '1rem', 'xl': '1.5rem', }, |
extend |
Extend Existing Default Values | javascript extend: { colors: { danger: '#FF5A5F', }, }, |
Example
theme
:
{
extend
:
{
colors
:
{
primary
:
'#1D4ED8'
,
secondary
:
'#64748B'
,
}
,
spacing
:
{
18
:
'4.5rem'
,
// Add 4.5rem spacing
}
,
fontSize
:
{
'4xl'
:
'2.5rem'
,
}
,
borderRadius
:
{
'xl'
:
'1.5rem'
,
}
,
screens
:
{
'2xl'
:
'1536px'
,
// Add a new responsive breakpoint
}
,
}
,
}
Define colors via theme.colors and reference them in styles.
theme: {
extend: {
colors: {
brand: {
light: '#93C5FD',
DEFAULT: '#3B82F6',
dark: '#1E40AF',
},
},
},
}
Example
Custom Colors Example
Try it »
Enable Tailwind's dark mode support via darkMode.
module.exports = {
darkMode: 'class', // 或 'media'
theme: {
extend: {
colors: {
background: {
light: '#FFFFFF',
dark: '#000000',
},
},
},
},
}
Example
Dark Mode Example
3. Configure Variants: variants
The variants option allows you to define which states (such as hover, focus, etc.) should be included in the final CSS.
You can extend or override the default variants:
variants: {
extend: {
borderColor: ['focus-visible'],
opacity: ['disabled'],
}
}
4. Using Plugins: plugins
The plugins option allows you to add custom utility classes.
These plugins can be officially provided by Tailwind CSS or created by the community, in the following format:
plugins: [
require('some-tailwindcss-plugin')
]
Using a Plugin Instance:
Example
const
plugin
=
require
(
'tailwindcss/plugin'
)
;
module.
exports
=
{
plugins
:
[
plugin
(
function
(
{
addUtilities
}
)
{
addUtilities
(
{
'.text-shadow'
:
{
textShadow
:
'2px 2px 4px rgba(0,0,0,0.5)'
,
}
,
}
)
;
}
)
,
]
,
}
Importing a Community Plugin:
Example
module.
exports
=
{
plugins
:
[
require
(
'@tailwindcss/forms'
)
,
// Form style plugin
require
(
'@tailwindcss/typography'
)
,
// Typography plugin
]
,
}
Tailwind CSS Configuration
By default, Tailwind looks for the tailwind.config.js file in the project root directory, where you can set the configuration information you need:
Example
/** @type {import('tailwindcss').Config} */
module.
exports
=
{
// Specify which files Tailwind CSS should extract class names from. Here it is configured to extract from./srcfiles in the.htmland.jsdirectory.
content
:
[
'./src/**/*.{html,js}'
]
,
theme
:
{
// Custom color configuration
colors
:
{
'blue'
:
'#1fb6ff'
,
// Defines a color named 'blue' with value #1fb6ff
'purple'
:
'#7e5bef'
,
// Defines a color named 'purple' with value #7e5bef
'pink'
:
'#ff49db'
,
// Defines a color named 'pink' with value #ff49db
'orange'
:
'#ff7849'
,
// Defines a color named 'orange' with value #ff7849
'green'
:
'#13ce66'
,
// Defines a color named 'green' with value #13ce66
'yellow'
:
'#ffc82c'
,
// Defines a color named 'yellow' with value #ffc82c
'gray-dark'
:
'#273444'
,
// Defines a color named 'gray-dark' with value #273444
'gray'
:
'#8492a6'
,
// Defines a color named 'gray' with value #8492a6
'gray-light'
:
'#d3dce6'
// Defines a color named 'gray-light' with value #d3dce6
}
,
// Custom font family configuration
fontFamily
:
{
sans
:
[
'Graphik'
,
'sans-serif'
]
,
// Defines a font family named 'sans', including 'Graphik' and the generic 'sans-serif'
serif
:
[
'Merriweather'
,
'serif'
]
// Defines a font family named 'serif', including 'Merriweather' and the generic 'serif'
}
,
// Custom extension configuration
extend
:
{
// Custom spacing extension
spacing
:
{
'8xl'
:
'96rem'
,
// Defines a spacing named '8xl' with value 96rem
'9xl'
:
'128rem'
// Defines a spacing named '9xl' with value 128rem
}
,
// Custom border radius extension
borderRadius
:
{
'4xl'
:
'2rem'
// Defines a border radius named '4xl' with value 2rem
}
}
}
,
}
The following table lists the configuration options of the tailwind.config.js file and their usage descriptions:
| Configuration Item | Description | Default value | Example |
|---|---|---|---|
| content | Configure the template file paths Tailwind CSS scans, used to remove unused styles. | ['./src/**/*.{html,js,ts,jsx,tsx}'] |
content: ["./src/**/*.html", "./src/**/*.vue"] |
| theme | Used to define and extend Tailwind's default design system, including colors, spacing, fonts, etc. | extend: {} |
theme: { colors: { primary: '#3490dc' } } |
| extend | Used to extend Tailwind's default configuration options rather than overriding the defaults. | {} |
extend: { colors: { primary: '#3490dc' } } |
| screens | Configure responsive breakpoints, determining when specific styles are applied. | { sm: '640px', md: '768px', lg: '1024px', xl: '1280px' } |
screens: { '2xl': '1536px' } |
| colors | Customize or extend the color palette. | A series of colors are provided by default, such as blue, red, green etc. |
colors: { primary: '#1D4ED8', secondary: '#64748B' } |
| spacing | Set padding and margins (p, m, pt, mt etc.). |
Default units are provided, such as 0.5rem, 1rem, 2rem etc. |
spacing: { 18: '4.5rem' } |
| fontFamily | Customize font families. | 'sans', 'serif', 'mono' |
fontFamily: { sans: ['Inter', 'sans-serif'] } |
| fontSize | Customize font sizes. | 'sm': '0.875rem', 'lg': '1.125rem' |
fontSize: { '4xl': '2.5rem' } |
| fontWeight | Customize font weights. | 'normal', 'bold', 'bolder', 'lighter' |
fontWeight: { 'light': '300', 'bold': '700' } |
| lineHeight | Set line height. | 'none': '1', 'tight': '1.25', 'normal': '1.5', 'loose': '2' |
lineHeight: { 'relaxed': '1.75', 'loose': '2' } |
| borderRadius | Set border radius. | 'none': '0px', 'sm': '0.125rem', 'md': '0.375rem', 'lg': '0.5rem' |
borderRadius: { 'xl': '1.5rem' } |
| borderWidth | Set border width. | { default: '1px', '0': '0', '2': '2px' } |
borderWidth: { '3': '3px' } |
| zIndex | Settings z-index Layering. |
{ '0': '0', '10': '10', '20': '20' } |
zIndex: { 'max': '9999' } |
| opacity | Set opacity values. | { '0': '0', '25': '0.25', '50': '0.5', '75': '0.75', '100': '1' } |
opacity: { '40': '0.4' } |
| boxShadow | Set box shadows. | { sm: '0 1px 2px 0 rgba(0, 0, 0, 0.05)', default: '0 4px 6px rgba(0, 0, 0, 0.1)'} |
boxShadow: { 'lg': '0 10px 15px -3px rgba(0, 0, 0, 0.1)' } |
| container | Set the container's maximum width (for responsive design). | { center: false, padding: '2rem' } |
container: { center: true, padding: '1rem' } |
| aspectRatio | Set element aspect ratio. | { 'none': 'none' } |
aspectRatio: { '16/9': '16/9' } |
| gridTemplateColumns | Set CSS Grid column templates. | { auto: 'auto', '1fr': '1fr' } |
gridTemplateColumns: { 'layout': 'repeat(3, 1fr)' } |
| gridColumn | Set Grid item column-spanning behavior. | { 'span-1': 'span 1 / span 1' } |
gridColumn: { 'span-2': 'span 2 / span 2' } |
| transitionProperty | Set transition properties. | { all: 'all' } |
transitionProperty: { 'color': 'color' } |
| transitionDuration | Set transition duration. | { 75: '75ms', 150: '150ms', 300: '300ms' } |
transitionDuration: { '400': '400ms' } |
| transitionTimingFunction | Set the timing function (transition function) used during transitions. | { 'ease': 'ease', 'linear': 'linear' } |
transitionTimingFunction: { 'ease-out': 'ease-out' } |
Through tailwind.config.js, you can easily customize Tailwind CSS's features and styles to suit various design needs. Reasonable use of features such as extend, plugins, and theme configuration can greatly improve project development efficiency and style flexibility.