⚛️ React ? Inline CSS ⚡ Dynamic Styling
Inline CSS in React lets you style elements directly using the style attribute. Instead of writing styles in separate CSS files, you define them as JavaScript objects and pass them into JSX.
This is especially useful for dynamic styling, where style values depend on component state or props, such as active buttons, theme toggles, or validation states.
style in JSX expects an object, not a string.backgroundColor instead of background-color....).In React, inline styles are passed as a JavaScript object to the style prop:
"10px", "red").
// Generic pattern for using inline styles in JSX
const styles = {
backgroundColor: "lightblue",
padding: "12px",
borderRadius: "6px"
};
function MyComponent() {
return (
<div style={styles}>Hello with inline styles</div>
);
}
Inline styles in React are defined as objects, where property names are written in camelCase instead of kebab-case. You pass this object directly to the style attribute.
// Simple component using inline styles on a div
function InlineExample() {
return (
<div
style={{
backgroundColor: "lightblue",
color: "darkblue",
padding: "15px",
borderRadius: "8px",
textAlign: "center"
}}
>
<h4>Inline CSS Example</h4>
<p>This element is styled directly using the style attribute.</p>
</div>
);
}
Notice that properties like background-color become backgroundColor in JSX.
It is often cleaner to define styles in a variable or constant, especially when you reuse the same styles in multiple places.
// Reuse a style object across the component
function ObjectStyleExample() {
const boxStyle = {
border: "2px solid #007bff",
borderRadius: "5px",
padding: "10px",
color: "#333",
backgroundColor: "#eaf3ff",
textAlign: "center"
};
return (
<div style={boxStyle}>
<h5>Using a Style Object</h5>
<p>Style objects make code cleaner and reusable.</p>
</div>
);
}
The style attribute accepts a JavaScript object, not a string — this allows programmatic control and reuse of styles.
You can dynamically change styles based on component state or user actions, such as toggling between two button colors.
// Change button color based on isActive state
function DynamicStyleExample() {
const [isActive, setIsActive] = React.useState(false);
const buttonStyle = {
padding: "10px 20px",
backgroundColor: isActive ? "#28a745" : "#6c757d",
color: "#fff",
border: "none",
borderRadius: "4px",
cursor: "pointer"
};
return (
<div className="text-center">
<button
style={buttonStyle}
onClick={() => setIsActive(!isActive)}
>
{isActive ? "Active" : "Inactive"}
</button>
</div>
);
}
Inline styling works perfectly with React’s state-driven rendering to create interactive UI effects like toggles, highlights, and conditional emphasis.
You can merge multiple style objects using the ... spread operator. This lets you keep base styles separate from variant styles (success, warning, error, etc.).
// Combine base styles with variant styles using the spread operator
function MergeStyleExample() {
const baseStyle = { padding: "10px", color: "#fff" };
const successStyle = { backgroundColor: "green" };
const warningStyle = { backgroundColor: "orange" };
const [mode, setMode] = React.useState("success");
const style =
mode === "success"
? { ...baseStyle, ...successStyle }
: { ...baseStyle, ...warningStyle };
return (
<div className="text-center">
<h5 style={style}>
{mode === "success" ? "Success Mode" : "Warning Mode"}
</h5>
<button
className="btn btn-outline-dark mt-2"
onClick={() => setMode(mode === "success" ? "warning" : "success")}
>
Toggle Mode
</button>
</div>
);
}
This approach keeps styles modular and allows easy combination of reusable objects while still remaining expressive and readable.
style.boxStyle object to keep JSX cleaner and styles consistent.In a real React app, placing these components inside a root component (and rendering with ReactDOM.render or createRoot) would show each of these behaviors visually.
:hover or :focus directly.buttonPrimary or alertWarning to make code self-documenting.<div>.type as a prop and uses inline styles to change its color.Goal: Learn to use inline CSS effectively for component-level and dynamic styling in React using JavaScript objects, state, and props.