CSS to React Style Properties Converter
Convert plain CSS declarations into React-compatible JavaScript style objects with camelCase properties, numeric values, and vendor prefix handling.
About
React components use JavaScript objects for inline styles, not CSS strings. Every hyphenated CSS property must become camelCase (background-color → backgroundColor), every numeric pixel value should drop its unit suffix to become a raw Number, and vendor prefixes follow browser-specific capitalization rules (-webkit-transform → WebkitTransform, but -ms-transform → msTransform). Getting any of these wrong produces silent failures: the property is simply ignored by the browser at runtime with zero error feedback. This converter parses raw CSS declarations, strips comments, handles shorthand properties, converts units, and outputs a copy-paste-ready React style object or JSX spread.
The parser handles edge cases that manual conversion misses: content property strings stay quoted, unitless properties like line-height, opacity, and z-index remain numbers, and !important annotations are stripped because React's style API does not support them. This tool approximates the behavior of the react-css npm package's fromCSS function entirely client-side with no dependencies.
Formulas
The conversion pipeline applies three sequential transformations to each CSS declaration:
propertyTransform: kebab-case → camelCase
Split on - delimiter. For each segment si where i > 0, capitalize first character. Exception: vendor prefix -ms- keeps lowercase ms; all other prefixes (-webkit-, -moz-, -o-) capitalize the prefix letter.
valueTransform: string → number | string
If value matches pattern vpx and property is not in the unitless set, extract numeric v. If value is purely numeric and property is in the unitless set {zIndex, opacity, fontWeight, flex, order, lineHeight, …}, return Number(v). Otherwise return trimmed string.
sanitize: Strip !important annotations, remove CSS comments (/* … */), normalize whitespace.
Where property is the CSS declaration name and value is the declaration value after the colon. The output object maps propertyTransform(property) → valueTransform(value).
Reference Data
| CSS Property | React Property | Value Handling |
|---|---|---|
| background-color | backgroundColor | String (color value) |
| font-size | fontSize | Number if px, String otherwise |
| margin-top | marginTop | Number if px |
| z-index | zIndex | Number (unitless) |
| opacity | opacity | Number (unitless) |
| line-height | lineHeight | Number if unitless, String if unit |
| -webkit-transform | WebkitTransform | String |
| -moz-appearance | MozAppearance | String |
| -ms-transform | msTransform | String (lowercase ms) |
| -o-transition | OTransition | String |
| content | content | String (preserves quotes) |
| flex | flex | Number if single value, String if shorthand |
| border | border | String (shorthand preserved) |
| font-weight | fontWeight | Number if numeric (400, 700) |
| animation-delay | animationDelay | String (time unit preserved) |
| box-shadow | boxShadow | String (complex value) |
| background-image | backgroundImage | String (url() preserved) |
| grid-template-columns | gridTemplateColumns | String |
| padding | padding | Number if single px, String if multi |
| border-radius | borderRadius | Number if single px, String if multi |
| transition | transition | String (complex value) |