Table of contents
- 1. Tailwind CSS - Home
- 2. Tailwind CSS - Roadmap
- 3. Tailwind CSS - Introduction
- 4. Tailwind CSS - Installation
- 5. Tailwind CSS - Editor Setup
- 6. Tailwind CSS - Core Concepts
- 7. Hover, Focus, and Other States
- 8. Responsive Design
- 9. Dark Mode
- 10. Reusing Styles
- 11. Adding Custom Styles
- 12. Functions & Directives
- 13. Customization
- 14. Plugins
- 15. Common Utility Classes
- 16. Building a Navbar with Tailwind CSS
- Final Tips
This comprehensive Tailwind CSS cheatsheet covers everything you need to master Tailwind CSS, from the basics to advanced customization. Whether you're just starting out or looking to enhance your skills, this guide provides a detailed, step-by-step overview.
1. Tailwind CSS - Home
Tailwind CSS is a utility-first CSS framework that provides pre-designed classes to build modern, responsive UIs without writing custom CSS.
Why Use Tailwind CSS?
Utility-First: Design directly in your HTML.
Customization: Fully customizable with configuration files.
Responsive: Built-in support for responsive design.
Consistent: Eliminates CSS bloat and ensures design consistency.
2. Tailwind CSS - Roadmap
To become proficient in Tailwind CSS, follow this roadmap:
Learn basic HTML and CSS.
Understand the utility-first concept.
Explore core concepts and responsive design.
Learn to customize Tailwind through configuration.
Build real-world projects.
3. Tailwind CSS - Introduction
Tailwind CSS provides low-level utility classes that let you build designs quickly without leaving your HTML.
4. Tailwind CSS - Installation
There are multiple ways to install Tailwind CSS:
Using npm:
npm install -D tailwindcss
npx tailwindcss init
This creates a tailwind.config.js
file for customization.
CDN:
For quick prototyping, include the CDN link in your HTML:
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
5. Tailwind CSS - Editor Setup
To improve your development experience, install the Tailwind CSS IntelliSense extension in VSCode. It provides:
Autocomplete for utility classes.
Syntax highlighting.
Hover previews for classes.
6. Tailwind CSS - Core Concepts
Utility-First Fundamentals
Tailwind CSS is built around the utility-first design principle, meaning you use predefined classes to style your elements.
Example:
<div class="p-4 bg-blue-500 text-white">Hello, World!</div>
p-4
: Adds padding.bg-blue-500
: Sets the background color.text-white
: Sets the text color.
7. Hover, Focus, and Other States
Tailwind allows you to style elements based on their state using modifiers.
Example:
<button class="bg-blue-500 hover:bg-blue-700 focus:outline-none">Click Me</button>
hover:bg-blue-700
: Changes the background color on hover.focus:outline-none
: Removes the outline on focus.
8. Responsive Design
Tailwind makes responsive design easy with breakpoint prefixes:
Example:
<div class="p-4 sm:p-6 md:p-8 lg:p-10 xl:p-12">Responsive Padding</div>
sm
,md
,lg
,xl
: Define breakpoints.
9. Dark Mode
Tailwind supports dark mode using the dark
class.
Example:
<div class="bg-white dark:bg-black text-black dark:text-white">Dark Mode</div>
10. Reusing Styles
You can reuse styles by creating custom classes or using Tailwind's @apply
directive in your CSS files.
Example:
.btn {
@apply px-4 py-2 bg-blue-500 text-white rounded;
}
11. Adding Custom Styles
Tailwind allows you to add custom styles through configuration.
Example:
module.exports = {
theme: {
extend: {
colors: {
'custom-blue': '#1fb6ff',
},
},
},
}
12. Functions & Directives
Tailwind includes several powerful functions and directives, such as:
@tailwind
: Imports Tailwind's base, components, and utilities.@apply
: Applies utility classes in custom CSS.@layer
: Allows adding custom styles to specific layers.
Example:
@tailwind base;
@tailwind components;
@tailwind utilities;
13. Customization
You can customize almost everything in Tailwind through its configuration file.
Example:
module.exports = {
theme: {
extend: {
spacing: {
'72': '18rem',
'84': '21rem',
},
},
},
}
14. Plugins
Tailwind supports plugins to add additional utilities.
Example:
const plugin = require('tailwindcss/plugin');
module.exports = {
plugins: [
plugin(function({ addUtilities }) {
addUtilities({
'.skew-10deg': {
transform: 'skewY(-10deg)',
},
});
}),
],
}
15. Common Utility Classes
Spacing
m-4
: Marginp-4
: Padding
Sizing
w-1/2
: Width 50%h-screen
: Height 100% of viewport
Typography
text-lg
: Large textfont-bold
: Bold font
Colors
bg-red-500
: Red backgroundtext-green-600
: Green text
Borders
border
: Adds a borderrounded-full
: Fully rounded corners
16. Building a Navbar with Tailwind CSS
Tailwind makes building complex components like a navbar quick and easy.
Example Navbar:
<nav class="bg-gray-800 p-4">
<div class="container mx-auto flex justify-between items-center">
<a href="#" class="text-white text-lg font-bold">MyApp</a>
<div class="space-x-4">
<a href="#" class="text-gray-300 hover:text-white">Home</a>
<a href="#" class="text-gray-300 hover:text-white">About</a>
<a href="#" class="text-gray-300 hover:text-white">Contact</a>
</div>
</div>
</nav>
bg-gray-800
: Sets the navbar background color.container mx-auto
: Centers the content.flex justify-between items-center
: Creates a flex container with spaced items.space-x-4
: Adds horizontal spacing between links.
Responsive Navbar:
<nav class="bg-gray-800 p-4">
<div class="container mx-auto flex justify-between items-center">
<a href="#" class="text-white text-lg font-bold">MyApp</a>
<button class="block md:hidden text-white">Menu</button>
<div class="hidden md:flex space-x-4">
<a href="#" class="text-gray-300 hover:text-white">Home</a>
<a href="#" class="text-gray-300 hover:text-white">About</a>
<a href="#" class="text-gray-300 hover:text-white">Contact</a>
</div>
</div>
</nav>
block md:hidden
: Shows the menu button only on small screens.hidden md:flex
: Hides the links on small screens and shows them on medium and larger screens.
Final Tips
Practice regularly by building real-world projects.
Read official documentation to stay updated.
Experiment with customization and plugins.
With this detailed cheatsheet, you're equipped to go from a beginner to an expert in Tailwind CSS. Keep building, keep experimenting, and happy coding!