Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Inline Styles | Styling in React Apps
course content

Course Content

Mastering React

Inline StylesInline Styles

One straightforward but somewhat limited approach to adding styles is using inline styles, similar to how we apply styles to HTML elements using the style attribute. In React, the only difference is that the value for this attribute is an object, not a string. Let's dive into the details.

The App component in the example above includes an h1 element with inline styles defined using the style attribute. Here are some key points to remember about inline styles:

  • Property names with two or more words should follow the camel case (camelCase) notation. For instance, instead of using the CSS property font-weight, we should use fontWeight.
  • When assigning values to CSS properties, we generally use strings, except for properties that expect plain numbers. This is why the fontWeight property uses a number (700) as its value, while the fontSize property uses a string value (32px).

Inline Styles in the Form of a Separate Object

To keep our JSX code clean and focused, we can define the inline styles as a separate JavaScript object and then assign that object to the style attribute. This approach improves the maintainability and readability of our components.

In this example, we introduce the appStyles object, representing the same styles used before but now as a separate object. This separation of concerns enhances the clarity of our component code.

1. What is the key difference between applying styles in HTML and using inline styles in React?
2. Which notation should you follow when defining property names with two or more words in inline styles?

What is the key difference between applying styles in HTML and using inline styles in React?

Select the correct answer

Which notation should you follow when defining property names with two or more words in inline styles?

Select the correct answer

Everything was clear?

Section 2. Chapter 2
course content

Course Content

Mastering React

Inline StylesInline Styles

One straightforward but somewhat limited approach to adding styles is using inline styles, similar to how we apply styles to HTML elements using the style attribute. In React, the only difference is that the value for this attribute is an object, not a string. Let's dive into the details.

The App component in the example above includes an h1 element with inline styles defined using the style attribute. Here are some key points to remember about inline styles:

  • Property names with two or more words should follow the camel case (camelCase) notation. For instance, instead of using the CSS property font-weight, we should use fontWeight.
  • When assigning values to CSS properties, we generally use strings, except for properties that expect plain numbers. This is why the fontWeight property uses a number (700) as its value, while the fontSize property uses a string value (32px).

Inline Styles in the Form of a Separate Object

To keep our JSX code clean and focused, we can define the inline styles as a separate JavaScript object and then assign that object to the style attribute. This approach improves the maintainability and readability of our components.

In this example, we introduce the appStyles object, representing the same styles used before but now as a separate object. This separation of concerns enhances the clarity of our component code.

1. What is the key difference between applying styles in HTML and using inline styles in React?
2. Which notation should you follow when defining property names with two or more words in inline styles?

What is the key difference between applying styles in HTML and using inline styles in React?

Select the correct answer

Which notation should you follow when defining property names with two or more words in inline styles?

Select the correct answer

Everything was clear?

Section 2. Chapter 2
some-alt