Course contentsShow
← React
Lesson 1895 of 2,39644. Styling ApproachesFree lesson

The style Prop: Object Syntax

How React's style prop expects a JavaScript object with camelCased properties, and why it differs from HTML style attributes.

The style Prop: Object Syntax

What you'll learn: How React's style prop differs from HTML's style attribute by requiring a JavaScript object with camelCased properties.

Why React Uses Objects for Styles

In HTML, you write inline styles as a string: <div style="background-color: blue; font-size: 16px;">. React takes a different approach—the style prop expects a JavaScript object, not a string. This keeps everything consistent with JavaScript syntax and allows React to validate and optimize styles.

The Object Syntax Rules

When using React's style prop, you must follow these conventions:

1. Pass an object with double curly braces:

<div style={{ backgroundColor: 'blue', fontSize: 16 }}>

The outer curly braces mean "JavaScript expression," and the inner braces define the object literal.

2. Property names must be camelCased: CSS's background-color becomes backgroundColor. CSS's font-size becomes fontSize. This matches JavaScript's property naming convention, since hyphens aren't valid in object keys without quotes.

3. Numeric values default to pixels: fontSize: 16 automatically becomes 16px. For other units, use strings: width: '50%' or margin: '2em'.

Think of It Like JSON

Imagine you're describing styles to JavaScript, not CSS. You're creating a configuration object that React translates into actual DOM styles. This object-based approach enables JavaScript logic within your styles—perfect for dynamic values or calculations.

Key Takeaway: React's style prop requires a JavaScript object with camelCased property names (not a string), allowing seamless integration with JavaScript expressions and making styles programmable within your components.