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
Inline Styles and Style Objects0%
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
9.1Inline Styles and Style Objects9.2CSS Stylesheets and Global Class Names9.3CSS Modules for Scoped Component Styling9.4Utility-First Styling with Tailwind CSS in React
HomeTutorialsReact.js for BeginnersInline Styles and Style Objects
CSS Stylesheets and Global Class Names

Inline Styles and Style Objects

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

1. Inline Styles in React

In traditional HTML, inline styles are declared as semicolon-delimited text strings:

1<div style="background-color: navy; color: white; padding: 12px;">
2 Standard HTML
3</div>

In React JSX, inline styles are passed as JavaScript objects.

This gives styles the full power of JavaScript: dynamic variables, ternary operators, and computed calculations can be seamlessly blended into your component presentation.

1import React from 'react';
2 
3export default function ProgressBar({ percentage }) {
4 // Style object defined as a JavaScript variable
5 const containerStyle = {
6 height: '16px',
7 width: '100%',
8 backgroundColor: '#e2e8f0',
9 borderRadius: '8px',
10 overflow: 'hidden'
11 };
12 
13 const fillStyle = {
14 height: '100%',
15 width: `${percentage}%`,
16 backgroundColor: percentage >= 100 ? '#10b981' : '#3b82f6',
17 transition: 'width 0.3s ease-in-out'
18 };
19 
20 return (
21 <div style={containerStyle}>
22 <div style={fillStyle} />
23 </div>
24 );
25}

2. Converting CSS Properties to camelCase

Because property keys in JavaScript objects cannot contain hyphens without being wrapped in quotes, React converts CSS property names to camelCase:

Standard CSSReact JSX Style Key
background-colorbackgroundColor
font-sizefontSize
margin-topmarginTop
border-radiusborderRadius
z-indexzIndex
box-shadowboxShadow

3. Automatic Pixel Units for Numbers

When passing numeric values to dimension properties (such as width, height, margin, padding, fontSize), React automatically appends the 'px' suffix for you:

1// React automatically appends 'px' to numbers for dimension properties:
2<div style={{ width: 300, height: 150, padding: 20 }}>
3 Box with automatic px units (width: 300px, height: 150px, padding: 20px)
4</div>
5 
6// For unitless properties like zIndex, opacity, or flex, React leaves them as unitless numbers:
7<div style={{ opacity: 0.8, zIndex: 10, flex: 1 }}>
8 Unitless properties remain unchanged
9</div>
10 
11// For other units (% , rem, em, vh), you must pass a string:
12<div style={{ width: '50%', fontSize: '1.25rem', height: '100vh' }}>
13 Explicit string units
14</div>

4. Pros and Cons of Inline Styles

Pros:

  • Scoped to the element: Zero risk of style name collisions with other components.
  • Dynamic Values: Ideal for styles driven by runtime data (e.g. progress bars, dynamic coordinates, drag-and-drop offsets).

Cons:

  • No Pseudo-classes or Pseudo-elements: Inline styles cannot define :hover, :focus, :active, ::before, or ::after.
  • No Media Queries: Responsive breakpoints (@media (max-width: 768px)) cannot be written in inline style objects.
  • Performance: Creating new style objects on every render creates minor garbage collection overhead compared to static CSS class names.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
How are CSS property names written inside React style objects?
Question 2
What unit does React automatically append when a number is passed to a dimension property like width: 250?
Question 3
Which CSS features CANNOT be implemented using standard React inline styles?
Question 4
Why are inline styles ideal for a dynamic progress bar or drag-and-drop position?
Question 5
How do you specify percentages or rem units in an inline style object?
Next Lesson

CSS Stylesheets and Global Class Names

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

Related Lessons

Previous LessonNext Lesson
Cleanup Functions in useEffectCSS Stylesheets and Global Class Names

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