HTML to JSX

Convert HTML into valid JSX. It renames attributes such as class and for, turns style strings into objects, closes void tags and wraps multiple roots in a fragment.

Use the tool

Why this tool exists

Copying a snippet from a design tool, a CSS framework example or an old template into React rarely works the first time. JSX is stricter than HTML: attributes are camelCased, class is reserved, void tags need a closing slash and there must be one root.

This tool applies those rules in one pass. It changes class to className and for to htmlFor, converts inline style into an object, camelCases attributes like tabindex and maxlength, self-closes elements such as input and br, and wraps several top-level elements in a fragment. Curly braces in text are escaped so they are not read as expressions. You can wrap the result in a component. Everything runs in your browser, and your markup is not uploaded.

How to use it

  1. Paste your HTML into the input box.
  2. Tick Wrap in component if you want a function component, and give it a name.
  3. Choose the indent for the output.
  4. Copy the JSX and check that any converted event handlers call functions that exist.

Frequently asked questions

What does it convert?

class to className, for to htmlFor, inline style strings to objects, hyphenated and lowercase attributes to camelCase, void tags to self-closing form, and comments to JSX comments.

Does it convert onclick handlers?

Yes, for common events. onclick="go()" becomes onClick={() => { go() }}, so the inline code is wrapped in an arrow function. Check that the called functions exist in your component.

What happens with several root elements?

They are wrapped in a fragment, written as an empty pair of angle brackets, because a JSX expression must have a single root.

Can I convert JSX back to HTML?

Yes. Use the JSX to HTML converter in this collection to turn className, htmlFor and style objects back into plain HTML attributes and strings.