In React, dynamic styles let you change how components look based on state or props. This powers interactive UIs like active buttons, form validation states, or light/dark themes.
You can implement dynamic styling using inline style objects, conditional class names, and even CSS variables that update when state changes.
style prop.className.--custom-variables from React for global theming.The most common patterns for dynamic styling in React are:
<div style={boxStyle}>...</div> where boxStyle is a JS object computed from state.className={`btn ${active ? "active" : "inactive"}`} to switch between CSS rules.document.documentElement. style.setProperty() to change theme colors.transition in CSS with state toggles in React.Here, we bind a boxStyle object to the style prop and compute its values from the component state. The background color switches when the count reaches 5 or more.
// React component with inline dynamic styles based on count
import React, { useState } from "react";
function InlineDynamicExample() {
const [count, setCount] = useState(0);
const boxStyle = {
padding: "20px",
backgroundColor: count >= 5 ? "#28a745" : "#ffc107",
color: "#fff",
borderRadius: "8px",
textAlign: "center",
transition: "background-color 0.3s ease"
};
return (
<div style={boxStyle}>
<h4>Count: {count}</h4>
<button
className="btn btn-dark me-2"
onClick={() => setCount(count + 1)}
>
+</button>
<button
className="btn btn-secondary"
onClick={() => setCount(0)}
>
Reset</button>
</div>
);
}
export default InlineDynamicExample;
count starts at 0.count >= 5, the box background turns green, otherwise it stays yellow.Conditional class names are cleaner when you have more complex CSS. Here, we toggle between .active and .inactive styles.
/* Button styles with active/inactive states */
.btn {
padding: 10px 20px;
border: none;
border-radius: 4px;
color: white;
transition: background 0.3s ease;
}
.btn.active {
background-color: #28a745;
}
.btn.inactive {
background-color: #6c757d;
}
// React button that toggles active/inactive classes
import React, { useState } from "react";
import "./DynamicButton.css";
function DynamicButton() {
const [active, setActive] = useState(false);
return (
<div className="text-center">
<button
className={`btn ${active ? "active" : "inactive"}`}
onClick={() => setActive(!active)}
>
{active ? "Active" : "Inactive"}
</button>
</div>
);
}
export default DynamicButton;
active toggles between true and false.btn active and btn inactive.Using CSS variables, you can create powerful theme switches. React updates --theme-bg and --theme-color so the whole UI can respond.
// Theme toggle using CSS variables
import React, { useState, useEffect } from "react";
function ThemeToggle() {
const [dark, setDark] = useState(false);
useEffect(() => {
document.documentElement.style.setProperty(
"--theme-bg",
dark ? "#212529" : "#f8f9fa"
);
document.documentElement.style.setProperty(
"--theme-color",
dark ? "#f8f9fa" : "#212529"
);
}, [dark]);
const pageStyle = {
backgroundColor: "var(--theme-bg)",
color: "var(--theme-color)",
height: "150px",
display: "flex",
alignItems: "center",
justifyContent: "center",
transition: "all 0.3s ease"
};
return (
<div style={pageStyle}>
<button
className="btn btn-outline-info"
onClick={() => setDark(!dark)}
>
{dark ? "Light Mode" : "Dark Mode"}
</button>
</div>
);
}
export default ThemeToggle;
dark controls whether the theme is dark or light.useEffect updates the CSS variables on every change of dark.div reads the variables using var(--theme-bg) and var(--theme-color).Combine state with CSS transitions or keyframes to animate style changes smoothly. Here, a box grows and changes color when toggled.
// Animated box that expands using a CSS transition
import React, { useState } from "react";
import "./AnimatedBox.css";
function AnimatedBox() {
const [expanded, setExpanded] = useState(false);
return (
<div className="text-center">
<div className={`box ${expanded ? "expanded" : ""}`}></div>
<button
className="btn btn-primary mt-3"
onClick={() => setExpanded(!expanded)}
>
Toggle Box</button>
</div>
);
}
export default AnimatedBox;
/* Box transitions in size and color when expanded */
.box {
width: 100px;
height: 100px;
background-color: #0d6efd;
margin: auto;
border-radius: 8px;
transition: all 0.4s ease;
}
.box.expanded {
width: 200px;
height: 200px;
background-color: #198754;
}
expanded state.box and box expanded.className toggles and CSS transitions for smooth visual effects.Goal: Master dynamically styling React components using state and props to create interactive, responsive, and visually engaging user interfaces.