React style object to CSS (and CSS to React)

Paste a React or React Native style object and get plain CSS (px on numbers, vendor prefixes, StyleSheet.create as rules), or paste CSS and get the

loading React style object to CSS (and CSS to React)
About this tool

What is a React style object to CSS converter?

React does not take CSS strings in the style prop. It takes an object whose keys are camelCase (backgroundColor, not background-color) and whose numbers mean pixels. That difference is small, but it bites every time you copy a snippet from a design file, from Stack Overflow or from a stylesheet into a component, or the other way around. This tool converts in both directions: a React or React Native style object becomes plain CSS, and CSS declarations or rules become a style object you can paste into JSX.

It handles the cases that make hand conversion tedious. Plain numbers get px, except for unitless properties such as lineHeight, opacity, zIndex, flexGrow and fontWeight, and except zero. Vendor prefixes follow React's naming: WebkitTransition becomes -webkit-transition and msTransform becomes -ms-transform. Custom properties such as '--accent' pass through untouched. A StyleSheet.create({ ... }) call becomes one CSS rule per key.

The object is never executed. A small parser reads strings, numbers, nested objects and arrays, so nothing you paste can run code, and nothing leaves your browser. The price of that safety is that variables and expressions are refused with a clear message, because the tool cannot know their value.

How to convert a style object or CSS

  1. Pick the direction in "Direction": "React style to CSS" or "CSS to React style". The example text switches with it until you type your own.
  2. Paste the input. In the first direction you can paste the bare object, style={{ ... }}, const x = { ... }; or a whole StyleSheet.create call. In the other, paste declarations or rules.
  3. For "React style to CSS", set "Selector for the rule" if you want something other than .element.
  4. For "CSS to React style", choose "Quotes" (single or double), "Wrapped as" (object only, style={...} or const) and whether to tick "Write px values as numbers (16px becomes 16)".
  5. Read the result in the output box. If the input is wrong, a message gives the line and column.
  6. Copy the result with the copy button in the output box.
  7. Use "Use the result as the input" to flip it back and check the round trip.

Examples

A style object to CSS

Input:

{ backgroundColor: '#0f172a', color: 'white', fontSize: 18, lineHeight: 1.6, padding: 16, margin: 0, opacity: 0.8, zIndex: 10 }

Output:

.element {
  background-color: #0f172a;
  color: white;
  font-size: 18px;
  line-height: 1.6;
  padding: 16px;
  margin: 0;
  opacity: 0.8;
  z-index: 10;
}

Note that fontSize and padding got px, while lineHeight, opacity, zIndex and the zero margin did not.

StyleSheet.create to several rules

Input: StyleSheet.create({ card: { padding: 8, borderRadius: 12 }, 'title-text': { fontSize: 20, fontWeight: 700, transform: [{ rotate: '45deg' }, { translateX: 10 }] } }).

.card {
  padding: 8px;
  border-radius: 12px;
}

.title-text {
  font-size: 20px;
  font-weight: 700;
  transform: rotate(45deg) translateX(10px);
}

The React Native transform list becomes a single CSS transform value.

CSS to a style object

Input:

background-color: #fff;
font-size: 14px;
line-height: 1.4;
-webkit-transition: opacity 200ms ease;
float: left;
--accent: #ffb224;

Output with single quotes and px as numbers:

{
  backgroundColor: '#fff',
  fontSize: 14,
  lineHeight: 1.4,
  WebkitTransition: 'opacity 200ms ease',
  cssFloat: 'left',
  '--accent': '#ffb224',
}

Two rules, .btn-primary { color: red; margin: 0 auto } and .card .title { font-size: 2em }, with "Wrapped as" set to const, give const styles = { btnPrimary: {...}, cardTitle: {...} };. Values that are not a plain number stay as strings, such as '0 auto' and '2em'.

Common mistakes

  • Pasting a variable or expression. { color: theme.color } is refused: the tool says "theme.color" is a variable or an expression, because it cannot know the value. Replace it with the final text first.
  • Joining strings with +. "1" + "px" is code. Write "1px".
  • Template literals with ${...}. They are code too. Use a plain string.
  • Forgetting quotes on values. { color: red } fails because red reads as a variable. Write 'red'.
  • Pasting @media or @keyframes into CSS to React. A style object cannot hold them, so the tool says only plain rules are supported. Move those parts to a stylesheet.
  • A declaration without a colon. color red is reported as missing its colon.
  • Expecting px on unitless properties. lineHeight: 24 is 24 times the font size, not 24 pixels. Write '24px' if you mean pixels.

Frequently asked questions

Why does React want camelCase in style objects?

The style prop maps to the DOM element.style object, whose properties are camelCase because a dash is not valid in a JavaScript identifier. Custom properties are the exception and keep their dashes, so they need quotes.

Is my code sent to a server?

No. The conversion runs in your browser and the object is parsed, not evaluated. You can use it with private code.

Can I use the result in React Native?

Mostly. Numbers in React Native are density-independent units, not CSS pixels, and some CSS properties and values do not exist there. Use the object as a starting point and check the properties against the React Native style reference.

What about rem and other units?

Strings are kept as you wrote them. To convert between rem and px first, use the REM to PX converter. For SVG icons in components, see SVG to JSX, and to embed an image as a CSS background, try image to CSS.

Report a problem