Like this:

function Component() {
  const style = { "--my-css-var": 10 } as React.CSSProperties;
  return <div style={style}>...</div>
}

Or without the extra style variable:

function Component() {
  return <div style={{ "--my-css-var": 10 } as React.CSSProperties} />
}
Answer from ixrock on Stack Overflow
🌐
Veera
veerasundar.com › blog › setting-css-variables-from-react
Setting CSS variables from a React app - Blog | Veera
June 19, 2023 - This way, I can pass in whatever the custom props to the underlying CSS variable. Manually constructing the style object will not scale well if you have 10s of custom attributes. In such cases, we can write a simple function that construct the style object for us. We can also add any validation to the code. /** my-custom-button.js **/ import styles from "./my-custom-button.module.css"; /** * * @param props react props object with key, value pairs * @param variableNames array of names, for ex.
Discussions

How can I use dynamic css variables in react?
Trying to do a drop-shadow hover effect in react. I thought I'd use inline style attribute so each icon can have a different color when it's hovered over. I'm stuck on how to achieve this. In my More on stackoverflow.com
🌐 stackoverflow.com
Inline CSS Variables
TypeScript right? Try: style={{ ‘--item-color’: props.color } as React.CSSProperties} More on reddit.com
🌐 r/reactjs
13
7
May 14, 2022
reactjs - update CSS variables in React dynamically? - Stack Overflow
You can't change CSS, you can make changes using Javascriptn React in your case, set a variable that will make call to your red color class, and that can be updated to use the class using the blue one .. More on stackoverflow.com
🌐 stackoverflow.com
Help with dynamic CSS properties with JS/React? How come 1 of these 2 examples don't work?
Could you try the following?: document.documentElement.style.setProperty( "--width-size", `${event.currentTarget.value}px` ); More on reddit.com
🌐 r/learnjavascript
5
1
July 27, 2022
🌐
Space Jelly
spacejelly.dev › posts › how-to-create-css-custom-properties-that-dynamically-update-with-react-javascript
How to Create CSS Custom Properties That Dynamically Update with React & JavaScript on Space Jelly
January 8, 2023 - In Create React App, the src/index.css file is loaded globally, so let’s open that file and add our first Custom Property definition. ... We’re defining the same color that the logo is currently set to, but we’re adding it to a variable scoped to the :root of the document.
🌐
Epic React
epicreact.dev › css-variables
Use CSS Variables instead of React Context | Epic React by Kent C. Dodds
July 9, 2024 - With CSS Variables, you can get the values in your CSS with var(--css-variable-name) and in your JavaScript using getComputedStyle(element).getPropertyValue('--css-variable-name') (which you really don't need to do...) Ok, let's look at some code. Here's an approach to using emotion's ThemeProvider: ... What's cool about this is it's "just JavaScript" so you get all the benefits of variables etc. But we're not really doing all that much with this other than passing it around through the ThemeProvider. (To be clear, the way the ThemeProvider works is it uses React's Context API to make the theme accessible to all emotion components without having to pass props all over the place).
🌐
Josh W. Comeau
joshwcomeau.com › css › css-variables-for-react-devs
How to use CSS variables with React • Josh W. Comeau
April 13, 2020 - But what if we kept the CSS properties the same, and changed the values? Inside our components, min-height always points to the same value, --min-tap-target-height, but that value is dynamic. ... By consolidating the breakpoint stuff in a single place, we now have a single source of truth. Before, it was possible for a wayward developer to accidentally delete one of the breakpoints, leading to inconsistent behavior. Now it's packaged into a resilient variable.
🌐
YouTube
youtube.com › watch
CSS Custom Properties that Dynamically Update with React & JavaScript - YouTube
Learn how to create CSS Variables (Custom Properties) and dynamically update them in a React application. We'll walk through spinning up a new React app, cre...
Published   October 21, 2021
Find elsewhere
🌐
Reddit
reddit.com › r/reactjs › inline css variables
r/reactjs on Reddit: Inline CSS Variables
May 14, 2022 -

I am trying to create a component that sets CSS Variables using the inline style attribute. Example:

<div style={{'--item-color': props.color}}>...</div>

Is this possible with react? I can't find any documentation or previous conversation on the topic. All I can find is using DOM CSS properties like backgroundColor: ....

The above example would give the following error:

Type '{ '--item-color': string; }' is not assignable to type 'Properties<string | number, string & {}>'.Object literal may only specify known properties, and ''--item-color'' does not exist in type 'Properties<string | number, string & {}>'.ts(2322)

🌐
Webscale
section.io › home › blog
Dynamically Update React and JavaScript with CSS ...
June 24, 2025 - Get the latest insights on AI, personalization, infrastructure, and digital commerce from the Webscale team and partners.
🌐
Medium
medium.com › @krandles › adding-dynamic-themes-to-a-react-app-using-css-variables-57957e39f0bf
Adding Dynamic Themes to a React App Using CSS Variables | by Kevin Randles | Medium
July 16, 2018 - First, adding a few key/value pairs to my state (the theme colors will eventually be defined in another file, since they don’t really belong in the state, but this works for the moment) — the currently selected theme’s name, and the variables for my first two themes. Next, I wrote a function that iterates over the object that corresponds to the currently selected theme, and sets the CSS variables to their intended values.
🌐
JavaScript in Plain English
javascript.plainenglish.io › using-css-variables-to-set-themes-for-react-components-953e2f0cb4a5
Using CSS Variables to Set Themes for React Components | by Brandon Evans | JavaScript in Plain English
July 19, 2023 - It uses the setProperty method to set new values for the CSS variables. This function is called whenever the theme changes using the useEffect hook. By dynamically updating the CSS variables, the Button component, and any other themeable components, ...
🌐
LogRocket
blog.logrocket.com › home › using css variables in react native
Using CSS variables in React Native - LogRocket Blog
June 4, 2024 - You can set or retrieve CSS variables programmatically through JavaScript · Let’s assume that you have a React Native application where you want to apply CSS styles to the paragraph element and Text component.
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › how-to-dynamically-update-scss-variables-using-reactjs
How to dynamically update SCSS variables using ReactJS? - GeeksforGeeks
July 23, 2025 - "#fff" : "#262833" ); }, [darkTheme]); const URL = "https://media.geeksforgeeks.org/" + "wp-content/uploads/20190918121833/geeksforgeeks-62.png"; return ( <> <div className="card"> <img className="image" src={URL} alt="geeksforgeeks" /> <div className="cardBody"> <h2> Dynamically changing scss variable using react{" "} </h2> <p> According to Wikipedia sass is a preprocessor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). </p> <button onClick={() => setDarkTheme(!darkTheme) } > {darkTheme ?
🌐
Patrick Desjardins
patrickdesjardins.com › blog › react-css-variable-component-css-files
Patrick Desjardins Blog - How to use CSS variables in React components and CSS files
CSS variables are usable in any old and new mechanism and are, in a way, future-proof. Also, it is framework agnostic allowing React or SolidJS, or Angular to consume the values. With the capability to change the value dynamically with CSS conditions, like with an attribute value change, the ...
🌐
Nikita Hlopov
nikitahl.com › handling-css-variables-with-react
Handling CSS variables (Custom Properties) in React
September 17, 2019 - So the main concept is to set the desired CSS properties to an object conditionally and then assign this object to a style attribute of an element.
🌐
Medium
medium.com › geekculture › using-reacts-state-to-update-css-dynamically-c9b45570340c
Using React’s state to update CSS dynamically | by Michael Sutton | Geek Culture | Medium
September 3, 2021 - And while yes, they do influence the display of the items on the page, what is of importance to dynamically change the colors on the page are lines 32 and on. As you can see, the class names are the same names contained within our array in App.js. I went ahead and modified the actual colors to display a more soothing set vs. the blinding generic green, red, and blue. If you execute. "npm start” This is what you should see in the browser window. ... So there you have it how you can use React’s state to modify CSS dynamically.
🌐
Thomasledoux
thomasledoux.be › blog › tailwind-setting-using-css-variables-react
Setting and using CSS variables in Tailwind with React
I add a prop “variant” to the component, which will be used to set the CSS variables. If the variant prop has the value “highlighted”, the component will change the variables for the CTA’s background color and text color using the [--VARIABLENAME:VALUE] syntax.
🌐
Cloudzilla
cloudzilla.ai › dev-education › dynamically-update-react-and-javascript-with-css-variables
Dynamically Update React and JavaScript with CSS Variables
November 24, 2021 - Level up your skills with Cloudzilla Dev Education. Explore expert tutorials on cloud computing, full-stack development, AI integration, and scalable app deployment.
🌐
GitHub
github.com › jide › react-css-variables
GitHub - jide/react-css-variables: A React component to set CSS variables.
It allows to update CSS of underlying components without any DOM operation. The HOC won't trigger a render if only one of the variables is changed. This can be a huge performance improvement if you have a component with a deep render tree, since ...
Starred by 92 users
Forked by 5 users
Languages   JavaScript 100.0% | JavaScript 100.0%