Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to apply styles on a button visible on render in ReactJS

Getting Started

In the fascinating world of software development, visuals play a significant role. Buttons are one of the basic UI (User Interface) elements that we use every day. They are the gateway between the user and the functionality of the application.

Now, imagine that you're designing an interface for a magical door. A button on this interface would be the equivalent of a shiny, big red knob on the door. You want this knob to be appealing and to match the overall look of the door. This is exactly what we're going to do today with a button in ReactJS. We'll make it not only functional but also visually appealing.

What is Styling?

Before we dive into the code, let's understand the concept of styling. In web development, styling refers to the process of defining the look and feel of a UI element. It's like selecting the color, shape, and size of the knob for our magical door.

Why Styling a Button is Important?

Styling a button is important because it enhances user experience. Just like the knob, a well-designed button can entice users to interact with your application. It can also communicate the function of the button. For example, a red button might signify an important action like 'Submit' or 'Delete'.

How to Style a Button in ReactJS

Now that we have a basic understanding of the importance of button styling, let's dive into how you can style a button in ReactJS.

Inline Styling

The simplest way to style a button in ReactJS is by using inline styles. It's just like painting our magical door's knob directly, without using any blueprint or design.

Here is an example:

<button style={{ color: 'white', backgroundColor: 'blue' }}>Click me!</button>

In the example above, the button will be displayed with white text on a blue background.

Using CSS Classes

While inline styles are easy to implement, using CSS classes is a more powerful and flexible way to style your buttons. It's like creating a design blueprint for our knob and then implementing it on the door.

Here's how you can do it:

  1. First, you need to create a CSS file with the styling rules for your button. For example:
// styles.css
.myButton {
  color: white;
  background-color: blue;
  1. Then, in your React component, you can import this CSS file and use the class to style your button:
import './styles.css';


<button className="myButton">Click me!</button>

In the code snippet above, the button will adopt the styles defined in the 'myButton' class from the imported CSS file.

Styling a Button on Render

Sometimes, you may want to style a button based on certain conditions when rendering the component. It's like changing the color of the knob to green when the door is unlocked and to red when it's locked.

Here's how you can do it:

const Button = ({ isDoorOpen }) => {
  const buttonStyle = isDoorOpen ? 'greenButton' : 'redButton';

  return (
    <button className={buttonStyle}>Click me!</button>

In this example, the button's style will be determined by the 'isDoorOpen' prop when the component is rendered. If 'isDoorOpen' is true, the button will be styled with the 'greenButton' class, otherwise, it will use the 'redButton' class.


In the grand scheme of things, the look of a button might seem like a trivial detail. But in the symphony of software development, every little detail contributes to the overall user experience. It's the seemingly insignificant details like the color, shape, and size of a button that can make the difference between an app that's merely functional and one that's truly delightful to use.

Styling a button in ReactJS might not be as adventurous as designing a magical door, but it's a fundamental skill that can add a touch of magic to your applications. The key is to experiment with different styles and approaches until you find what works best for your project and your users.

After all, in the realm of software development, the only limit is your imagination. So, keep learning, keep creating, and don't be afraid to add a splash of color to your buttons!