Hey everyone! đ
Letâs talk about styling.
If youâve been writing React for a while, you know the struggle. You write a beautiful component, and then⊠you have to write a separate CSS file.
Or maybe you use CSS Modules, and your file names look like Button_button__1x2y3. đ”
âThere has to be a better way!â you scream at your monitor.
Well, there is. And itâs called Tailwind CSS.
Now, I know what youâre thinking. âIsnât that just inline styles with extra steps?â Hold your horses! đ Let me explain why React and Tailwind are actually a match made in heaven.
1. The Problem with âTraditionalâ CSS
Imagine you are building a button. In standard CSS, you have to:
- Create
Button.js. - Create
Button.css. - Think of a class name (is it
.btn-primaryor.button-main?). - Switch back and forth between files to check padding, colors, etc.
It breaks your flow. You are context-switching constantly.
2. Enter Tailwind CSS
With Tailwind, you write your styles right inside your JSX. It sounds messy, but once you get used to it, itâs incredibly fast.
Check this out:
// A standard button component
const Button = ({ text }) => {
return (
<button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
{text}
</button>
);
};
Look at that! No separate file. No made-up class names. You just read the classes and you know exactly what it looks like:
bg-blue-500: Blue background.hover:bg-blue-700: Darker blue on hover.rounded: Rounded corners.
Itâs readable, itâs portable, and you can copy-paste this component anywhere and it will look exactly the same.
3. Dynamic Styles
âBut wait,â you ask, âWhat if I need dynamic styles based on props?â Easy peasy. Since we are in JavaScript land, we can use template literals.
const Alert = ({ type, message }) => {
// Define base styles
const baseClass = "p-4 mb-4 text-sm rounded-lg";
// Define dynamic colors based on 'type' prop
const colorClass = type === 'error'
? "bg-red-100 text-red-700"
: "bg-green-100 text-green-700";
return (
<div className={`${baseClass} ${colorClass}`} role="alert">
<span className="font-medium">Notice:</span> {message}
</div>
);
};
See? We are just manipulating strings. React is good at that.
4. Donât Repeat Yourself (DRY)
One common complaint is: âMy HTML looks cluttered!â If you find yourself repeating the same long string of classes, just extract a component.
Donât use @apply in CSS files unless you really have to. React is your component framework. Use it!
// Instead of repeating classes on every button...
const PrimaryButton = ({ children }) => (
<button className="bg-indigo-600 text-white px-4 py-2 rounded shadow hover:bg-indigo-700">
{children}
</button>
);
// Now your main code is clean:
const App = () => (
<div>
<PrimaryButton>Save</PrimaryButton>
<PrimaryButton>Cancel</PrimaryButton>
</div>
);
Closing
Tailwind forces you to think in âdesign tokensâ (spacing, colors, typography) rather than âpixelsâ. And in React, where everything is a component, this approach feels incredibly natural.
So, give it a shot. Delete that .css file and embrace the utility classes.
Your Alt-Tab keys will thank you. đ
Happy Coding! đ