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
React Mastery
course content

Contenido del Curso

React Mastery

React Mastery

1. Introduction to React Fundamentals
2. Styling in React Apps
3. React Hooks and Context
4. React in the Real World

bookInline 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?

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

Selecciona la respuesta correcta

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

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt