Inline Styles and Style Objects0%

Inline Styles and Style Objects

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

Inline Styles and Style Objects

1. Inline Styles in React

In traditional HTML, inline styles are declared as semicolon-delimited text strings:

HTML
<div style="background-color: navy; color: white; padding: 12px;">
Standard HTML
</div>

In React JSX, inline styles are passed as JavaScript objects.

This gives styles the full power of JavaScript: dynamic variables, ternary operators, and computed calculations can be seamlessly blended into your component presentation.

JSX
import React from 'react';
 
export default function ProgressBar({ percentage }) {
// Style object defined as a JavaScript variable
const containerStyle = {
height: '16px',
width: '100%',
backgroundColor: '#e2e8f0',
borderRadius: '8px',
overflow: 'hidden'
};
 
const fillStyle = {
height: '100%',
width: `${percentage}%`,
backgroundColor: percentage >= 100 ? '#10b981' : '#3b82f6',
transition: 'width 0.3s ease-in-out'
};
 
return (
<div style={containerStyle}>
<div style={fillStyle} />
</div>
);
}

2. Converting CSS Properties to camelCase

Because property keys in JavaScript objects cannot contain hyphens without being wrapped in quotes, React converts CSS property names to camelCase:

Standard CSSReact JSX Style Key
background-colorbackgroundColor
font-sizefontSize
margin-topmarginTop
border-radiusborderRadius
z-indexzIndex
box-shadowboxShadow

3. Automatic Pixel Units for Numbers

When passing numeric values to dimension properties (such as width, height, margin, padding, fontSize), React automatically appends the 'px' suffix for you:

JSX
// React automatically appends 'px' to numbers for dimension properties:
<div style={{ width: 300, height: 150, padding: 20 }}>
Box with automatic px units (width: 300px, height: 150px, padding: 20px)
</div>
 
// For unitless properties like zIndex, opacity, or flex, React leaves them as unitless numbers:
<div style={{ opacity: 0.8, zIndex: 10, flex: 1 }}>
Unitless properties remain unchanged
</div>
 
// For other units (% , rem, em, vh), you must pass a string:
<div style={{ width: '50%', fontSize: '1.25rem', height: '100vh' }}>
Explicit string units
</div>

4. Pros and Cons of Inline Styles

Pros:

  • Scoped to the element: Zero risk of style name collisions with other components.
  • Dynamic Values: Ideal for styles driven by runtime data (e.g. progress bars, dynamic coordinates, drag-and-drop offsets).

Cons:

  • No Pseudo-classes or Pseudo-elements: Inline styles cannot define :hover, :focus, :active, ::before, or ::after.
  • No Media Queries: Responsive breakpoints (@media (max-width: 768px)) cannot be written in inline style objects.
  • Performance: Creating new style objects on every render creates minor garbage collection overhead compared to static CSS class names.

Interactive Quiz

Practice Quiz

Question 1
How are CSS property names written inside React style objects?
Question 2
What unit does React automatically append when a number is passed to a dimension property like width: 250?
Question 3
Which CSS features CANNOT be implemented using standard React inline styles?
Question 4
Why are inline styles ideal for a dynamic progress bar or drag-and-drop position?
Question 5
How do you specify percentages or rem units in an inline style object?
Next Lesson

CSS Stylesheets and Global Class Names

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext