Author avatar

Desmond Nyamador

Inline Styling with React

Desmond Nyamador

  • Jul 31, 2020
  • 4 Min read
  • 67 Views
  • Jul 31, 2020
  • 4 Min read
  • 67 Views
Web Development
Front End Web Development
Client-side Framework
React

Introduction

In frontend projects, which seldom require the use of a single-page app, inline styles of DOM elements are often placed in the style="property:value" attribute of the target element. But in React, things are quite different when it comes to styling inline. This guide focuses on exactly how to achieve that using a real-world example of building a user profile card component.

Let's get started!

Styling Components in React

You may already be aware of the regular way of styling React components using the className attribute coupled with an external stylesheet as follows:

1
2
3
4
5
6
7
8
9
import React from "react"
import './style.css'

function myComponent(){
      return(

           return <p className="paragraph-text">ClassName Styled Text</p>
     )
}
jsx
1
2
3
4
.paragraph-text{
   font-weight: bold;
   color: beige;
}
css

Inline Styles

Achieving the same results with inline styles works quite differently. To use inline styles in React, use the style attribute, which accepts a Javascript object with camelCased properties. Example:

1
2
3
4
5
function MyComponent(){

return <div style={{ color: 'blue', lineHeight : 10, padding: 20 }}> Inline Styled Component</div>

}
jsx

Notice that the value of padding does not have a unit as React appends a 'px' suffix to some numeric inline style properties. In cases where you need to use other units, such as 'em' or 'rem', specify the unit with the value explicitly as a string. Applying that to the padding property should result in padding: '1.5em'. In addition, these styles are not vendor-prefixed automatically, so you have to add vendor prefixes manually.

Improving Readability

The readability of MyComponent reduces dramatically if styles become a lot and everything gets clunky. Since styles are mere objects, they can be extracted out of the component as shown below.

1
2
3
4
5
6
7
8
9
10
11
const myComponentStyle = {
   color: 'blue',
   lineHeight: 10,
   padding: '1.5em',
}

function MyComponent(){

     return <div style={myComponentStyle}> Inline Styled Component</div>

}
jsx

Building a Card Component

Enough talk! Let's build the user card component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const cardStyles = {
  container: {
    display: "flex",
    height: 100,
    width: 400,
    boxShadow: "0 0 3px 2px #cec7c759",
    alignItems: "center",
    padding: 20,
    borderRadius: 20,
  },
  profilePicture: {
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "orange",
    color: "white",
    height: 20,
    width: 20,
    borderRadius: "50%",
    padding: 10,
    fontWeight: "bold",
  },
  bio: {
    marginLeft: 10,
  },
  userName: {
    fontWeight: "bold",
  },
};

function userCardComponent(){

         <div style={cardStyles.container}>
        <span style={cardStyles.profilePicture}>D</span>
        <div style={cardStyles.bio}>
          <p style={cardStyles.userName}>Desmond Nyamador</p>
          <p>I just learned an easy way to style React Components</p>
        </div>
      </div>

}
jsx

Rule of Thumb

The official React documentation frowns upon the use of inline styling as a primary means of styling projects and recommends the use of the className attribute instead.

Note: Some examples in the documentation use style for convenience, but using the style attribute as the primary means of styling elements is generally not recommended. In most cases, className should be used to reference classes defined in an external CSS stylesheet. style is most often used in React apps to add dynamically computed styles at render time.

Conclusion

Well, that's a wrap! In this guide, you were introduced to the concept of inline styling in React. If you would like to read more on this, you can visit the the React docs for more details: React Styling.

Feel free to ping me on Twitter as well: Desmond Nyamador.

1