HTML to JSX: className, Style Objects and Event Handlers

Last updated: · 6 min read

Paste HTML from a template, a design tool export or a CSS framework's documentation into a React component and the compiler objects at once: class is not accepted, <input> is never closed, style cannot be a string. The reason is that JSX is not HTML. It is syntax written inside JavaScript that compiles to JavaScript objects, and it follows JavaScript's rules. The HTML to JSX Converter takes care of the mechanical differences: it renames attributes, turns style strings into objects and closes tags. There are things it cannot do. It cannot translate inline event code into a React function, and it cannot know which piece of state a form field belongs to. This guide explains the rules along with the reasons behind them and points out what is left for you to finish. The output in the example is the tool's real result.

Formula

Rules:
1. The HTML is parsed the way browsers parse it: tag and attribute names are lower-cased and unclosed tags are repaired.
2. class → className and for → htmlFor; names such as tabindex → tabIndex, maxlength → maxLength, readonly → readOnly and colspan → colSpan are changed to what React expects. data-* and aria-* attributes stay as they are.
3. style="margin-top: 8px" becomes the object style={{ marginTop: "8px" }}; custom properties of the form --name are kept as quoted keys.
4. Void elements such as img, input, br and hr, and elements with no content, are self-closed: <br />.
5. Boolean attributes without a value stay bare (disabled, required). On input, value → defaultValue and checked → defaultChecked; the content of a textarea moves to defaultValue.
6. Comments become {/* … */}; { and } in text are written as {"{"} and {"}"}; meaningful spaces that JSX would swallow at a line end are preserved with {" "}.
7. Inline handlers such as onclick="…" become an empty function, onClick={() => { /* old code */ }}, and a warning line is appended to the output.
8. Several root nodes are wrapped in <>…</>. Attributes that cannot be written in JSX (such as @click or v-on:click) are skipped and listed in a warning line.

Input:
<div class="card" style="margin-top: 8px; background-color: #fff">
  <label for="email">Email</label>
  <input type="email" id="email" value="a@b.co" maxlength="80" required>
  <!-- note -->
  <button class="btn" tabindex="0" onclick="send()" disabled>Send</button>
</div>
<p>Done</p>

Output:
<>
  <div className="card" style={{ marginTop: "8px", backgroundColor: "#fff" }}>
    <label htmlFor="email">Email</label>{" "}
    <input type="email" id="email" defaultValue="a@b.co" maxLength="80" required /> {/* note */}{" "}
    <button className="btn" tabIndex="0" onClick={() => { /* send() */ }} disabled>Send</button>
  </div>
  <p>Done</p>
</>

// Inline event handlers (onclick etc.) were left as comments: convert them to functions by hand.

This is a syntax transformation rather than a formula. The rules come from React's own documentation: when JSX is compiled, attributes become keys of a JavaScript object, so their names cannot contain dashes or be reserved words such as class; React uses the DOM property names (className, htmlFor) instead. The output is indented with two spaces and long lines are wrapped at roughly 100 characters. Numeric attribute values stay strings (maxLength="80"); React accepts that, and you may write {80} if you prefer.

What the inputs mean

HTML
The HTML fragment to convert. It may be one element or several in a row; SVG inside it is converted as well. Up to 1,000,000 characters and 256 levels of nesting are processed.

Worked example: Converting a form card to JSX

  1. The input has two root elements (<div> and <p>). A JSX expression needs a single root, so the output starts with <> and ends with </>.
  2. class="card" becomes className="card", and the style string becomes the object style={{ marginTop: "8px", backgroundColor: "#fff" }}: margin-top and background-color are camel-cased and the values remain strings.
  3. The <label for="email"> element gets htmlFor="email".
  4. <input> is self-closed (/>). value="a@b.co" becomes defaultValue="a@b.co", maxlength becomes maxLength="80", and required stays bare.
  5. The comment <!-- note --> becomes {/* note */}. The {" "} expressions at the line ends keep the space that HTML had between label, input and button; JSX ignores whitespace at the end of a line.
  6. onclick="send()" is not turned into executable code: the tool writes onClick={() => { /* send() */ }} and adds this warning as the last line of the output: // Inline event handlers (onclick etc.) were left as comments: convert them to functions by hand.

Result: The output is eight lines of JSX wrapped in <>…</>, followed by a warning line. It compiles as it stands, but the button does nothing until you write the onClick function yourself.

⚛️ HTML to JSX Converter (React)

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

The output is syntactically valid JSX, not a finished component. Three jobs remain. First, events: in HTML onclick holds a string of code, whereas React's onClick takes a function, so the tool leaves the old code in a comment to show you what needs rewriting. Pass the function as onClick={handleSend}; if you write onClick={handleSend()}, it runs during rendering without waiting for a click. To stop a default browser action, call e.preventDefault(). Second, form fields: defaultValue supplies only the initial value (an uncontrolled field); if state should drive the value, use value together with onChange. Third, structure: the tool does not generate a component function, import lines or TypeScript types; you place the JSX inside the return statement of your own component. This is general information.

Common mistakes

  • Deleting the {" "} expressions because they look redundant. Without them the space between inline elements disappears and a label ends up glued to its field.
  • Leaving the empty handler in place. onClick={() => { /* send() */ }} raises no error and does nothing; rewrite the commented code as a function inside the component.
  • Changing defaultValue to value without adding onChange. React then warns that the field will be read-only, and the user cannot type in it.
  • Expecting <option selected> to be converted. The tool leaves the attribute alone; according to the React documentation, selected on an option is not supported and <select defaultValue="…"> should be used instead.
  • Pasting a Vue or Alpine template. Attributes such as @click and v-on:click have no JSX form; the tool drops them and adds the line "Elements or attributes that cannot be written in JSX were skipped: @click, v-on:click".
  • Pasting a complete page and dropping the result into a component. <html>, <head> and <body> are converted to JSX too; take only the part inside body.
  • Assuming style values become numbers. The tool keeps "8px" as a string. If you write a number by hand (marginTop: 8), React appends px to it unless the property is unitless.

Frequently asked questions

Why does JSX use className instead of class?

The React documentation explains that class is a reserved word in JavaScript, so React uses className, named after the corresponding DOM property. For the same reason the for attribute of a label becomes htmlFor.

Why do I have to close <img> and <br>?

In HTML, img, br, input and hr are void elements and take no end tag. JSX requires every tag to be closed explicitly: <img /> instead of <img>, and <li>apple</li> instead of <li>apple. The tool does this for you.

Are data-* and aria-* attributes camel-cased too?

No. The React documentation notes that, for historical reasons, these two groups are written with dashes as in HTML; data-id and aria-label appear unchanged in the output.

Does the tool produce a ready-made React component?

No, it produces JSX only. You put the result inside the return statement of a function such as function Card() { return ( … ); }. The same JSX is valid in .tsx files.

Is SVG inside the HTML converted as well?

Yes. Hyphenated attributes on SVG elements are camel-cased: stroke-width="2" becomes strokeWidth="2". For turning a standalone SVG file into a component, the SVG to JSX tool is the better fit, because it also removes editor data.

Is my HTML uploaded to a server?

No, the conversion runs in your browser.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice