Tailwind CSS is a utility-first CSS framework designed to allow developers to build custom designs quickly. Instead of writing custom styles, Tailwind provides a collection of utility classes that you can use to style elements directly in the HTML.
1. Installation and Setup
How to install Tailwind using CDN
For quick prototyping, you can include Tailwind directly in your project with a CDN. Add the following to your HTML <head> tag:
Add the Play CDN script tag to the <head> of your HTML file, and start using Tailwind’s utility classes to style your content.
| |
2. Key Concepts in Tailwind CSS
Utility-First Approach
Tailwind uses small utility classes like text-center, bg-blue-500, and mt-4 to control elements’ design directly in the HTML. This eliminates the need for writing custom CSS.
Responsive Design
Tailwind provides responsive utilities that help you design mobile-first and scale up for larger screens. You can add breakpoints to utilities using sm:, md:, lg:, xl:, etc.
3. Core Concepts
Colors
Tailwind provides a set of predefined colors. You can apply them using classes like bg-blue-500 or text-red-600.
Example:
| |
Spacing (Padding and Margin)
Tailwind has utilities for margin (m-*) and padding (p-*), where * is a scale value.
Example:
| |
Typography
Tailwind provides utilities for font size (text-*), font weight (font-*), text color (text-*), and line height (leading-*).
Example:
| |
4. Layout Utilities
Flexbox and Grid
Tailwind CSS makes it easy to use Flexbox and CSS Grid with utilities like flex, grid, flex-col, justify-center, etc.
Example of Flexbox:
| |
Example of Grid:
| |
Container
The .container class centers your content and applies responsive width.
<div class="container mx-auto p-4">
Content here will be centered.
</div>5. Customization
You can customize Tailwind’s default theme using the tailwind.config.js file. For example, you can change the color palette, font sizes, spacing, etc.
| |
7. Tailwind Directives
@tailwind base;: Applies the base styles like resets.@tailwind components;: Injects component-level styles.@tailwind utilities;: Includes utility classes like padding, margin, colors, etc.@apply: Use this directive to group utilities into a single class (useful for custom components).
| |
8. Handling States with Pseudo-Classes
Tailwind provides utilities for pseudo-classes such as hover:, focus:, active:, etc.
Example:
| |
9. Transitions and Animations
You can add smooth transitions and animations using Tailwind’s utility classes. Example:
| |
10. Plugins
Tailwind allows you to extend its functionality with plugins, such as for forms, typography, or aspect ratio utilities. Example of installing a plugin:
| |
Then, add it to your tailwind.config.js:
| |
11. Tailwind CSS with JIT Mode
Just-in-time (JIT) mode generates only the classes you use, reducing file size and speeding up development. To enable JIT:
| |
12. Example Project
| |