Tailwind CSS Basic Concepts
Tailwind CSS is a highly customizable CSS framework that provides a set of predefined utility classes, allowing developers to quickly build and design user interfaces.
The following are some basic concepts and usages of Tailwind CSS:
| Concept | Description |
|---|---|
| Utility Classes (Utility-First) | The core of Tailwind CSS is utility classes, used to quickly set styles and apply them directly to HTML elements. |
| Responsive Prefixes | Use such assm:, md:, lg:, xl:prefixes to control styles at different screen sizes. |
| Colors and Sizes | Provides predefined colors (such asbg-red-500) and sizes (such astext-lg) to quickly set styles. |
| Spacing | Throughp-, m-, pt-, pr-and other classes to control padding and margin. |
| Layout | Providesflex, grid, floatand other classes to implement layout control. |
| Text Styles | Includes utility classes for text alignment, font styles, colors, and transformations, such astext-center, font-bold, uppercase。 |
| Backgrounds and Borders | Provides utility classes for background colors, background images, border styles, and colors, such asbg-gray-200, border-red-500。 |
| Hover and States | Usehover:, focus:, active:and other prefixes to define styles for interactive states. |
| Sizing | Usew-, h-classes to control width and height, such asw-64, h-screen。 |
| Visibility | Usevisible, invisibleand other classes to control element visibility. |
| Grid System | Provides utility classes based on CSS Grid, such asgrid, grid-cols-3, col-span-2, to implement responsive grid layouts. |
| Custom Configuration | Throughtailwind.config.jscustomize colors, spacing, font sizes, etc., to suit project needs. |
| Dark Mode | Supports dark mode, usingdark:prefix to set styles, and dark mode can be enabled in the configuration. |
| Plugins | Extend functionality through plugins, adding custom utility classes or features. |
| Directives | Use in CSS files@tailwinddirectives to import different layers of styles, such asbase, components, utilities。 |
Concept Details
1. Utility Classes (Utility-First)
Tailwind's utility classes allow developers to quickly define styles without manually writing CSS.
Explanation:
text-center: Center alignment.text-blue-500: Blue text.font-bold: Bold font.
2. Responsive Prefixes
Use prefixes to set styles at different screen sizes.
Example
Responsive Example
</div>
Try it »
Explanation:
- Default: Red background.
- Small screens (≥640px): Green background.
- Medium screens (≥768px): Blue background.
- Large screens (≥1024px): Yellow background.
3. Colors and Sizes
Tailwind provides a rich set of predefined colors and sizes for quick application.
Explanation:
text-lg: Font size.text-gray-600: Gray text.
4. Spacing
Set element padding and margins using spacing utility classes.
Explanation:
p-4: Padding is 1rem.m-8: Margin is 2rem.
5. Layout
The provided classes can quickly implement flexible layout methods, such as Flexbox and Grid.
Example
<div>Item 1</div>
<div>Item 2</div>
</div>
Try it »
Grid Example:
6. Text Styles
Control text alignment, color, size, and other styles.
7. Backgrounds and Borders
Use utility classes to set background colors, images, and border styles.
Example
Background and Border Example
</div>
Try it »
8. Hover and States
Set state styles for interactive elements.
9. Sizing
Set the width and height of elements.
10. Visibility
Control the display and hiding of elements.
11. Grid System
Use Tailwind's Grid utility classes to create grid layouts.
12. Custom Configuration
Customize the design system through configuration files.
Example
module.exports = {
theme: {
extend: {
colors: {
primary: '#1D4ED8',
},
},
},
}
Try it »
13. Dark Mode
Enable dark mode and define styles.
Example
Dark Mode Example
</div>
Try it »
14. Plugins
Extend Tailwind's functionality through plugins.
Example
const plugin = require('tailwindcss/plugin')
module.exports = {
plugins: [
plugin(function({ addUtilities }) {
addUtilities({
'.rotate-45': {
transform: 'rotate(45deg)',
},
})
}),
],
}
15. Directives
Through@tailwinddirectives to load different layers of Tailwind.
Example
@tailwind components;
@tailwind utilities;