Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
How To Use Inline Styles in ReactJS?
Next article icon

How to use styles in ReactJS ?

Last Updated : 09 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

React is a popular JavaScript library for building single-page applications (SPAs) with dynamic user interfaces. Styling in React can be done in various ways, each with its advantages. In this article, we will explore different approaches to applying styles in React, including inline styles, CSS files, CSS modules, and styled components.

Prerequisites

Before you start, make sure you have:

  • A basic understanding of CSS.
  • ReactJS set up in your development environment.

Table of Content

  • Using Inline Styles
  • Using CSS File
  • Using CSS Modules
  • Using Styled-Components

Steps to Create a React Application

Before learning into the different styling approaches, let's start by creating a React application.

Run the following command to create a new React app:

npx create-react-app  app-name                                                                                 

Replace my-app with your desired project name.

1. Using Inline Styles

Inline styles in React are defined using the style prop, which accepts an object. The keys in the object are written in camelCase, and the values are the corresponding CSS values.

Syntax: The syntax to assign inline styles to CSS elements is mentioned below.

<div style={{backgroundColor: 'red'}}></div>

Example: The content of the App.js file is mentioned in the code given below in which we have added inline styling to the React elements. 

React
const App = () => {     return (         <div             style={{                 display: "flex",                 alignItems: "center",                 justifyContent: "center",                 height: "100vh",                 backgroundImage:                     "linear-gradient(to right, #427ceb, #1dad6f)",             }}         >             <h1 style={{ color: "white" }}>GeeksForGeeks</h1>         </div>     ); };  export default App; 

Step to run the application: Use the following command to start the application.

npm start

Output: Open the browser and go to http://localhost:3000, you will see the following output.

Note: For all below-given examples, the output will remain as above only. Though you can verify it at your end by pasting the content of App.js and App.css files and running the React app on your device.

2. Using CSS File

To style elements using a traditional CSS file, you can create a CSS file and import it into your React component.

Syntax: The syntax to assign the classes to the className prop is mentioned below. 

<div className="name_of_the_class"></div>

Example: The content of App.js and App.css files demonstrating the use of CSS files to style React elements is mentioned below.

CSS
/*App.css*/  .container-div {     display: flex;     align-items: center;     justify-content: center;     height: 100vh;     background-image: linear-gradient(to right, #427ceb, #1dad6f); }  .heading {     color: white; } 
React
// App.js  import './App.css';  const App = () => {     return (         <div className='container-div'>             <h1 className='heading'>GeeksForGeeks</h1>         </div>     ); };  export default App; 

3. Using CSS Modules

CSS Modules allow you to locally scope CSS by generating unique class names. To use CSS Modules, create a file with the .module.css extension and import it into your component.

Syntax:

import styles from './App.module.css';

Now we can easily assign the classes to the className properties mentioned below.

<div className={styles['container-div']}> 
<h1 className={styles.heading}>GeeksForGeeks</h1>
</div>

The square bracket is used to access the class when it contains a hyphen or we can use it generally also. The dot can be used to access the class when it does not contain a hyphen. 

Example :The content of App.js and App.css files demonstrating the use of CSS modules to style the React element is mentioned below.

CSS
/*App.modules.css*/  .container-div {     display: flex;     align-items: center;     justify-content: center;     height: 100vh;     background-image: linear-gradient(           to right, #427ceb, #1dad6f); }  .heading {     color: white; } 
React
// App.js  import styles from './App.module.css';  const App = () => {     return (         <div className={styles['container-div']}>             <h1 className={styles.heading}>GeeksForGeeks</h1>         </div>     ); };  export default App; 

4. Using Styled-Components

styled-components is a popular library for writing component-level styles in JavaScript. It allows you to create styled components with a combination of CSS and JavaScript.

Module Installation: In order to use the styled-components you must first install it as a dependency using the following command from the command line.

npm install styled-components

Syntax: To create a styled component you can use the syntax mentioned below.

import styled from 'styled-components';
const GeeksHeading = styled.h1`
color: white;
`;

The code above will create a new component based on the h1 element and style it with the CSS properties passed to it. The content of the App.js file demonstrating the use of styled-components is mentioned below.

React
// App.js  import styled from 'styled-components';  const PageDiv = styled.div`   display: flex;   align-items: center;   justify-content: center;   height: 100vh;   background-image: linear-gradient(       to right, #427ceb, #1dad6f); `;  const GeeksHeading = styled.h1`   color: white; `;  const App = () => {     return (         <PageDiv>             <GeeksHeading>GeeksForGeeks</GeeksHeading>         </PageDiv>     ); };  export default App; 

Next Article
How To Use Inline Styles in ReactJS?
author
shivamsingh00141
Improve
Article Tags :
  • Web Technologies
  • ReactJS
  • Web technologies
  • React-Questions

Similar Reads

  • How To Use Inline Styles in ReactJS?
    Inline styles in React allow you to directly apply CSS styles to elements using the style attribute. This is often used for dynamic styling, where styles need to be applied based on the component's state or props. Inline styles in React are written as JavaScript objects. Each CSS property is camelCa
    3 min read
  • How to use Inline Styles in React ?
    ReactJS is a JavaScript library that is used to build a user interface. React makes it easy to create an interactive user interface by using a component-based approach. It offers various ways to apply styles to components. One of the methods is using inline styles, where styles are defined directly
    3 min read
  • Using styled API in ReactJS
    Styled components in ReactJS is a CSS-in-JS library that can be used for a better UI design. This API allows us to create a styled component and apply all styling properties. For using Styled API in React we need to install styled components. Prerequisites:Basics of ReactJSStyled ComponentsApproachT
    3 min read
  • How to style with SASS/SCSS in React ?
    Styling in React is very important for creating better user interfaces. using SASS/SCSS in React, which offers many features over traditional CSS, you can write more maintainable and structured stylesheets. Prerequisite:Introduction to SASSKnowledge of ReactNode and NPM/Yarn installedSetting up envi
    1 min read
  • How to use ReactJS with HTML Template ?
    We all know ReactJS is a front-end development framework. For the front end, we need to create HTML templates. So, this article teaches you how to use HTML templates in ReactJS. The process is very simple.  Prerequisites:NodeJS or NPMReact JSApproach: We will create a simple one-page HTML template i
    4 min read
  • How to Set Text Color in ReactJS ?
    React provides you the ability to create interactive and dynamic use­r interfaces. Within these­ interfaces, the choice of text color holds significant importance as it enhance­s the visual appeal and engage­ment for users. A foundational aspect of styling revolve­s around modifying text color. In t
    3 min read
  • How to create Tabs in ReactJS ?
    Tabs make it easy to explore and switch between different views. Material UI for React has this component available for us and it is very easy to integrate. We can use Tabs Component in ReactJS using the following approach. Prerequisites:NPM & Node.jsReact JSMaterial UIwe have these approaches t
    4 min read
  • How to add styles to autocomplete in ReactJS ?
    Autocomplete functionality in web applications is common for predictive user input. This article focuses on enhancing the visual appearance of ReactJS autocomplete components using Material-UI's package, offering customization options for seamless integration with the overall application design. Pre
    3 min read
  • How to Create List Styling in ReactJS ?
    React, a declarative, efficient, and flexible JavaScript library for building user interfaces, plays a crucial role as the 'V' in MVC. Specifically, ReactJS, an open-source, component-based front-end library maintained by Facebook, focuses on the view layer of applications. In this article, we will
    3 min read
  • How to Use and Style React Components in Gatsby?
    Gatsby is a popular static site generator that leverages React for building user interfaces. React components play a crucial role in Gatsby, allowing developers to create reusable UI elements and manage the state effectively. This article explores how to use and style React Components in Gatsby. Ste
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences