Tailwind
Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to build custom designs without leaving your HTML. It allows for rapid UI development by composing utilities directly in your markup.
Installation
To install Tailwind CSS, see
Tailwind CSS installation guide. It supports various build tools like PostCSS, Webpack, and Vite.
If you are using VS Code, you can install Tailwind CSS IntelliSense extension for better autocompletion and syntax highlighting.
Add an @import to your CSS file that imports Tailwind CSS.
1 | @import "tailwindcss"; |
Styling with utility classes
After the setup is completed. start using Tailwind’s utility classes to style your content
Button example:
1 | <button type="button" |
Input example:
1 | <input |
Background Color
To set the background color of an element, use the bg-{color}
utility class. For example, to set a blue background:
1 | <div class="p-4 bg-blue-500 text-white"> |
Text Color
To set the text color, use the text-{color}
utility class. For example, to set white text:
1 | <div class="p-4 bg-amber-300 text-white"> |
Margin and Padding
To add margin or padding, use the m-{size}
and p-{size}
utility classes. For example:
1 | <div class="m-4 p-6 bg-gray-200"> |
With direction utilities, you can specify margin or padding for specific sides:
1 | <div class="ml-4 mt-4 px-5 py-2 bg-gray-200 rounded-md"> |
Border
To add borders, use the border
utility class along with color and width utilities. For example:
1 | <div class="border border-gray-300 p-4"> |
Width and Height
To set the width and height of an element, use the w-{size}
and h-{size}
utility classes. For example:
1 | <div class="w-64 h-32 bg-green-500"> |
Font Size and Weight
To set the font size, use the text-{size}
utility class. For example:
1 | <div class="text-lg font-bold text-gray-800"> |
To set the font weight, use the font-{weight}
utility class. For example:
1 | <div class="text-base font-semibold"> |
Font Family
To set the font family, use the font-{family}
utility class. For example:
1 | <div class="font-sans text-gray-700"> |
Example values are font-sans
, font-serif
, and font-mono
. You can also use custom fonts by adding them to your Tailwind configuration.
Flexbox
To create a flex container, use the flex
utility class. You can also use flex-row
or flex-col
to set the direction of the flex items. For example:
1 | <div class="flex flex-row space-x-4"> |
ALign items along the main axis using justify-{content}
utility class:
1 | <div class="flex justify-between items-center p-4 bg-gray-200"> |
Align items along the cross axis using items-{alignment}
utility class:
1 | <div class="flex items-center h-32 bg-gray-300"> |
Grid
To create a grid layout, use the grid
utility class. You can specify the number of columns using grid-cols-{n}
. For example:
1 | <div class="grid grid-cols-3 gap-4"> |
Dark Mode
By default, Tailwind CSS uses prefers-color-scheme
to detect dark mode. You can use a css selector instead of prefers-color-scheme
.
override the dark variant to use your custom selector
1 | @custom-variant dark (&:where(.dark, .dark *)); |
To enable dark mode, add dark
class to the root element of your HTML. This will apply dark mode styles when the class is present.
1 | document.documentElement.classList.add('dark'); |
you can choose to enable or disable dark mode based on user preference or a local storage setting.
Then, you can use the dark:
prefix to apply styles in dark mode:
1 | <div class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white p-4"> |