Codecademy Logo

Intermediate React

React CSS Styles

React supports inline CSS styles for elements. Styles are supplied as a style prop with a JavaScript object.

// Passing the styles as an object
const color = {
color: 'blue',
background: 'sky'
<h1 style={color}>Hello</h1>
// Passing the styles with an inline object, as a shorthand
<h1 style={{ color: 'red' }}>I am red!</h1>

Style Names And Values

In React, style names are written in “camelCase”, unlike in CSS where they are hyphenated. In most cases, style values are written as strings. When entering numeric values, you don’t have to enter px because React automatically interprets them as pixel values.

// Styles in CSS:
// font-size: 20px;
// color: blue;
// Would look like this style object in React:
const style = {
fontSize: 20,
color: 'blue',

Controlled vs. Uncontrolled Form Fields

In React, form fields are considered either uncontrolled, meaning they maintain their own state, or controlled, meaning that some parent maintains their state and passes it to them to display. Usually, the form fields will be controlled.

The example code shows an uncontrolled and controlled input.

const uncontrolledInput = <input />;
const controlledInput = (
<input value={stateValue} onChange={handleInputChange} />

Controlled Components

A controlled form element in React is built with a change handler function and a value attribute.

const controlledInput = (
<input value={stateValue} onChange={handleInputChange} />