Inline Styles

Subject: React

Inline Styles in React — Detailed Explanation

šŸ” What Are Inline Styles?

Inline styles in React are applied using the style prop on a JSX element.
They are written as JavaScript objects with camelCased property names (not kebab-case like in regular CSS).

šŸ‘‰ Inline styles are useful for quick, dynamic, or component-specific styling — especially when styles depend on variables or conditions.


āœ… Basic Syntax


🧠 Key Differences from Regular CSS

Traditional CSSReact Inline Style
background-colorbackgroundColor
font-sizefontSize
border-radiusborderRadius
Units like 20pxMust be a string
!importantāŒ Not supported

āœ… Full Example: Card with Inline Styling


āœ… Dynamic Styles with Inline Logic

Inline styles work well with state or props for dynamic changes.

šŸ”§ Example: Change Color Based on State


āœ… Hover Effects with Inline Styles?

āŒ Inline styles do not support pseudo-selectors like :hover, :focus, or @media.

āœ… Workaround: use state to simulate hover.

šŸ”§ Simulating Hover With State


ā— When to Use Inline Styles

āœ… Good ForāŒ Not Good For
Quick prototypingLarge, complex UI projects
One-off component stylingGlobal theming / style reuse
Dynamic or conditionally styled UIResponsive design, media queries
Simple conditional appearancePseudo-classes like :hover, :focus

šŸ“Œ Summary Table

FeatureDescription
style={{ key: val }}Apply inline styles using a JS object
camelCase keysUse backgroundColor instead of background-color
String valuesUnits like 20px must be quoted
No pseudo-classes:hover, :focus, @media not supported
Dynamic stylingCan use props, state, or logic inside JSX

šŸ’¬ Final Thoughts

  • āœ… Great for quick, dynamic, or one-off styles
  • āŒ Not ideal for large apps, responsive UI, or reusable styles

For production apps, prefer CSS Modules, Styled Components, or Tailwind CSS for better scalability and maintainability.