MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
JSX Attributes, ClassName, and Self-Closing Tags0%
Study Materials
React.js for BeginnersProgress 0%
1.1Why React and Single-Page Architecture1.2Setting Up Modern React with Vite1.3React Project Directory Structure and Entry Points1.4Virtual DOM and React Render Cycle Explained
2.1Understanding JSX Syntax and HTML Differences2.2Embedding JavaScript Expressions in JSX2.3JSX Attributes, ClassName, and Self-Closing Tags2.4React Fragments and Returning Clean Markup
HomeTutorialsReact.js for BeginnersJSX Attributes, ClassName, and Self-Closing Tags
React Fragments and Returning Clean Markup

JSX Attributes, ClassName, and Self-Closing Tags

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. Attributes in JSX

Attributes in JSX allow you to configure elements and components with data, behavior, and styling. While many attributes resemble their HTML counterparts, JSX attributes follow standard JavaScript DOM properties and strict naming conventions.

Passing String Literals vs Expressions

  • For static strings, pass the value enclosed in quotes:
1 <input type="email" placeholder="student@mskinstitute.com" />
  • For numbers, booleans, arrays, functions, or variables, wrap them in curly braces:
1 <input
2 type="number"
3 min={1}
4 max={100}
5 disabled={false}
6 defaultValue={defaultCount}
7 />

2. The className Attribute

In standard HTML, styles are applied using the class attribute (<div class="card">). In JSX, you must write className:

1// Static classes
2<div className="card shadow-md p-4 rounded-xl">
3 <h2 className="text-xl font-bold">Course Overview</h2>
4</div>
5 
6// Dynamic classes with template literals
7function StatusBadge({ isEnrolled }) {
8 return (
9 <span className={`badge ${isEnrolled ? 'badge-success' : 'badge-warning'}`}>
10 {isEnrolled ? 'Active' : 'Pending'}
11 </span>
12 );
13}

3. Passing Inline Styles: The Double Curly Braces {{}}

A common stumbling block is inline styling in JSX. In HTML, styles are passed as semicolon-separated strings (style="color: red; font-size: 16px;"). In JSX, the style attribute expects a JavaScript object.

This creates the syntax pattern style={{ ... }}:

  • The outer braces { } indicate a dynamic JavaScript expression.
  • The inner braces { } represent a JavaScript object literal.
1export default function AlertBox() {
2 const alertStyles = {
3 backgroundColor: '#fee2e2', // camelCase instead of background-color
4 color: '#991b1b',
5 padding: '16px',
6 borderRadius: '8px',
7 fontSize: '14px' // camelCase instead of font-size
8 };
9 
10 return (
11 <div style={alertStyles}>
12 <strong>Attention:</strong> Your lab environment resets in 10 minutes.
13 </div>
14 );
15}
16 
17// Or passed directly inline:
18<div style={{ color: 'blue', marginTop: '20px' }}>
19 Direct Inline Styling
20</div>

Note: CSS property names that feature hyphens (like font-size, background-color, margin-top) are converted to camelCase in JSX (fontSize, backgroundColor, marginTop).

4. Self-Closing Tags

In standard HTML5, several elements are categorized as "void elements" (elements that cannot contain child nodes or closing tags), such as:

  • <img>
  • <input>
  • <br>
  • <hr>
  • <meta>

In HTML, writing <img> without a slash is valid. In JSX, this is a syntax error. Any element that has no children must be explicitly self-closed using a trailing forward slash:

1// ❌ Syntax Error in JSX: Unterminated JSX contents
2<input type="text">
3<img src="logo.png">
4<hr>
5 
6// ✅ Valid JSX: Properly self-closing
7<input type="text" />
8<img src="logo.png" alt="Company Logo" />
9<hr />
10 
11// ✅ Custom React components with no children should also self-close
12<Sidebar navigation={navLinks} />

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why does inline styling in JSX often look like style={{ color: 'red' }}?
Question 2
How must the CSS property background-color be written when passing inline styles to a React element?
Question 3
Which of the following elements is syntactically INVALID in JSX?
Question 4
How do you pass a boolean true value to an attribute named isActive in JSX?
Question 5
What is the correct way to specify multiple dynamic classes conditionally using standard JavaScript template literals?
Next Lesson

React Fragments and Returning Clean Markup

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Embedding JavaScript Expressions in JSXReact Fragments and Returning Clean Markup

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master React.js for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext