npm install tailwindcss
Add Tailwind to your CSS file:
@tailwind base;
@tailwind components;
@tailwind utilities;
Create a tailwind.config.js
file:
npx tailwindcss init
Customize your Tailwind configuration:
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
extend: {},
},
plugins: [],
}
<div class="container mx-auto">
<!-- Your content -->
</div>
Centers content and applies default max-widths.
<div class="flex justify-center items-center">
<!-- Your content -->
</div>
Applies flex display and centers items both horizontally and vertically.
<div class="grid grid-cols-3 gap-4">
<!-- Your content -->
</div>
Creates a grid with 3 columns and a gap of 4 units.
<div class="p-4">
<!-- Your content -->
</div>
Applies padding of 1rem (4 units).
<div class="m-4">
<!-- Your content -->
</div>
Applies margin of 1rem (4 units).
<div class="flex space-x-4">
<!-- Your content -->
</div>
Applies horizontal spacing of 1rem (4 units) between flex items.
<div class="w-1/2">
<!-- Your content -->
</div>
Sets the width to 50% of the parent element.
<div class="h-64">
<!-- Your content -->
</div>
Sets the height to 16rem (64 units).
<div class="text-lg">
<!-- Your content -->
</div>
Sets the font size to large (1.125rem).
<div class="font-bold">
<!-- Your content -->
</div>
Sets the font weight to bold.
<div class="text-center">
<!-- Your content -->
</div>
Centers the text.
<div class="text-blue-500">
<!-- Your content -->
</div>
Sets the text color to a shade of blue.
<div class="bg-red-500">
<!-- Your content -->
</div>
Sets the background color to a shade of red.
<div class="border border-green-500">
<!-- Your content -->
</div>
Sets the border color to a shade of green.
<div class="rounded-lg">
<!-- Your content -->
</div>
Applies large border-radius to the element.
<div class="border-4">
<!-- Your content -->
</div>
Sets the border width to 4 units.
<div class="shadow-lg">
<!-- Your content -->
</div>
Applies a large box shadow.
<div class="hover:bg-blue-500">
<!-- Your content -->
</div>
Changes background color to blue on hover.
<input class="focus:outline-none">
Removes the outline on focus.
<div class="sm:w-1/2">
<!-- Your content -->
</div>
Sets the width to 50% on small screens and up.
<div class="md:text-center">
<!-- Your content -->
</div>
Centers text on medium screens and up.
<div class="lg:p-8">
<!-- Your content -->
</div>
Applies padding of 2rem on large screens and up.
<div class="xl:flex">
<!-- Your content -->
</div>
Applies flex display on extra large screens and up.
Extend Tailwind's default theme:
module.exports = {
theme: {
extend: {
colors: {
customColor: '#123456',
},
},
},
}
Adds a custom color to the theme.
Add Tailwind plugins:
module.exports = {
plugins: [
require('@tailwindcss/forms'),
],
}
Includes the Tailwind CSS Forms plugin.
Enable dark mode:
module.exports = {
darkMode: 'class', // or 'media'
}
Allows toggling dark mode via a class.
<html class="dark">
<body class="bg-white dark:bg-black">
<!-- Your content -->
</body>
</html>
Sets up dark mode styles.
Add custom variants:
module.exports = {
variants: {
extend: {
backgroundColor: ['active'],
},
},
}
Adds an active
variant for background color.
<button class="bg-blue-500 active:bg-blue-700">
<!-- Your content -->
</button>
Changes background color on active state.