All articles

HTML vs JSX: the differences that matter

· 3 min read

JSX lets you write React components with markup that looks like HTML. But it's JavaScript underneath: <div className="card"> compiles to a function call such as jsx("div", { className: "card" }). Every difference from HTML follows from that, and knowing the list saves time when you paste a template into a component.

Attribute names

className and htmlFor

class and for are reserved words in JavaScript, so JSX uses the DOM property names:

<label for="email" class="label">Email</label>
<label htmlFor="email" className="label">Email</label>

camelCase attributes

Multi-word attributes use camelCase, matching DOM properties: tabIndex, readOnly, maxLength, colSpan, autoComplete, crossOrigin, srcSet, contentEditable, spellCheck.

The exceptions are data-* and aria-*, which stay exactly as in HTML.

Event handlers

HTML event attributes are strings of code. In JSX they're camelCase props that take a function:

<button onclick="save()">Save</button>
<button onClick={save}>Save</button>
<button onClick={() => save(item.id)}>Save</button>

Pass the function, don't call it: onClick={save()} runs save during every render.

Inline styles are objects

<div style="background-color: #f5f5f5; margin-top: 8px; --gap: 4px"></div>
<div style={{ backgroundColor: "#f5f5f5", marginTop: 8, "--gap": "4px" }}></div>
  • Property names are camelCase; custom properties keep their -- name as a string key.
  • Numbers get px added for most length properties (marginTop: 8). Unitless properties like lineHeight, opacity and zIndex stay as numbers.
  • Vendor prefixes start with a capital letter: WebkitLineClamp. ms prefixes are the exception (msTransform).

The double braces are just a JavaScript object inside a JSX expression.

Every tag must be closed

HTML allows void elements without a closing slash. JSX requires one:

<img src="logo.png" alt="Logo">
<br>
<input type="text" name="q">
<img src="logo.png" alt="Logo" />
<br />
<input type="text" name="q" />

Any element without children can self-close in JSX, including <div />.

Comments

HTML comments are a syntax error inside JSX. Use a JavaScript comment inside braces:

{/* This is a comment */}

Boolean attributes

In HTML, the presence of an attribute makes it true. In JSX, pass a boolean:

<input disabled />               // true
<input disabled={isSaving} />    // depends on the value
<input disabled={false} />       // not disabled (in HTML, disabled="false" is still disabled)

Form elements

React treats form values differently:

  • value and checked make an input controlled: React owns the value, so you need an onChange handler. For an initial value only, use defaultValue or defaultChecked.
  • <textarea> takes its content from value or defaultValue, not from children.
  • <select> uses value on the <select> itself instead of selected on an <option>.
<select value={country} onChange={(e) => setCountry(e.target.value)}>
  <option value="in">India</option>
  <option value="us">United States</option>
</select>

Text and special characters

Text in JSX is escaped automatically, which protects against injection; see escaping HTML. HTML entities like &nbsp; and &copy; work in JSX text, but { and } must be written as {"{"} and {"}"} because braces start expressions.

Whitespace differs too: JSX removes line breaks and indentation between lines. Add an explicit {" "} when you need a space between an element and text on the next line.

Inline SVG

SVG pasted into JSX needs the same treatment, plus a few SVG-specific attributes:

SVG JSX
stroke-width strokeWidth
fill-rule, clip-rule fillRule, clipRule
stroke-linecap strokeLinecap
xlink:href xlinkHref (or plain href in modern SVG)
class className
style="..." style={{ ... }}

viewBox keeps its camelCase from SVG. Remove xmlns:xlink declarations if you no longer use xlink.

Things that don't carry over

  • Inline <script> tags inside components don't run when React inserts them on the client. Load scripts with your framework's script component, an effect, or (in React 19) an async script with a src.
  • Inline event code like onclick="..." must become a function.
  • <!DOCTYPE>, <html> and <head> belong to your framework's document or layout, not to components.

Quick reference

HTML JSX
class="x" className="x"
for="id" htmlFor="id"
tabindex="0" tabIndex={0}
onclick="f()" onClick={f}
style="color: red" style={{ color: "red" }}
<br> <br />
<!-- note --> {/* note */}
disabled disabled or disabled={true}
<option selected> <select value=...>
<textarea>text</textarea> <textarea defaultValue="text" />

Frequently asked questions

Can I use class instead of className?

React DOM warns about class and expects className, because class is a reserved word in JavaScript. Preact accepts both, and some other JSX libraries such as Solid use class.

Why are data- and aria- attributes not camelCased?

React passes data-* and aria-* attributes through unchanged, so keep them hyphenated: data-id, aria-label.

How do I render raw HTML in JSX?

Use dangerouslySetInnerHTML={{ __html: html }}, and only with trusted or sanitized HTML, because it bypasses React's escaping.

Does JSX allow multiple root elements?

A component returns one element. Wrap siblings in a fragment, <>...</>, to avoid an extra DOM node.

Further reading