Tailwind CSS Configuration
The power of Tailwind CSS lies in its extreme customizability. Almost everything—from color palettes to spacing units to breakpoints—can be customized by modifying the tailwind.config.js file.
Here is a typical tailwind.config.js file structure:
/** @type {import('tailwindcss').Config} */
export default {
content: [
'./src/**/*.{html,js,vue}',
],
theme: {
extend: {},
},
plugins: [],
}content
The content array tells Tailwind's JIT engine where to scan your code to find classes being used. Properly configuring this array is key to ensuring all needed CSS is generated.
theme
The theme object is where you define your project's design system. It contains all visual-related configurations including your color palettes, fonts, spacing, breakpoints, and more.
Overriding Default Theme vs. Extending Default Theme
By default, any configuration you place under the theme object will override Tailwind's default configuration.
// tailwind.config.js
module.exports = {
theme: {
colors: {
// This will completely replace Tailwind's default color palette
'blue': '#1fb6ff',
'purple': '#7e5bef',
},
// ...
}
}If you only want to add new values or modify an existing value while keeping the other default values, you should place your configuration under the theme.extend object.
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
// This will add a new 'tahiti' color and keep all default colors
'tahiti': '#3ab7bf',
},
spacing: {
// This will add a new spacing unit '128' and keep all default spacing
'128': '32rem',
}
}
}
}Best Practice: Always place your custom configuration inside extend unless you truly want to completely replace a specific theme option.
Referencing Other Theme Values
If you want to reference other values in the theme, you can provide a closure function. This function receives a theme() helper function that you can use to access other configuration values.
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
'cyan': theme => theme('colors.blue.500'),
},
height: theme => ({
'screen/2': '50vh',
...theme('spacing') // Reference the entire spacing object
})
}
}
}plugins
The plugins array allows you to register JavaScript plugins that can extend Tailwind's functionality. For example, the official @tailwindcss/typography plugin can add beautiful typographic default styles to native HTML tags (like <h1>, <p>).
// tailwind.config.js
module.exports = {
// ...
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
],
}By deeply understanding and utilizing tailwind.config.js, you can transform Tailwind CSS into a CSS framework that completely matches your project's brand and design specifications.