React JSX HTML Attributes and Properties
In React, we write HTML-like elements using JSX (JavaScript XML). Most HTML attributes work in React, but certain attribute names and their behavior are different.
For example:
|
HTML |
React JSX |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Let's understand the important differences, their purposes, and how to use them.
1. HTML attributes that have different names in React
React generally follows JavaScript DOM property naming conventions. Many HTML attributes use camelCase in JSX.
Here is a practical reference of commonly encountered conversions.
|
HTML attribute |
React JSX property |
Purpose |
|---|---|---|
|
|
|
Applies CSS classes |
|
|
|
Associates a label with an input |
|
|
|
Controls keyboard tab order |
|
|
|
Makes a field non-editable |
|
|
|
Sets maximum text length |
|
|
|
Sets minimum text length |
|
|
|
Automatically focuses an element |
|
|
|
Controls browser autocomplete |
|
|
|
Controls automatic capitalization |
|
|
|
Enables or disables spell checking |
|
|
|
Allows content editing |
|
|
|
Specifies table cell column span |
|
|
|
Specifies table cell row span |
|
|
|
Configures cross-origin resource requests |
|
|
|
Specifies responsive image sources |
|
|
|
Specifies a date/time value |
|
|
|
Associates an image with an image map |
|
|
|
Specifies accepted character encodings |
|
|
|
Provides HTTP-equivalent metadata |
|
|
|
Disables browser form validation |
|
|
|
Disables validation for a particular submit button |
|
|
|
Overrides the form submission destination |
|
|
|
Specifies form submission encoding |
|
|
|
Overrides the form submission method |
|
|
|
Specifies where the response is displayed |
|
|
|
Allows inline video playback |
Example:
HTML:
<div class="container">
<label for="username">Username</label>
<input type="text" id="username" readonly>
</div>
React JSX:
<div className="container">
<label htmlFor="username">Username</label>
<input type="text" id="username" readOnly />
</div>
Important: These are naming changes, not completely different functionalities. For example, className does the same job as HTML class.
Source: React – Common Components.
2. HTML attributes that remain the same in React
Not every HTML attribute needs a naming change.
The following attributes generally remain unchanged.
|
Property |
Purpose |
Example |
|---|---|---|
|
|
Identifies an element |
|
|
|
Specifies the form field name |
|
|
|
Specifies input type |
|
|
|
Specifies or controls a value |
|
|
|
Displays input guidance |
|
|
|
Makes a field mandatory |
|
|
|
Disables an element |
|
|
|
Controls checkbox/radio selection |
|
|
|
Specifies a resource location |
|
|
|
Provides alternative image text |
|
|
|
Specifies a hyperlink destination |
|
|
|
Specifies where a link opens |
|
|
|
Specifies the relationship to a linked resource |
|
|
|
Provides additional information |
|
|
|
Specifies width |
|
|
|
Specifies height |
|
|
|
Specifies the minimum permitted value |
|
|
|
Specifies the maximum permitted value |
|
|
|
Specifies the permitted value interval |
|
|
|
Specifies a validation pattern |
|
These properties can be used directly in JSX.
function Profile() {
return (
<div>
<img
src="/profile.png"
alt="User Profile"
width="150"
height="150"
/>
<input
type="text"
name="username"
placeholder="Enter username"
required
/>
<a href="/about" target="_blank">
About Us
</a>
</div>
);
}
3. Important difference: value vs defaultValue
This is one of the most important concepts in React forms.
Both properties can specify an input's value, but they work differently.
A. value – Controlled component
When you use value, React controls the input value.
Normally, you connect it with React state using useState.
import { useState } from "react";
function UserForm() {
const [username, setUsername] = useState("Syed");
return (
<div>
<input
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
<p>Username: {username}</p>
</div>
);
}
export default UserForm;
How does it work?
-
Initially,
usernamecontains"Syed". -
React displays
"Syed"in the input. -
The user types another character.
-
onChangeexecutes. -
setUsername()updates the state. -
React renders the updated input value.
Here, React controls the input's current value.
B. defaultValue – Uncontrolled component
When you use defaultValue, React specifies only the initial value.
After that, the browser manages the input value.
function UserForm() {
return (
<div>
<input
type="text"
defaultValue="Syed"
/>
</div>
);
}
export default UserForm;
How does it work?
-
Initially, React displays
"Syed". -
The user changes the input to
"Syed Ahmed". -
The browser manages the updated value.
-
React does not need to maintain that value in component state.
Comparison
|
Feature |
|
|
|---|---|---|
|
Type |
Controlled |
Uncontrolled |
|
Initial value |
Yes |
Yes |
|
Controls current value |
Yes |
No |
|
Requires |
Yes |
No |
|
Uses React state |
Usually |
Not necessarily |
|
Suitable for dynamic form updates |
Yes |
Usually not |
defaultValue is not a replacement for value. Use value when React needs to control the current input value. Use defaultValue when you only need to provide an initial value.
Source: React – Input.
4. checked vs defaultChecked
These properties are specifically important for checkboxes and radio buttons.
-
checked: React controls whether the checkbox or radio button is selected. -
defaultChecked: Specifies its initial selection state.
Using checked:
import { useState } from "react";
function Agree() {
const [isChecked, setIsChecked] = useState(false);
return (
<input
type="checkbox"
checked={isChecked}
onChange={(e) => setIsChecked(e.target.checked)}
/>
);
}
Using defaultChecked:
function Agree() {
return (
<input
type="checkbox"
defaultChecked={true}
/>
);
}
Comparison
|
Property |
Purpose |
|---|---|
|
|
Controls the current selection |
|
|
Sets the initial selection |
Important: For checkboxes and radio buttons, use checked or defaultChecked to control their selection. value represents the data submitted when that option is selected; it does not control whether it is selected.
5. Important properties for HTML form elements
React provides special behavior for form elements.
A. <input> properties
These are the important properties available for input fields.
|
Property |
Description |
|---|---|
|
|
Specifies text, password, email, number, checkbox, radio, file, etc. |
|
|
Controls the current input value |
|
|
Sets the initial input value |
|
|
Controls checkbox/radio selection |
|
|
Sets initial checkbox/radio selection |
|
|
Defines the form field name |
|
|
Displays placeholder text |
|
|
Makes the input mandatory |
|
|
Disables the input |
|
|
Prevents editing |
|
|
Automatically focuses the input |
|
|
Controls browser autocomplete |
|
|
Specifies maximum character count |
|
|
Specifies minimum character count |
|
|
Specifies maximum numeric/date value |
|
|
Specifies minimum numeric/date value |
|
|
Specifies allowed increments |
|
|
Specifies a validation pattern |
|
|
Allows multiple selections where supported |
|
|
Specifies accepted file types |
|
|
Requests camera or microphone capture |
|
|
Associates the input with a datalist |
|
|
Associates the input with a particular form |
|
|
Overrides the parent form action |
|
|
Overrides the form encoding |
|
|
Overrides the form HTTP method |
|
|
Overrides form validation |
|
|
Overrides the form target |
|
|
Specifies a field for text direction |
|
|
Alternative text for image-type inputs |
|
|
Image source for image-type inputs |
|
|
Image input height |
|
|
Image input width |
|
|
Preferred visible width of the input |
Example:
<input
type="email"
name="email"
placeholder="Enter email"
maxLength={100}
required
autoComplete="email"
/>
Note that numeric JSX values can be passed as JavaScript expressions using curly braces, as in maxLength={100}.
Source: React – Input Properties.
B. <textarea> properties
A textarea is used for multiline text input.
|
Property |
Description |
|---|---|
|
|
Controls the current text |
|
|
Sets the initial text |
|
|
Specifies the number of visible rows |
|
|
Specifies the approximate width in characters |
|
|
Specifies the form field name |
|
|
Displays placeholder text |
|
|
Sets maximum character count |
|
|
Sets minimum character count |
|
|
Disables the textarea |
|
|
Prevents editing |
|
|
Makes it mandatory |
|
|
Automatically focuses the textarea |
|
|
Controls autocomplete |
|
|
Associates it with a form |
|
|
Controls text wrapping during form submission |
Example:
<textarea
name="description"
rows={5}
cols={40}
placeholder="Enter description"
maxLength={500}
defaultValue="Welcome to React"
/>
Important difference from HTML: In React, you should not put the initial text between <textarea> and </textarea>. Use defaultValue instead.
C. <select> properties
Used to create dropdown lists.
|
Property |
Description |
|---|---|
|
|
Controls currently selected option |
|
|
Specifies the initially selected option |
|
|
Specifies the form field name |
|
|
Allows multiple selections |
|
|
Disables the dropdown |
|
|
Makes the dropdown mandatory |
|
|
Automatically focuses the dropdown |
|
|
Controls autocomplete |
|
|
Associates it with a particular form |
|
|
Specifies the number of visible options for a multiple select |
Example:
function Country() {
return (
<select defaultValue="India">
<option value="India">India</option>
<option value="USA">USA</option>
<option value="UK">UK</option>
</select>
);
}
Important: In regular HTML, developers sometimes use selected on an <option>. In React, you should use value or defaultValue on the parent <select> instead.
Source: React – Select.
D. <form> properties
|
Property |
Description |
|---|---|
|
|
Specifies the form submission destination or a React Action function |
|
|
Specifies GET or POST |
|
|
Executes when the form is submitted |
|
|
Executes when the form is reset |
|
|
Disables browser form validation |
|
|
Specifies where the response is displayed |
|
|
Specifies form data encoding |
|
|
Specifies accepted character encodings |
Example:
function Register() {
const handleSubmit = (e) => {
e.preventDefault();
console.log("Form submitted");
};
return (
<form onSubmit={handleSubmit}>
<input type="text" name="username" />
<button type="submit">
Register
</button>
</form>
);
}
React also supports function-based form action properties, introduced as part of its React 19 form APIs.
6. Common React properties supported by HTML elements
These properties can be used across many built-in HTML elements.
|
Property |
Description |
Example |
|---|---|---|
|
|
Applies CSS classes |
|
|
|
Specifies a unique element ID |
|
|
|
Applies inline CSS |
|
|
|
Specifies tooltip text |
|
|
|
Represents nested content |
|
|
|
Provides access to a DOM element |
|
|
|
Defines an accessibility role |
|
|
|
Controls keyboard navigation |
|
|
|
Hides an element |
|
|
|
Specifies text direction |
|
|
|
Specifies content language |
|
|
|
Defines a keyboard shortcut |
|
|
|
Makes content editable |
|
|
|
Enables dragging |
|
|
|
Enables/disables spell checking |
|
|
|
Suggests an input keyboard type |
|
|
|
Suggests the virtual keyboard's Enter key action |
|
|
|
Controls capitalization |
|
|
|
Adds custom data attributes |
|
|
|
Adds accessibility attributes |
|
|
|
Specifies structured-data information |
|
|
|
Controls translation of content |
|
There are also several React-specific props:
|
React property |
Description |
|---|---|
|
|
Identifies elements when rendering lists |
|
|
References a DOM element or component |
|
|
Inserts raw HTML into an element |
|
|
Suppresses a warning related to |
|
|
Suppresses certain server/client hydration mismatch warnings |
Important details:
-
keyis used by React to identify list elements. It is not an ordinary HTML attribute and is not passed as a normal prop to your component. -
childrenrepresents content placed inside a component. -
dangerouslySetInnerHTMLshould only be used with trusted or properly sanitized HTML because it can introduce XSS vulnerabilities. -
refallows you to access the underlying DOM element.
Example of key:
const users = [
{ id: 1, name: "Syed" },
{ id: 2, name: "Ahmed" }
];
function UserList() {
return (
<ul>
{users.map(user => (
<li key={user.id}>
{user.name}
</li>
))}
</ul>
);
}
Example of ref:
import { useRef } from "react";
function Search() {
const inputRef = useRef(null);
const handleClick = () => {
inputRef.current.focus();
};
return (
<>
<input ref={inputRef} />
<button onClick={handleClick}>
Focus Input
</button>
</>
);
}
Source: React Common Element Props.
7. React event properties
This is another major difference between HTML and React.
In HTML, event names are generally lowercase:
<button onclick="showMessage()">
Click Me
</button>
In React, event names use camelCase, and you pass a JavaScript function.
function App() {
const showMessage = () => {
alert("Hello React");
};
return (
<button onClick={showMessage}>
Click Me
</button>
);
}
A. Common event properties
|
React event |
When does it execute? |
|---|---|
|
|
When an element is clicked |
|
|
When an element is double-clicked |
|
|
When the mouse button is pressed |
|
|
When the mouse button is released |
|
|
When the mouse moves |
|
|
When the mouse enters an element |
|
|
When the mouse leaves an element |
|
|
When the mouse moves over an element |
|
|
When the mouse moves out of an element |
|
|
When the context menu is requested |
|
|
When an element receives focus |
|
|
When an element loses focus |
|
|
When a keyboard key is pressed |
|
|
When a keyboard key is released |
|
|
When an input value changes |
|
|
When input content changes |
|
|
When a form is submitted |
|
|
When a form is reset |
|
|
When form validation fails |
|
|
When text selection changes |
|
|
When content is copied |
|
|
When content is cut |
|
|
When content is pasted |
|
|
While an element is being dragged |
|
|
When dragging starts |
|
|
When dragging ends |
|
|
When dragged content enters a drop target |
|
|
When dragged content leaves a drop target |
|
|
When dragged content moves over a drop target |
|
|
When dragged content is dropped |
|
|
When an element is scrolled |
|
|
When the mouse wheel is used |
|
|
When touch interaction starts |
|
|
When a finger moves |
|
|
When touch interaction ends |
|
|
When touch interaction is cancelled |
|
|
When a pointer becomes active |
|
|
When a pointer is released |
|
|
When a pointer moves |
|
|
When a pointer enters an element |
|
|
When a pointer leaves an element |
|
|
When a pointer interaction is cancelled |
|
|
When a CSS animation starts |
|
|
When a CSS animation finishes |
|
|
When an animation iteration finishes |
|
|
When a CSS transition finishes |
|
|
When a resource loads |
|
|
When a resource fails to load |
B. What is Capture in React events?
React also supports capture-phase event handlers for many events.
For example:
<button
onClick={handleClick}
onClickCapture={handleClickCapture}
>
Click Me
</button>
There are two event propagation phases:
-
Capture phase – The event travels from the parent towards the target.
-
Bubble phase – The event travels from the target back towards the parent.
onClick normally handles the bubbling phase, whereas onClickCapture handles the capture phase.
Some events, such as onMouseEnter and onMouseLeave, do not have corresponding capture variants.
Source: React Event Handlers.
8. Important difference in the style property
In HTML, inline CSS is written as a string.
HTML:
<div style="background-color: red; font-size: 20px;">
Hello
</div>
In React, style receives a JavaScript object.
React:
<div
style={{
backgroundColor: "red",
fontSize: "20px"
}}
>
Hello
</div>
CSS property differences
CSS properties use kebab-case, while React style object properties use camelCase.
|
CSS property |
React style property |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using dynamic values
function App() {
const headingColor = "blue";
const fontSize = 25;
return (
<h1
style={{
color: headingColor,
fontSize: fontSize
}}
>
Welcome to React
</h1>
);
}
React automatically adds px to numeric values for CSS properties that require a unit.
For example:
<div style={{
width: 200,
height: 100,
opacity: 0.5
}}>
Hello
</div>
Here:
-
width: 200means200px. -
height: 100means100px. -
opacity: 0.5remains unitless.
For most static styles, using className and an external CSS file is preferable.
9. Boolean properties in React
In HTML, boolean attributes can often be written without assigning a value.
HTML:
<input type="text" disabled>
React:
<input type="text" disabled={true} />
You can also write:
<input type="text" disabled />
Both examples enable the disabled property.
To disable it conditionally:
<input
type="text"
disabled={false}
/>
Common boolean properties
|
Property |
Purpose |
|---|---|
|
|
Disables an element |
|
|
Makes an input mandatory |
|
|
Selects checkbox/radio |
|
|
Enables multiple selections |
|
|
Makes an input non-editable |
|
|
Automatically focuses an element |
|
|
Hides an element |
|
|
Disables form validation |
|
|
Starts media playback automatically |
|
|
Displays media controls |
|
|
Repeats media playback |
|
|
Mutes audio/video |
|
|
Opens a details or dialog element |
|
|
Avoid for controlling React dropdown selection; use parent |
Note: Not every attribute that accepts true/false follows exactly the same HTML boolean-attribute rules. For example, aria-* attributes generally use string values such as "true" and "false".
10. Additional important JSX rules
Apart from properties, remember these JSX rules.
|
Rule |
Example |
|---|---|
|
Use |
|
|
Use |
|
|
Use curly braces for JavaScript expressions |
|
|
Pass functions to event handlers |
|
|
Use self-closing tags |
|
|
Use camelCase for many DOM properties |
|
|
Use an object for inline styles |
|
|
Use |
|
|
Use |
|
11. Complete practical example
This example combines several of the properties discussed above.
import { useState } from "react";
function RegistrationForm() {
const [username, setUsername] = useState("");
const [email, setEmail] = useState("");
const [gender, setGender] = useState("Male");
const [isAgreed, setIsAgreed] = useState(false);
const handleSubmit = (e) => {
e.preventDefault();
console.log({
username,
email,
gender,
isAgreed
});
};
return (
<div className="container">
<h2 className="heading">
Registration Form
</h2>
<form onSubmit={handleSubmit}>
<label htmlFor="username">
Username
</label>
<input
id="username"
name="username"
type="text"
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter username"
maxLength={50}
required
/>
<label htmlFor="email">
Email
</label>
<input
id="email"
name="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Enter email"
autoComplete="email"
required
/>
<label htmlFor="gender">
Gender
</label>
<select
id="gender"
name="gender"
value={gender}
onChange={(e) => setGender(e.target.value)}
>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<label>
<input
type="checkbox"
checked={isAgreed}
onChange={(e) => setIsAgreed(e.target.checked)}
/>
I agree to the terms and conditions.
</label>
<button
type="submit"
disabled={!isAgreed}
>
Register
</button>
</form>
</div>
);
}
export default RegistrationForm;
This example demonstrates:
-
classNameandhtmlFor. -
valueandonChange. -
checkedandonChange. -
disabledwith a conditional expression. -
id,name,type,required, andplaceholder. -
onSubmitandpreventDefault().
12. Official React documentation – Complete reference
React supports the browser's built-in HTML and SVG elements. The common element reference covers shared props and events, while individual element references document their specific properties. Therefore, there is no single short list containing every property for every HTML element.
These official references are the best resources to keep bookmarked:
|
Documentation |
What you will learn |
|---|---|
|
Shared HTML properties, events, styles and refs |
|
|
All input properties and controlled inputs |
|
|
Dropdown properties |
|
|
Textarea properties |
|
|
Form submission and Actions |
|
|
All built-in React DOM elements |
My recommendation for learning React: Pay particular attention to className, htmlFor, onClick, onChange, value, defaultValue, checked, defaultChecked, style, key, and ref. These are among the properties you will encounter frequently when building React applications.