Tailwindcss
Tailwind CSS Cheat-Sheet
Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined utility classes to quickly build responsive and customizable user interfaces. This cheat-sheet covers essential aspects of using Tailwind CSS in your web development projects.
Installation
- Install Tailwind CSS using npm or yarn:
npm install tailwindcss
- Create a configuration file (tailwind.config.js) to customize the framework:
npx tailwindcss init
- Include the Tailwind CSS styles in your project's CSS file:
@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
Basic Usage
Tailwind CSS provides a wide range of utility classes that you can directly apply to HTML elements. For example:
<div class="bg-blue-500 text-white p-4 rounded-lg">Hello Tailwind!</div>
In the above example, bg-blue-500
sets the background color to blue, text-white
sets the text color to white, p-4
adds padding of 4 units, and rounded-lg
adds rounded corners.
Responsive Design
Tailwind CSS offers responsive design utilities using breakpoints. For example:
<div class="text-center md:text-left">Centered text on small screens, left-aligned on medium screens and above.</div>
The md:text-left
class will apply the left alignment to the text on screens with a width of the medium breakpoint and above.
Spacing
Tailwind CSS provides spacing utilities to add margin and padding to elements:
<div class="m-4">Margin</div>
<div class="p-4">Padding</div>
You can use different variations like mt
, mb
, ml
, mr
, pt
, pb
, pl
, and pr
to apply margin and padding to specific sides.
Flexbox and Grid
Tailwind CSS includes flexbox and grid utilities to create responsive layouts:
<div class="flex justify-center items-center">Centered content with flexbox</div>
<div class="grid grid-cols-3 gap-4">Three-column grid layout</div>
Typography
Tailwind CSS provides typography utilities for text styling:
<h1 class="text-4xl font-bold">Large and bold text</h1>
<p class="text-gray-700">Gray text color</p>
Customization
You can customize Tailwind CSS by editing the tailwind.config.js file to add or modify colors, spacing, typography, and more:
module.exports = {
theme: {
extend: {
colors: {
customColor: '#ff0000',
},
spacing: {
'100': '25rem',
},
},
},
variants: {},
plugins: [],
};
Dark Mode
Tailwind CSS has built-in support for dark mode, which can be enabled using a class or JavaScript:
<!-- Enable dark mode using a class -->
<div class="dark:bg-gray-900">Dark mode background</div>
// Enable dark mode using JavaScript
document.documentElement.classList.add('dark');
Conclusion
This cheat-sheet covers the basics of using Tailwind CSS to quickly create responsive and customizable user interfaces. Tailwind CSS's utility-first approach provides a convenient way to style your web projects without the need for writing custom CSS. For more advanced usage and customization options, refer to the official Tailwind CSS documentation. Happy styling!