Conteúdo do Curso
React Mastery
React Mastery
Inline 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 propertyfont-weight
, we should usefontWeight
; - 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 thefontSize
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.
Obrigado pelo seu feedback!