Lesson 3 of 15
Styling Basics in React
Lesson Updated: Apr 28, 2026
Requirement
Learn the difference between inline styles (using JavaScript objects) and standard CSS classes (using the className attribute) in React.
Assignments (Start First)
- Simple beginner UI task: Create a
DangerButtoncomponent that uses an inline style object to make its background red and text white. - Slightly improved version: Convert the
DangerButtonto use a standard CSS class calledbtn-dangerinstead of inline styles. - Practical real-world component: Build a
ProfileAvatarcomponent. Give it an inline style to dynamically set itsbackgroundImagebased on animageUrlprop. - "The Escalation": Create a
StatusBadgecomponent that accepts astatusprop ("online" or "offline"). Conditionally apply either a green or gray background color using inline styles based on the prop.
Lessons (Optional)
- className: Because
classis a reserved word in JavaScript, React usesclassNamefor applying CSS classes. - Inline Styles: Inline styles in React are written as JavaScript objects, where CSS properties are camelCased (e.g.,
backgroundColorinstead ofbackground-color).
The Summary and Quizzes
- Concept explanation: React gives you the flexibility to style elements via standard CSS classes or dynamic inline styles when values need to change based on JS variables.
- Why it works in React: React translates the
styleobject into standard CSS strings when it renders the Virtual DOM to the actual browser DOM. - Challenge question: Write the exact JSX syntax to apply an inline style that sets
font-sizeto "20px" on a<p>tag.