JSX Attributes
Learn how to add attributes to JSX elements, use JavaScript expressions in attributes, apply CSS classes, and handle common JSX attribute rules.
1. What Are JSX Attributes?
JSX attributes provide additional information to elements. They are similar to HTML attributes, but some attribute names are different in JSX because JSX follows JavaScript naming conventions.
<img src="/images/react.png" alt="React Logo" />
Here, src specifies the image location and
alt provides alternative text.
2. JSX Attributes — Visual Diagram
A JSX element can contain a tag name, attributes, attribute values, and content.
button
Defines the element.
className
Defines an element property.
"primary-btn"
Specifies the CSS class.
{true}
Uses a JavaScript value.
function App() {
return (
<button className="primary-btn" disabled={true}>
Enroll Now
</button>
);
}
export default App;
3. String Attributes
Use quotation marks to provide a string value to a JSX attribute.
function App() {
return (
<div>
<h2 title="React Tutorial">
Learn React
</h2>
<a href="http://ciitstacklab.com/">
Official React Website
</a>
</div>
);
}
export default App;
Expected Browser Output
Learn React
Official React Website4. Using JavaScript Expressions in Attributes
Curly braces allow you to pass variables and JavaScript expressions as attribute values.
function App() {
const courseName = "React JS";
const imagePath = "/images/react-logo.png";
const isDisabled = true;
return (
<div>
<h2 title={courseName}>
{courseName}
</h2>
<img
src={imagePath}
alt="React Logo"
width={120}
/>
<button disabled={isDisabled}>
Enroll Now
</button>
</div>
);
}
export default App;
title="React JS" for a literal string and
title={courseName} for a JavaScript variable.
5. className Instead of class
In JSX, use className to assign a CSS class
to an element. This follows the JavaScript DOM property
naming convention.
<h2 class="heading">Hello</h2>
<h2 className="heading">Hello</h2>
function App() {
return (
<div className="course-card">
<h2 className="course-title">
React Full Stack
</h2>
<p className="course-description">
Learn React step by step.
</p>
</div>
);
}
export default App;
6. JSX Attribute Naming Rules
Many JSX DOM event and property names use camelCase. Some familiar HTML attributes also have JSX-specific names.
| HTML Attribute | JSX Attribute | Purpose |
|---|---|---|
class |
className |
Assigns a CSS class. |
for |
htmlFor |
Connects a label to a form control. |
onclick |
onClick |
Handles a click event. |
tabindex |
tabIndex |
Controls keyboard tab order. |
readonly |
readOnly |
Makes a field read-only. |
function App() {
return (
<div>
<label htmlFor="studentName">
Student Name
</label>
<input
id="studentName"
type="text"
readOnly={true}
value="Priya"
/>
</div>
);
}
export default App;
7. Boolean Attributes
Boolean attributes represent true or false states.
For example, disabled disables a button
when its value is true.
function App() {
const canEnroll = false;
return (
<div>
<button disabled={!canEnroll}>
Enroll Now
</button>
<button disabled={false}>
View Courses
</button>
</div>
);
}
export default App;
8. Inline Styles in JSX
JSX uses a JavaScript object for inline styles. CSS property names are generally written in camelCase, and string values are used for values such as colors.
function App() {
const headingStyle = {
color: "blue",
backgroundColor: "lightyellow",
padding: "12px",
borderRadius: "8px"
};
return (
<h2 style={headingStyle}>
Welcome to CIIT Training Institute
</h2>
);
}
export default App;
Expected Browser Output
Welcome to CIIT Training Institute
style={headingStyle}, not a CSS string.
9. Common Mistakes
Using class instead of className
Avoid in JSX:
<div class="box">Hello</div>
Use:
<div className="box">Hello</div>
Passing a variable without curly braces
Incorrect for a variable:
<img src="imagePath" alt="Course" />
Correct:
<img src={imagePath} alt="Course" />
Using a CSS string in the style attribute
Incorrect:
style="color: blue"
Correct:
style={{ color: "blue" }}
Using lowercase event names
Incorrect:
onclick={handleClick}
Correct:
onClick={handleClick}
10. Practice Questions
- Create a heading with a
titleattribute. - Display a course image using a variable in the
srcattribute. - Create a button with a dynamic
disabledattribute. - Build a student form using
htmlForandclassName. - Apply inline styles using a JavaScript object.
11. Summary
- JSX attributes configure elements.
- String values can be provided inside quotation marks.
- Curly braces allow JavaScript expressions as attribute values.
- Use
classNameinstead ofclass. - Use camelCase for many JSX event and DOM property names.
- Inline styles use JavaScript objects.
- Boolean attributes can control element states.