Tailwind CSS Custom Styles
Tailwind CSS custom styles are mainly used by modifying the tailwind.config.js configuration file.
Tailwind provides multiple ways to customize, including custom colors, spacing, fonts, screen breakpoints, and other configuration options.
You can create a tailwind.config.js file with the following command:
npx tailwindcss init
This command generates a default configuration file in the project root directory.
Custom Theme (theme)
In the theme section of the tailwind.config.js file, you can define colors, fonts, spacing, etc.
For example, extend default colors or add new colors:
module.exports = {
theme: {
extend: {
colors: {
cyan: '#9cdbff',
}
}
}
}
This adds a new color named cyan, and you can use class names like bg-cyan or text-cyan in your project.
Example
Custom Colors
Try it »
Custom Colors (Colors)
Tailwind provides a default color system, but you can add or modify colors for your project via theme.extend.
// tailwind.config.js
module.exports = {
theme: {
extend: {
colors: {
'custom-blue': '#1c64f2',
'custom-gray': '#3a3a3a',
},
},
},
};
In the example above, custom-blue and custom-gray have been added to the color system, and you can use them in HTML just like using default colors:
Example
Custom blue background
Try it »
Custom Spacing (Spacing)
Tailwind's spacing system includes m- (margin) and p- (padding) classes, and default spacing values can be extended via theme.extend.spacing:
// tailwind.config.js
module.exports = {
theme: {
extend: {
spacing: {
'128': '32rem', // 定义一个自定义的间距值
'144': '36rem',
},
},
},
};
This allows you to use custom spacing in HTML:
Example
Custom padding
Try it »
Custom Fonts (Fonts)
Tailwind also allows you to customize font families. You can extend font settings via fontFamily:
// tailwind.config.js
module.exports = {
theme: {
extend: {
fontFamily: {
'sans': ['Helvetica', 'Arial', 'sans-serif'],
'custom': ['"Open Sans"', 'Arial', 'sans-serif'],
},
},
},
};
Then you can use the font-custom class to apply the custom font:
Example
This is custom text
Try it »
Custom Breakpoints (Breakpoints)
Tailwind provides five responsive breakpoints (sm, md, lg, xl, 2xl).
You can modify these breakpoints in tailwind.config.js, or add custom breakpoints:
// tailwind.config.js
module.exports = {
theme: {
extend: {
screens: {
'xs': '475px', // 添加一个新的小屏幕断点
'xxl': '1600px', // 添加一个超大屏幕断点
},
},
},
};
Then you can use xs:w-64 or xxl:w-96 classes to set widths at different breakpoints:
Example
Responsive width
Try it »
Custom Shadows (Box Shadows)
You can extend shadow effects via the boxShadow property.
// tailwind.config.js
module.exports = {
theme: {
extend: {
boxShadow: {
'custom': '0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.1)',
},
},
},
};
Then, you can apply this custom shadow in HTML:
Example
Element with custom shadow
Try it »
Extending Tailwind with Plugins
In addition to modifying the configuration file, Tailwind also allows you to extend functionality with plugins, so you can use more utility classes in your project.
Using Tailwind Plugins
You can install third-party plugins to add extra functionality:
npm install @tailwindcss/forms
Then, add the plugin in tailwind.config.js:
// tailwind.config.js
module.exports = {
plugins: [
require('@tailwindcss/forms'),
],
};
This plugin provides you with many utility classes for form controls.
Reusing Styles with @apply
If you find yourself using the same combination of styles in multiple places, you can reuse them with the @apply directive.
For example, create a custom class that combines multiple utility classes:
@tailwind base;
@tailwind components;
@tailwind utilities;
/* In your CSS file */
.avatar {
@apply w-16 h-16 rounded-full border-2 border-white;
}
Compile Tailwind CSS:
npx tailwindcss -i ./styles.css -o ./output.css --watch
The output.css file generated after compilation replaces @apply with specific CSS styles.
You can apply the .avatar class to multiple elements without repeating all the utility classes:
Example
Defining Custom Components with @layer
Tailwind allows you to use the @layer directive to define custom component classes.
For example, create a custom button with Tailwind styles:
/* 在你的 CSS 文件中 */
@layer components {
.btn {
@apply px-4 py-2 bg-blue-500 text-white font-semibold rounded-lg;
}
.btn-danger {
@apply px-4 py-2 bg-red-500 text-white font-semibold rounded-lg;
}
}
Then use the custom button class in HTML:
Example
Extending State Modifiers with variants
In tailwind.config.js, you can use variants to configure whether Tailwind should generate utility classes for specific states.
For example, add more style variants for hover and focus states:
// tailwind.config.js
module.exports = {
variants: {
extend: {
backgroundColor: ['active', 'disabled'],
textColor: ['group-hover'],
},
},
};
This generates more utility classes, such as bg-red-500 active:bg-blue-500, switching the background color in the active state.