Inline Styles in React

From the React Components & JSX cheat sheet · JSX Fundamentals · verified Jul 2026

Inline Styles

Apply styles using JavaScript objects

javascript
function StyledComponent() {
  const styles = {
    color: 'blue',
    fontSize: '20px',
    backgroundColor: '#f0f0f0',
    padding: '10px'
  }
  
  return (
    <div>
      {/* Style object */}
      <h1 style={styles}>Styled heading</h1>
      
      {/* Inline style object */}
      <p style={{ 
        color: 'red', 
        fontWeight: 'bold' 
      }}>
        Inline styled text
      </p>
      
      {/* Dynamic styles */}
      <div style={{
        backgroundColor: true ? 'green' : 'red',
        width: '100px',
        height: '100px'
      }} />
    </div>
  )
}
💡 Style attribute takes an object, not string
📌 Use camelCase for CSS properties
⚡ Values are strings or numbers (px assumed)
jsxstylescss

More React tasks

Back to the full React Components & JSX cheat sheet