How to use Tailwind CSS in React
Implementing utility-first CSS with Tailwind in React applications provides rapid development, consistent design systems, and responsive layouts with minimal custom CSS. As the creator of CoreUI, a widely used open-source UI library, I’ve integrated Tailwind CSS in numerous React projects for rapid prototyping and consistent styling across enterprise applications. From my expertise, the most efficient approach is to install Tailwind CSS and configure it with PostCSS for optimal build integration. This method provides access to thousands of utility classes while maintaining excellent performance through automatic purging of unused styles.
Install Tailwind CSS and use utility classes in className attributes for rapid styling.
function Button({ children }) {
return (
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
{children}
</button>
)
}
Tailwind CSS provides utility classes that you apply directly to elements using the className
prop. Classes like bg-blue-500
for background color, text-white
for text color, and py-2 px-4
for padding create consistent styling without writing custom CSS. The utility-first approach enables rapid development, easy responsive design with prefixes like md:text-lg
, and consistent spacing using Tailwind’s design system. Install with npm install tailwindcss
and configure with npx tailwindcss init
.
Best Practice Note:
This is the same approach we use when integrating Tailwind with CoreUI React components for rapid customization.
Use Tailwind’s @apply
directive in CSS files for component-level abstractions, and configure the content
array in tailwind.config.js to enable proper purging of unused styles in production builds.