Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use inline styling in ReactJS

Getting Started with Inline Styling in ReactJS

ReactJS, a JavaScript library, is widely used for constructing user interfaces, particularly for single-page applications. One of the key aspects of making your application visually appealing and user-friendly is styling. So let's talk about inline styling in ReactJS.

Understanding Inline Styling

Before we jump into the code, let's take a moment to understand what we mean by "inline styling". Imagine you're writing a letter. You could define how every element of the letter should look at the top of the document - that's a bit like using a CSS (Cascading Style Sheets) file. But what if you wanted a single word or sentence to look different? You might change the style right there in the line - that's inline styling.

In ReactJS, we handle inline styles a little differently than in HTML. Instead of a CSS file, we use JavaScript objects to define styles. It's like giving each HTML element its own unique set of instructions for how to dress up!

Creating Your First Inline Style in ReactJS

Let's start with a simple example: a heading that we want to style.

Here's what it would look like in HTML with a CSS file:

<h1 style="color: red; font-size: 24px;">Hello World</h1>

In ReactJS, we use JavaScript objects for our styles instead. Here's how we'd write the same heading:

const headingStyle = {
  color: 'red', 
  fontSize: 24
};

<h1 style={headingStyle}>Hello World</h1>

This is our first inline style! We've told our h1 heading to be red and have a font size of 24 pixels.

Keys as CamelCased

In our JavaScript object, you'll notice that fontSize is written in camelCase. This is a standard in JavaScript where the first word is all lowercase, and every word after that begins with a capital letter, with no spaces. This is one area where inline styles in ReactJS differ from CSS, where we'd use a hyphen (font-size).

Using Inline Styles with Components

ReactJS is all about components - reusable, self-contained bits of code. Let's see how we can use inline styles with our components.

const buttonStyle = {
  backgroundColor: 'blue',
  color: 'white',
  padding: '10px 20px',
  border: 'none'
};

function BlueButton() {
  return <button style={buttonStyle}>Click Me</button>;
}

Here we've defined a BlueButton component with a certain style. Any time we want a blue button, we can just use this component and know it will always have the style we want.

Conditional Styling

Sometimes, you might want to change the style of an element based on certain conditions. For instance, a button might change color when it is clicked. Inline styling makes this very easy.

Here's an example where we change the color of our button when it's clicked:

class ClickableButton extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      clicked: false
    }
  }

  handleClick = () => {
    this.setState({ clicked: !this.state.clicked })
  }

  render() {
    const buttonStyle = {
      backgroundColor: this.state.clicked ? 'green' : 'blue',
      color: 'white',
      padding: '10px 20px',
      border: 'none'
    };

    return <button style={buttonStyle} onClick={this.handleClick}>Click Me</button>;
  }
}

In this code, we've added to our button a clicked state that changes when the button is clicked. We've then used this state to change the backgroundColor in our style object.

Conclusion

Imagine you're an artist with a palette full of colors. Inline styling in ReactJS is like having an infinite palette to paint your components any way you like, anytime you want. It's a powerful tool that, when used effectively, can make your application not only look good, but also feel more interactive and responsive.

Remember, the key to good styling is consistency and meaningful design. Don't change styles without reason. Each style should enhance the user's experience, guide them through your application, and make their journey as enjoyable as possible. Now go forth and paint the digital world with your inline styles!