Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Gaurav Singhal

How to Assign a Class to an Element

Gaurav Singhal

  • Jun 9, 2020
  • 7 Min read
  • Jun 9, 2020
  • 7 Min read
Web Development
Front End Web Development
Client-side Framework


React creates intuitive user interfaces by following component-based architecture, in which each component consists of various HTML elements that require styling logic. There are various options available to style the component, and your choice ultimately depends on what is the most feasible and suitable styling for your situation.

In this guide, you will learn different ways of styling components, such as using inline style, using the class name, and conditional class name assignment.

Assign Inline Styles

Inline style assignment is one of the approaches used to style elements. It uses the name of the style followed by its values.

To provide the inline style to the elements, use the added property along with the element called style.

A complete example of inline styling is below.

1import React, { Component } from "react";
3class InlineStyle extends Component {
4  render() {
5    return (
6      <div>
7        <span>Apply inline style to element</span>
8        <div
9          style={{
10            position: "absolute",
11            top: "50%",
12            left: "50%",
13            transform: "translate(-50%,-50%)"
14          }}
15        >
16          <span>This is a centered DIV using CSS inline style</span>
17        </div>
18      </div>
19    );
20  }
23export default InlineStyle;

In the above example, notice that there is one added property called style, used like this.

2       position: "absolute",
3       top: "50%",
4       left: "50%",
5       transform: "translate(-50%,-50%)"

It can apply the various stylesheet rules, and it is also possible to have multiple stylesheet rules using comma separator styles.

Assign a Class Name

As you create any new web app, you can choose a stylesheet configuration such as CSS, SCSS, or SASS. Assign the styles at the time of implementing the chosen stylesheet.

Another way of assigning the class name to the various HTML elements is by using the class name.

Below is a simple example that shows how to assign the class name directly to the element.

The first step is to add the class to the CSS stylesheet file, as shown below.


1.myDiv {
2  position: absolute;
3  top: 50%;
4  left: 50%;
5  transform: translate(-50%,-50%);

After creating the stylesheet file, the next step is to create the element and assign the class name that you created before.

1import React, { Component } from "react";
2import "./style.css";
4class AssignClass extends Component {
5  render() {
6    return (
7      <div>
8        <span>Apply inline style to element</span>
9        <div className="myDiv">
10          <span>This is a centered DIV by applying class name</span>
11        </div>
12      </div>
13    );
14  }
17export default AssignClass;

As you can see, the class myDiv is created in the stylesheet file, and the added property className is used to apply the styles to the <table> element.

This is how the class name can be assigned directly from the stylesheet, and this is the conventional approach to using class names.

Assign the Styles or Class Name Conditionally

So far in this guide. you have learned simple approaches to assigning styles, including using the inline style and using the stylesheet name. In many cases, however, you may need to assign the styles conditionally.

The styles or class name can be assigned conditionally by either using state & props, ternary operation, or any other conditional operator, such as && (and) or || (or).

Let’s create the styles class in the file style.css like this.

1.tableRoot {
2  border: 2px solid purple

The next step is to create the state variable with the default value as stated below.

1constructor() {
2    super();
3    this.state = {
4      isTableVisible: true
5    };

In the above example, the state variable created is called isTableVisible with the true as a default value. The same variable can be used to assign the dynamic class name, as explained below.

1render() {
2    return (
3      <div>
4        <span>Apply multiple class names</span>
5        <div>
6          <table className={this.state.isTableVisible ? 'tableRoot' : ''}>
7            <tr>
8              <td>Column 1</td>
9              <td>Column 2</td>
10            </tr>
11            <tr>
12              <td>Column 1</td>
13              <td>Column 2</td>
14            </tr>
15          </table>
16        </div>
17      </div>
18    );

In the render() function, there is one <table> element, but the provision is that the style is applied if the state variable is true, and if it is false, it works like this.

1<table className={this.state.isTableVisible ? 'tableRoot' : ''}>

This is one of the best approaches for when the class or styles need to be assigned dynamically based on a specific condition.

Assign Multiple Classes Using a Third-party Library

So far in this guide, you have learned how to assign styles or classes using conventional approaches, but when it comes to assigning multiple classes to an element, try the default approach below.

1<table className="classname1 classname2 classname3">

The same thing is also possible using the third-party package classnames, a widely used NPM package, to assign the class name.

Before using the package, install it using the below npm command.

1npm install classname

After installing the package, use it as shown below.

1import React, { Component } from "react";
2import classNames from "classnames";
3import "./style.css";
5class ClassNames extends Component {
6  render() {
7    return (
8      <div>
9        <span>Apply multiple class names</span>
10        <div>
11          <table className={classNames("tableRoot", "tableAnotherRoot")}>
12            <tr>
13              <td>Column 1</td>
14              <td>Column 2</td>
15            </tr>
16            <tr>
17              <td>Column 1</td>
18              <td>Column 2</td>
19            </tr>
20          </table>
21        </div>
22      </div>
23    );
24  }
27export default ClassNames;

Here the primary focus is the line where the package classnames has been used.

1<table className={classNames("tableRoot", "tableAnotherRoot")}>

The import method classNames is used along with various class names as an argument. By using this method, you can assign multiple classnames, and the related styles can be applied to the desired element.


Styling an element is a standard operation in app development. You can choose different styling options, such as CSS or SCSS, and create related classes and use them along with the HTML elements.

In this guide, you learned all the usage scenarios to apply classnames and styles, including using inline style and class name third-party packages. I hope it will help you to style your app effortlessly. Thanks for reading!