Home / Learn React / HTML Elements and Props in JSX

HTML Elements and Props in JSX

In JSX you write normal HTML elements, but attributes follow JavaScript names: className, htmlFor, camelCase events and a style object.

In shortUse className, htmlFor, onClick, style={{}}. data- and aria- attributes stay with dashes. dangerouslySetInnerHTML is a last resort.

Theory in plain words

HTML vs JSX namesHTMLclass="box"for="email"onclick="go()"style="color:red"tabindex="0"JSXclassName="box"htmlFor="email"onClick={go}style={{ color: "red" }}tabIndex={0}
HTML vs JSX names

What it is

React DOM elements are the lowercase tags such as div, input, img. React supports the standard HTML and SVG tags, with attribute names changed where JavaScript has a conflict.

Why we use it

JSX is JavaScript, so words like class and for are reserved. Using the right names avoids silent bugs, and the object form of style makes dynamic styling simple.

How it works

React turns each element into a DOM node and sets properties from your props. Event props such as onClick use React's synthetic event system. Unknown lowercase props pass through to the DOM element.

Where it is used

  • Every React screen: layouts, forms, media, tables, SVG icons
Common elements in one componentTry it Yourself

The button is disabled until both the checkbox and name are filled. value on select and textarea works like on input in React.

ElementTypical propsNote
ahref, target, relAdd rel="noopener noreferrer" with target="_blank". Use Link from a router for internal pages.
imgsrc, alt, width, height, loadingAlways set alt and size to avoid layout jumps.
buttontype, onClick, disabledSet type="button" when not submitting.
inputvalue, onChange, checked, typeUse defaultValue for uncontrolled inputs.
textarea, selectvalue, onChangeValue is a prop, not text between tags.
labelhtmlForLinks the label to an input id.
ul, likeyKeys go on the outer element in map.
svgviewBox, strokeWidth, fillRuleSVG attributes use camelCase: stroke-width becomes strokeWidth.
dialog, detailsopen, onToggleNative, accessible building blocks.

Boolean attributes and dangerouslySetInnerHTML

Booleans, data- and aria- attributesTry it Yourself
Careful:dangerouslySetInnerHTML inserts raw HTML and can create XSS holes. Use it only with HTML you have cleaned with a library such as DOMPurify.

Quick quiz

1. JSX name for the HTML class attribute?

2. How do you write inline style in JSX?

3. Which attribute links a label to an input?

Common questions

JSX name for the HTML class attribute?

className. class is reserved in JavaScript.

How do you write inline style in JSX?

style={{ color: "red" }}. An object with camelCase keys.

Which attribute links a label to an input?

htmlFor. htmlFor.