Styling in React
React CSS Tailwind styling ke tareeqe, className, inline style object, CSS Modules aur Tailwind CSS mein farq aur sahi choice Roman Urdu mein.
On this page
1. Styling in React kya hai?
React khud koi styling system nahi deta. Aap wahi CSS use karte hain jo web par chalti hai, bas JSX mein class ki jagah className likhte hain.
2. Kyun zaroori hai?
Components chhote aur reusable hain, to unki styling bhi organized honi chahiye. Ek global CSS file jaldi messy ho jati hai.
| Ek bari global CSS | Component-wise styling |
|---|---|
| Class names aapas mein takrate hain | Har component ki apni scoped classes |
| Kaunsa CSS kahan use hai, pata nahi | Style component ke saath rehta hai |
| Unused CSS jama hota rehta hai | Component hataya to style bhi gaya |
| Dynamic styling mushkil | State ke mutabiq class badalna aasaan |
3. Kahan use hota hai?
| Tareeqa | Kab behtar hai |
|---|---|
| Plain CSS + className | Chhoti app, global base styles |
| Inline style | Runtime dynamic values, jaise progress width |
| CSS Modules | Scoped classes, plain CSS likhna pasand ho |
| Tailwind CSS | Tez development, design system, zyada tar modern projects |
4. Kaise kaam karta hai?
Inline style mein CSS string nahi, ek JavaScript object jata hai. Property names camelCase hote hain: backgroundColor, fontSize.
5. Code example
className aur conditional class
import "./Button.css";
function Button({ variant = "primary", children }) {
return <button className={`btn btn-${variant}`}>{children}</button>;
}Inline style (dynamic value)
function ProgressBar({ percent }) {
return (
<div className="track">
<div style={{ width: `${percent}%`, backgroundColor: "teal" }} />
</div>
);
}CSS Modules
/* Card.module.css */
.card {
padding: 16px;
border-radius: 8px;
}import styles from "./Card.module.css";
function Card({ children }) {
return <div className={styles.card}>{children}</div>;
}Tailwind CSS
function Badge({ active }) {
return (
<span
className={`rounded-full px-3 py-1 text-sm ${
active ? "bg-green-600 text-white" : "bg-gray-200 text-gray-700"
}`}
>
{active ? "Active" : "Inactive"}
</span>
);
}Code kaise kaam kar raha hai?
| Cheez | Matlab |
|---|---|
className | HTML ka class, JSX mein reserved word ki wajah se naya naam |
style={{ ... }} | Bahar wale braces JSX ke, andar wale object ke |
styles.card | CSS Module unique class name bana deta hai |
| Template string | State ya props ke mutabiq class jorna |
| Tailwind classes | Har class ek chhota CSS rule |
Tailwind v4 mein Vite ke saath setup simple hai: @tailwindcss/vite plugin lagao aur CSS file mein @import "tailwindcss"; likho.
6. Common mistakes
Galati: JSX mein class likhna
Why? JSX JavaScript hai aur class ek reserved word hai, React className expect karta hai.
Behtar: Hamesha className likho.
Galati: Inline style mein CSS string dena, jaise style='color: red'
Why? React style prop mein object chahta hai, string par error aata hai.
Behtar: style={{ color: 'red' }} likho aur property names camelCase rakho.
Galati: Har cheez inline style se karna
Why? Hover, media queries aur pseudo classes inline style mein nahi chalte aur code bhaari ho jata hai.
Behtar: Static styling class se karo, inline style sirf runtime dynamic values ke liye.
Galati: Tailwind class names ko string jor kar dynamically banana, jaise bg- plus color
Why? Tailwind build ke waqt poore class names dhoondta hai, adhoore toote names ki CSS generate nahi hoti.
Behtar: Poore class names likho aur condition se unmein se choose karo.
7. Pehle aur baad mein kya seekhein?
| Stage | Chapters |
|---|---|
| Pehle | JSX, Setup with Vite |
| Saath mein | Conditional Rendering, Props |
| Baad mein | TypeScript with React |
Final quick revision
| Point | Detail |
|---|---|
| Class | className |
| Inline | Object, camelCase properties |
| Scoped CSS | *.module.css |
| Utility first | Tailwind CSS |
| Dynamic | Condition se poori class choose karo |