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 applied directly to HTML elements. |
| Responsive Prefixes | Use prefixes like sm:, md:, lg:, xl: to control styles at different screen sizes. |
| Colors and Sizing | Provides predefined colors (such as bg-red-500) and sizes (such as text-lg) to quickly set styles. |
| Spacing (Spacing) | Pass p-, m-, pt-, pr- and other classes control padding and margin. |
| Layout (Layout) | Provides flex, grid, float and other classes to achieve layout control. |
| Text Styles | Utility classes including text alignment, font style, color, and transformation, such as text-center, font-bold, uppercase。 |
| Backgrounds and Borders | Utility classes providing background colors, background images, border styles and colors, such as bg-gray-200, border-red-500。 |
| Hover and States | Use hover:, focus:, active: and other prefixes define styles for interactive states. |
| Sizing (Sizing) | Use w-, h- classes control width and height, such as w-64, h-screen。 |
| Visibility (Visibility) | Use visible, invisible and other classes control element visibility. |
| Grid System (Grid System) | Provides CSS Grid-based utility classes, such as grid, grid-cols-3, col-span-2, to achieve responsive grid layouts. |
| Custom Configuration | Pass tailwind.config.js Customize colors, spacing, font sizes, and more to suit project needs. |
| Dark Mode (Dark Mode) | Supports dark mode, set styles via the dark: prefix, and can enable dark mode in the configuration. |
| Plugins (Plugins) | Extend functionality through plugins, adding custom utility classes or features. |
| Directives (Directives) | Use in CSS files @tailwind directives to import styles of different layers, such as base, components, utilities。 |
Concepts in Detail
1. Utility Classes (Utility-First)
Tailwind's utility classes let developers quickly define styles without manually writing CSS.
Example
Tailwind Utility Classes
Try it »
Analysis:
text-center: Center alignment.text-blue-500: Blue text.font-bold: Bold font.
2. Responsive Prefixes
Set styles for different screen sizes via prefixes.
Example
Responsive Example
Try it »
Analysis:
- Default: Red background.
- Small screens (≥640px): Green background.
- Medium screens (≥768px): Blue background.
- Large screens (≥1024px): Yellow background.
3. Colors and Sizing
Tailwind provides a rich set of predefined colors and sizes for quick application.
Example
Text with pre-defined size and color
Try it »
Analysis:
text-lg: Text size.text-gray-600: Gray text.
4. Spacing (Spacing)
Set element padding and margin via spacing utility classes.
Example
Padding and Margin Example
Try it »
Analysis:
p-4: Padding of 1rem.m-8: Margin of 2rem.
5. Layout (Layout)
The provided classes can quickly achieve flexible layouts such as Flexbox and Grid.
Example
Item 1
Item 2
Try it »
Grid Example:
Example
1
2
3
Try it »
6. Text Styles
Control text alignment, color, size, and other styles.
Example
Centered Bold Text
Try it »
7. Backgrounds and Borders
Set background colors, images, and border styles via utility classes.
Example
Background and Border Example
Try it »
8. Hover and States
Set state styles for interactive elements.
Example
Try it »
9. Sizing (Sizing)
Set element width and height.
Example
Width and Height
Try it »
10. Visibility (Visibility)
Control element display and hiding.
Example
Visible only on small screens
Try it »
11. Grid System (Grid System)
Use Tailwind's Grid utility classes to create grid layouts.
Example
1
2
3
Try it »
12. Custom Configuration
Customize the design system via the configuration file.
Example
// tailwind.config.js
module.
exports
=
{
theme
:
{
extend
:
{
colors
:
{
primary
:
'#1D4ED8'
,
}
,
}
,
}
,
}
Try it »
13. Dark Mode
Enable dark mode and define styles.
Example
Dark Mode Example
Try it »
14. Plugin
Extend Tailwind's functionality through plugins.
Example
// tailwind.config.js
const
plugin
=
require
(
'tailwindcss/plugin'
)
module.
exports
=
{
plugins
:
[
plugin
(
function
(
{
addUtilities
}
)
{
addUtilities
(
{
'.rotate-45'
:
{
transform
:
'rotate(45deg)'
,
}
,
}
)
}
)
,
]
,
}
15. Directives (Directives)
Pass @tailwind Directives load Tailwind's different layers.
Example
@tailwind base;
@tailwind components;
@tailwind utilities;