Be Careful with Inline Styles: Why It's a Bad Practice?
When you need to quickly color a button or tweak a margin, your hand instinctively reaches for style={{ ... }}
.
But inline styles are like fast food: quick and convenient, but harmful to the long-term health of your project.
Inline styles don’t support basic CSS features:
:hover
, :focus
, :active
)<button style={{ color: 'red' }}>:hover won't work without CSS!</button>
::before
, ::after
)@media
)@keyframes
)var(--color-primary)
)🙅♂️ These limitations make inline styles unsuitable for responsive design and rich interfaces.
Inline styles are injected directly into the DOM. That means:
Styles define appearance. Logic defines behavior.
Inline styles blur the line:
// bad
<div style={{ padding: 10, backgroundColor: 'blue' }}>Content</div>
// good
<div className="box">Content</div>
CSS is cascade-based. Inline styles have highest specificity and are hard to override:
.button {
color: green; /* won't override style={{ color: 'red' }} */
}
🙃 This complicates working with components and makes them rigid and fragile.
When styles live inside components:
Pseudo-classes like :hover
or :focus
don’t work inline — you end up hacking with useState/useEffect:
const [hover, setHover] = useState(false);
<button
style={{ color: hover ? 'red' : 'black' }}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
Hover me
</button>;
👎 Instead of one CSS line — a mini workaround.
Sometimes they are acceptable:
width: ${progress}%
)Inline styles aren’t evil. But:
📌 Use CSS modules, Emotion, Styled Components — anything but don’t stuff everything into style={{…}}.
”Inline styles are like fast food: quick, but bad for long-term project health” 🍔🚫.