Tailwind CSS Configuration
The core configuration file of Tailwind CSS istailwind.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 system elements such as colors, spacing, fonts, and breakpoints to meet specific project requirements.
Create Configuration File
You can quickly generate a basic tailwind.config.js file using the Tailwind CSS CLI tool.
Run the following command in the project root directory:
npx tailwindcss init
This will create a tailwind.config.js file containing the basic configuration.
Basic Structure
The following is the structure of a typical tailwind.config.js file:
Example
module.exports = {
content: ["./src/**/*.{html,js,vue}"], // Define the template file paths to be scanned
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 Items
1. Configure Template Path: content
In the tailwind.config.js file, the content option is used to specify 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 is used to specify the template file paths for Tailwind. Tailwind scans these files to generate the necessary styles, reducing the generated CSS file size by only generating styles used in the template files, 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 the default colors or add new colors:
theme: {
extend: {
colors: {
cyan: '#9CDBFF',
}
}
}This will add a new color named cyan, and you can use class names like bg-cyan in your project.
| Configuration Item | Description | Example |
|---|---|---|
fontSize | Custom font size | javascript fontSize: { 'xs': '0.75rem', '4xl': '2.5rem', }, |
screens | Define breakpoints (responsive design) | javascript screens: { 'sm': '640px', 'xl': '1280px', }, |
borderRadius | Custom border radius | javascript borderRadius: { 'lg': '1rem', 'xl': '1.5rem', }, |
extend | Extend existing default values | javascript extend: { colors: { danger: '#FF5A5F', }, }, |
Example
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 new responsive breakpoints
},
},
}
Define colors via theme.colors and reference them in styles.
theme: {
extend: {
colors: {
brand: {
light: '#93C5FD',
DEFAULT: '#3B82F6',
dark: '#1E40AF',
},
},
},
}
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
</div>
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. Use 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, with the format as follows:
plugins: [
require('some-tailwindcss-plugin')
]
Example of using a plugin:
Example
module.exports = {
plugins: [
plugin(function ({ addUtilities }) {
addUtilities({
'.text-shadow': {
textShadow: '2px 2px 4px rgba(0,0,0,0.5)',
},
});
}),
],
}
Import community plugins:
Example
plugins: [
require('@tailwindcss/forms'), // Form styles 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
module.exports = {
// Specify which files Tailwind CSS should extract class names from. Here it is configured to extract from all `.html` and `.js` files in the `./src` directory.
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': '#1370CE', // Defines a color named 'green' with value #1370CE
'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 for Tailwind CSS to scan, 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 items rather than overriding default values. | {} | extend: { colors: { primary: '#3490dc' } } |
| screens | Configure responsive breakpoints to determine when specific styles are applied. | { sm: '640px', md: '768px', lg: '1024px', xl: '1280px' } | screens: { '2xl': '1536px' } |
| colors | Customize or extend the color palette. | Provides a range of colors by default, such asblue, red, greenwait | colors: { primary: '#1D4ED8', secondary: '#64748B' } |
| spacing | Set inner and outer margins (p, m, pt, mtetc.). | Provides units by default, such as0.5rem, 1rem, 2remwait | spacing: { 18: '4.5rem' } |
| fontFamily | Customize font family. | 'sans', 'serif', 'mono' | fontFamily: { sans: ['Inter', 'sans-serif'] } |
| fontSize | Customize font size. | 'sm': '0.875rem', 'lg': '1.125rem' | fontSize: { '4xl': '2.5rem' } |
| fontWeight | Customize font weight. | '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 | Setz-indexthe level. | { '0': '0', '10': '10', '20': '20' } | zIndex: { 'max': '9999' } |
| opacity | Set opacity value. | { '0': '0', '25': '0.25', '50': '0.5', '75': '0.75', '100': '1' } | opacity: { '40': '0.4' } |
| boxShadow | Set box shadow. | { 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 maximum width of the container (for responsive design). | { center: false, padding: '2rem' } | container: { center: true, padding: '1rem' } |
| aspectRatio | Set the aspect ratio of elements. | { 'none': 'none' } | aspectRatio: { '16/9': '16/9' } |
| gridTemplateColumns | Set the column template of CSS Grid. | { auto: 'auto', '1fr': '1fr' } | gridTemplateColumns: { 'layout': 'repeat(3, 1fr)' } |
| gridColumn | Set the column-span behavior of Grid items. | { '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 for transitions. | { 'ease': 'ease', 'linear': 'linear' } | transitionTimingFunction: { 'ease-out': 'ease-out' } |
With tailwind.config.js, you can easily customize the features and styles of Tailwind CSS to suit various design needs. Making reasonable use of features such as extend, plugins, and theme configuration can greatly improve project development efficiency and style flexibility.
Other Extensions