In JSX, attributes look similar to HTML but behave like JavaScript. JSX attributes:
tabIndex).class → className and for → htmlFor.{ }.✨ JSX = JavaScript + XML-like syntax
className is used instead of class to avoid conflicts with the JavaScript keyword.{ } to embed variables and expressions.onclick become onClick in JSX.JSX attributes work similarly to HTML, but with a few key differences. JSX attributes are written using camelCase naming conventions, and some attribute names differ from their HTML counterparts to avoid conflicts with JavaScript keywords.
For example, instead of using class or for like in HTML, JSX uses className and htmlFor.
This is a simple JSX element that uses attributes like src, alt, and width.
// JSX example with basic image attributes
const element = <img src="logo.png" alt="App Logo" width="200" />;
This syntax looks like HTML but is actually JavaScript under the hood. JSX converts this into:
// How React internally represents the same element
const element = React.createElement("img", {
src: "logo.png",
alt: "App Logo",
width: "200"
});
React then uses this object to efficiently render and update the DOM.
In HTML, we use the class attribute to apply CSS styles. However, since class is a reserved word in JavaScript, JSX uses className instead.
// ✅ Correct JSX using className
<h2 className="title">Welcome to React!</h2>
// ❌ Incorrect: HTML class will cause an error in JSX
<h2 class="title">This will cause an error</h2>
The className attribute links your JSX elements to CSS classes defined in your stylesheet.
You can embed JavaScript expressions inside attributes using curly braces { }. This allows you to assign dynamic values such as variables, functions, or expressions.
// Dynamic attributes using variables
const imageUrl = "https://reactjs.org/logo-og.png";
const altText = "React Logo";
const element = <img src={imageUrl} alt={altText} width={150} />;
React evaluates the expression inside { } and inserts the computed value into the attribute.
// Conditional className with a ternary operator
const isActive = true;
const buttonClass = isActive ? "btn btn-primary" : "btn";
const element = <button className={buttonClass}>Click me</button>;
In JSX, the style attribute is written as a JavaScript object, not a string. Properties use camelCase instead of hyphenated names.
// Inline styles are JavaScript objects in JSX
const titleStyle = { color: "blue", fontSize: "24px", textAlign: "center" };
const element = <h1 style={titleStyle}>Styled JSX Heading</h1>;
Remember that inline styles are defined within double curly braces — one for JavaScript, one for the object: style={{ color: "red" }}.
JSX follows the same naming rules as JavaScript properties — camelCase is always used. Some common conversions are:
class → classNamefor → htmlFortabindex → tabIndexonclick → onClickmaxlength → maxLengthConsider the JSX:
const heading = <h2 className="title">Welcome to React!</h2>;
React will render this to the DOM as:
<h2 class="title">Welcome to React!</h2>
Even though you write className in JSX, the actual HTML uses class. JSX is just a convenient syntax that compiles down to plain JavaScript and standard HTML attributes in the browser.
{ }.{ } can include variables, functions, or expressions.onClick is different from onclick.StyledCard that displays your name and role.className to apply external CSS for text styling.title attribute that changes based on a variable.Goal: Understand how JSX handles HTML-like attributes, use className for styling, and apply both static and dynamic attributes in React components.
// Example structure for a StyledCard component
function StyledCard() {
const role = "Frontend Developer";
const cardStyle = {
padding: "16px",
borderRadius: "8px",
border: "1px solid #e5e7eb",
backgroundColor: "#f9fafb"
};
return (
<div
className="card"
style={cardStyle}
title={`Role: ${role}`}
>
<h2 className="card-title">Your Name</h2>
<p className="card-role">{role}</p>
</div>
);
}