JSX Attributes and className
In this page:
<element className="class-name" attributeName={expression} />
<element style={{ camelCaseProperty: "value" }} />
Setting className Instead of class
Because class is a reserved word in JavaScript, JSX uses className to apply a CSS class to an element instead of HTML's class attribute.
Example: Setting className Instead of class
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function StyledBox() {
return (
<div className="box" style={{border: '2px solid #333', padding: '8px'}}>
Styled with className
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<StyledBox />);
</script>
</body>
</html>
Passing Dynamic Attribute Values
Any JSX attribute can take a dynamic value using curly braces, like src={imageUrl} or disabled={isLoading}, instead of a fixed string.
Example: Passing Dynamic Attribute Values
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function DynamicButton() {
const isLoading = true;
return <button disabled={isLoading}>{isLoading ? 'Loading...' : 'Submit'}</button>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<DynamicButton />);
</script>
</body>
</html>
The style Attribute Takes an Object
Unlike HTML, JSX's style attribute takes a JavaScript object with camelCased CSS properties, not a semicolon-separated string.
Example: The style Attribute Takes an Object
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function InlineStyled() {
return <p style={{ color: 'blue', fontWeight: 'bold' }}>Styled inline</p>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<InlineStyled />);
</script>
</body>
</html>
- Using
classinstead ofclassName, which silently does nothing in JSX. - Forgetting to wrap dynamic attribute values in curly braces instead of quotes.
- Using
forinstead ofhtmlForon label elements.
- JSX attributes use camelCase names, unlike lowercase HTML attributes.
classNamereplacesclass;htmlForreplacesfor.- Static attribute values use quotes; dynamic values use curly braces.
- Event handler attributes like
onClicktake a function reference, not a string.
No browser-specific restrictions -- attribute naming is a JSX convention, not a runtime feature.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: