Styling with the Style Attribute
Learn how to apply inline styling to React components.
We'll cover the following
The simplest way to style components in React is using the style
attribute on regular HTML elements. It differs from regular HTML though. React components expect an object in the form of:
property: value
Declaring style attributes
The property itself needs to be declared in JavaScript (not its regular
CSS counterpart) form, meaning:
zIndex
instead ofz-index
backgroundColor
instead ofbackground-color
marginTop
instead ofmargin-top
.
Code example with px
If the values accept declarations in pixels, it is optional to define px
as the corresponding unit explicitly:
Get hands-on with 1400+ tech skills courses.