Open In App

8 Ways to Style React Components

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

React is a JavaScript library for building user interfaces. React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.

There are about eight different ways to style React JS components, there names and explanations of some of them are mentioned below.

  1. Inline CSS
  2. Normal CSS
  3. CSS in JS
  4. Styled Components
  5. CSS module
  6. Sass & SCSS
  7. Less
  8. Stylable

Here are a few examples to style React Components.

Inline CSS:

In inline styling basically, we create objects of style. And render it inside the components in style attribute using the React technique to incorporate JavaScript variable inside the JSX (Using ‘{ }’ )

Example 1: This example illustrates how to use inline styling to style react components. 

Javascript




// Filename - App.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - StudentList.js
 
import React, { Component } from "react";
 
class StudentList extends Component {
    render() {
        const { name, classNo, roll, addr } = this.props;
        const ulStyle = {
            border: "2px solid green",
            width: "40%",
            listStyleType: "none",
        };
        const liStyle = { color: "blue", fontSize: "23px" };
        return (
            <ul style={ulStyle}>
                <li style={liStyle}>Name : {name}</li>
                <li style={liStyle}>Class: {classNo}</li>
                <li style={liStyle}>Roll: {roll}</li>
                <li style={liStyle}>Address : {addr}</li>
            </ul>
        );
    }
}
 
export default StudentList;


Output: 

Styling using inline styles

Example 2:

Javascript




// Filename - App.js
 
import React from 'react';
import Lottery from './Lottery'
 
function App() {
  return (
    <div>
      <Lottery />
      <Lottery title='Mini Daily'/>
    </div>
  );
}
 
export default App;


Javascript




// Filename - Lottery.js
 
import React, { Component } from "react";
import Ball from "./Ball";
 
class Lottery extends Component {
    static defaultProps = {
        title: "lotto",
        numbers: [1, 2, 3, 4, 5, 6],
    };
 
    render() {
        const lotteryStyle = {
            border: "2px solid grey",
            borderRadius: "10px",
            width: "40em",
            textAlign: "center",
            margin: "1em auto auto",
            padding: "1em 0 2em 0",
        };
        return (
            <div style={lotteryStyle}>
                <h3>{this.props.title}</h3>
                <div>
                    {this.props.numbers.map((n) => (
                        <Ball num={n} />
                    ))}
                </div>
            </div>
        );
    }
}
 
export default Lottery;


Javascript




// Filename - Ball.js
 
import React, { Component } from "react";
 
class Ball extends Component {
    render() {
        const ballStyle = {
            backgroundColor: "tomato",
            borderRadius: "50%",
            width: "3em",
            height: "2.25em",
            textAlign: "center",
            paddingTop: "0.75em",
            display: "inline-block",
            marginRight: "0.5em",
            marginTop: "1em",
            color: "white",
            fontWeight: "bold",
            fontSize: "1.5em",
        };
        return (
            <div style={ballStyle}>{this.props.num}</div>
        );
    }
}
 
export default Ball;


 Output:

Inline Styling

Normal CSS:

In the external CSS styling technique, we basically create an external CSS file for each component and do the required styling of classes. and use those class names inside the component. It is a convention that name of the external CSS file same as the name of the component with ‘.css’ extension. It is better if the name of the classes used, follow the format ‘componentName-context’ (here context signifies where we use this classname). For example, if we style the header of a component called ‘Box’, a better classname should style this element should be ‘Box-header’.

Example: This example illustrates how to style react components with external stylesheets. 

Javascript




// Filename - StudentList.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
import "./App.css
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - App.js
 
import React, { Component } from "react";
 
class StudentList extends Component {
    render() {
        const { name, classNo, roll, addr } = this.props;
        return (
            <ul className="StudentList">
                <li className="StudentList-details">
                    Name : {name}
                </li>
                <li className="StudentList-details">
                    Class: {classNo}
                </li>
                <li className="StudentList-details">
                    Roll: {roll}
                </li>
                <li className="StudentList-details">
                    Address : {addr}
                </li>
            </ul>
        );
    }
}
 
export default StudentList;


CSS




/* Filename: App.css */
 
.StudentList{
    border: 2px solid green;
    width: 40%:
    list-style-type: none;
    }
 
.StudentList-details{
    color: blue;
    font-size: 23px;
    }


Output:

External CSS styling

CSS in JS:

The’react-jss’ integrates JSS with react app to style components. It helps to write CSS with Javascript and allows us to describe styles in a more descriptive way. It uses javascript objects to describe styles in a declarative way using ‘createUseStyles’ method of react-jss and incorporate those styles in functional components using className attribute.

Step to Install required Module: install third-party react-jss package using the below command in terminal

npm i react-jss

Example : This example demonstrate the use of react-jss library to add style to react components.

Javascript




// Filename - App.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - App.js
 
import React, { Component } from "react";
import { createUseStyles } from "react-jss";
 
const styles = createUseStyles({
    student: {
        border: "2px solid green",
        width: "40%",
        listStyleType: "none",
    },
 
    studentDetails: {
        color: "blue",
        fontSize: "23px",
    },
});
 
const StudentList = (props) => {
    const classes = styles();
    const { name, classNo, roll, addr } = props;
    return (
        <ul className={classes.student}>
            <li className={classes.studentDetails}>
                Name : {name}
            </li>
            <li className={classes.studentDetails}>
                Class: {classNo}
            </li>
            <li className={classes.studentDetails}>
                Roll: {roll}
            </li>
            <li className={classes.studentDetails}>
                Address : {addr}
            </li>
        </ul>
    );
};
 
export default StudentList;


Output: 

Styling with CSS in JS

Styled Components: The styled-components allows us to style the CSS under the variable created in JavaScript. style components is a third party package using which we can create a component as a JavaScript variable that is already styled with CSS code and used that styled component in our main component. styled-components allow us to create custom reusable components which can be less of a hassle to maintain.

Step to Install required Module: install third-party styled-components package using the below command in terminal.

npm i --save styled-components

Example: This example use styled-cpmponents to create react components.

Javascript




// Filename - App.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - StudentList.js
 
import React, { Component } from "react";
import styled from "styled-components";
 
//styled component Li
const Li = styled.li`
    color: blue;
    font-size: 23px;
`;
 
//Styled component Ul
const Ul = styled.ul`
    border: 2px solid green;
    width: 40%;
    list-style-type: none;
`;
 
const StudentList = (props) => {
    const { name, classNo, roll, addr } = props;
    return (
        <Ul>
            <Li>Name : {name}</Li>
            <Li>Class: {classNo}</Li>
            <Li>Roll: {roll}</Li>
            <Li>Address : {addr}</Li>
        </Ul>
    );
};
 
export default StudentList;


Output :

Styling with styled components

CSS Modules:

A CSS module is a simple CSS file but a key difference is by default when it is imported every class name and animation inside the CSS module is scoped locally to the component that is importing it also CSS file name should follow the format ‘filename.module.css’. This allows us to use a valid name for CSS classes without worrying about conflicts with other class names in your application.

To use CSS modules create a normal CSS file, import the module you have created from within your component using the syntax

import styles from './filename.module.css

Example : 

Javascript




// Filename - App.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - StudentList.js
 
import React, { Component } from "react";
import style from "./StudentList.module.css";
 
const StudentList = (props) => {
    const { name, classNo, roll, addr } = props;
    return (
        <ul className={style.list}>
            <li className={style.details}>Name : {name}</li>
            <li className={style.details}>
                Class: {classNo}
            </li>
            <li className={style.details}>Roll: {roll}</li>
            <li className={style.details}>
                Address : {addr}
            </li>
        </ul>
    );
};
 
export default StudentList;


CSS




/* Filename: StudentList.module.css */
 
.list {
    border: 2px solid green;
    width: 40%;
    list-style-type: none;
}
 
.details {
    color: blue;
    font-size: 23px;
}


Output :

Styling with CSS modules

Sass and SCSS:

Sass is the most stable, powerful, professional-grade CSS extension language. It’s a CSS preprocessor that adds special features such as variables, nested rules, and mixins or syntactic sugar in regular CSS. The aim is to make the coding process simpler and more efficient.

Installation Steps:

  • Step 1: Before moving further, firstly we have to install node-sass , by running the following command in your project directory, with the help of terminal in your SRC folder or you can also run this command in Visual Studio Code’s terminal in your project folder. 
     
npm install node-sass
  • Step 2 : After the installation is complete we create a file name StudentList.scss .
  • Step 3: Now include the necessary CSS effects in your CSS file.
  • Step 4: Now we import our file the same way we import a CSS file in React.
  • Step 5: In your app.js file, add this code snippet to import  StudentList.scss.
import './ StudentList.scss';

Example: This example demonstrate above approach.

Javascript




// Filename - App.js
 
import React, { Component } from "react";
import StudentList from "./StudentList";
 
const App = () => {
    return (
        <div>
            <StudentList
                name="Ashank"
                classNo="X"
                roll="05"
                addr="Kolkata, West Bengal"
            />
            <StudentList
                name="Samir"
                classNo="Xi"
                roll="09"
                addr="Jalpaiguri, West Bengal"
            />
            <StudentList
                name="Tusar"
                classNo="Xii"
                roll="02"
                addr="Howrah, West Bengal"
            />
            <StudentList
                name="Karishma"
                classNo="ix"
                roll="08"
                addr="Mednipur, West Bengal"
            />
        </div>
    );
};
 
export default App;


Javascript




// Filename - StudentList.js
 
import React, { Component } from "react";
import "./StudentList.scss";
 
const StudentList = (props) => {
    const { name, classNo, roll, addr } = props;
    return (
        <ul className="list">
            <li className="details">Name : {name}</li>
            <li className="details">Class: {classNo}</li>
            <li className="details">Roll: {roll}</li>
            <li className="details">Address : {addr}</li>
        </ul>
    );
};
 
export default StudentList;


CSS




/* Filename: StudentList.scss */
 
 
&blue-shade: #264BC4;
&pale-green-color: #3CC27F;
 
.list{
    border: 2px solid &pale-green-color;
    width: 40%;
    list-style-type: none;
}
 
.details{
    color: &blue-shade;
    font-size: 23px;
}


Output :

Styling using Sass



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads