Working with CSS styles in the style attribute in React
In previous lessons, we passed a variable containing an object with CSS styles to the style
attribute. You can avoid using an intermediate variable and instead describe the object directly in the attribute - in this case, we need two pairs of curly brackets - the first from the JSX insert, and the second from the object.
So, let's take our component without CSS styles:
function App() {
return (
<div>
<p>text</p>
<p>text</p>
<p>text</p>
</div>
);
}
For example, let's directly write CSS properties for the first paragraph of our React component App
:
<p style = {{
color: 'orangered',
fontWeight: 'bold' }}>
text
</p>
We will add styles for the remaining tags in a similar manner.
As a result, the component code will look like this:
function App() {
return (
<div style = {{
width: '200px',
border: '2px solid brown',
padding: '10px',
textAlign: 'center' }}>
<p style={{
color: 'orangered',
fontWeight: 'bold' }}>
text
</p>
<p style = {{
fontStyle: 'brown',
color: 'brown' }}>
text
</p>
<p style = {{
backgroundColor: 'orange',
fontWeight: 'bold',
color: 'white' }}>
text
</p>
</div>
);
}
Take the React component you made in the task for the last lesson. Add styles to each tag directly in the style
attribute, as shown in this lesson.