How to Use Tailwind CSS for Styling: A Practical Guide
Tailwind CSS is a utility-first framework that lets you style HTML directly with classes like flex, text-lg, and shadow-md. Instead of switching between HTML and CSS files, you compose design systems right in your markup, making development faster and more consistent.
To get started, install Tailwind via npm or include the Play CDN link in your page. Then, configure your theme in the tailwind.config.js file and import Tailwind’s base, components, and utilities layers.
1. Style with Utility Classes
Apply utilities directly to elements. For example, class="bg-blue-500 text-white p-4 rounded-lg" creates a blue button with padding and rounded corners. This covers most layout, spacing, typography, and color needs without custom CSS.
2. Build Responsive Layouts Easily
Tailwind includes breakpoint prefixes such as sm, md, lg, and xl. Use them to create adaptive designs: class="text-base md:text-lg lg:text-xl" scales text as the viewport widens. Mobile-first is the default.
3. Customize Your Design System
Extend the default theme in tailwind.config.js to add brand colors, custom fonts, spacing, and animations. Since Tailwind scans your files, only the classes you use are compiled—keeping your final CSS extremely small.
4. Reuse Styles with @apply
For repeated patterns, group utilities into components using @apply in your CSS file:
@apply bg-blue-500 text-white px-4 py-2 rounded;
Conclusion
Tailwind CSS makes styling intuitive and maintainable. Start with utility classes, then explore customization and component extraction as your project grows.