Using styled API in ReactJS
Last Updated :
03 Oct, 2024
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:
Approach
To use the styled API in React JS we will install the styled-components npm package in our project. Then import and define the components using styled with custom stylings and directly use them as normal react components.
Steps to use styled components in React JS
Step 1: Initialize a React Project
Create a React application using the following command:
npx create-react-app styled
Step 2: Switch to Project Directory
After creating your project i.e. styled, move to the same directory:
cd styled
Step 3: Install styled-components
Use the command given below to install styled-components in your React application.
With npm:
npm i --save styled-components
With yarn:
yarn add styled-components
Project Structure:

List of updated dependencies in the package.json file:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^6.1.0",
"web-vitals": "^2.1.4"
}
Step 4: Import the Module
Now import React and styled modules.
import React from 'react';
import styled from 'styled-components';
Below is a sample program to illustrate the use of styled-components:
Example 1: This example uses the styled component to change the background color of the button to green.
JavaScript
// Filename - App.js
import React from "react";
// Importing styled from styled-components
import styled from "styled-components";
// Importing the background-color of your
// choice to the button using css
// Button component that will render an <a>
// tag with some styles using styled.
const Button = styled.a`
background-color: green;
color: white;
padding: 1rem 2rem;
margin-top: 100px;
width: 150px;
display: block;
`;
const App = () => {
return (
<center>
<Button>GeeksforGeeks</Button>
</center>
);
};
export default App;
Output:

Example 2: This example uses the styled components to add a border to the button.
JavaScript
// Filename - App.js
import React from "react";
// Importing styled from styled components
import styled from "styled-components";
// Importing the background-color of your
// choice to the button using css
// Button component that will render an <a>
// tag with some styles using styled.
const Button = styled.a`
background-color: green;
color: white;
padding: 1rem 2rem;
margin-top: 100px;
width: 150px;
display: block;
border: 8px solid black;
`;
const App = () => {
return (
<center>
<Button>GeeksforGeeks</Button>
</center>
);
};
export default App;
Output:

Hence, using the above-mentioned steps, we can use the Styled-components to import and change the style of components in React.
Similar Reads
Movie Trailer app using ReactJS In this article, we are going to make a simple app that searches for any movie/web series trailers. The users can search for their favourite movie trailers using this application. The API will fetch the trailer from the internet and display it on the webpage. We will use 'movie-trailer' npm package
3 min read
File Uploading In ReactJS File uploading in ReactJS is an important functionality for many web applications, especially those that require user interaction, such as submitting forms, uploading images, or handling documents. In this article, we will walk you through the process of implementing file upload functionality in Rea
5 min read
News App using React News App using React JS uses React's basic principles to fetch the latest news from a News API and show them to users based on their interests. It's a simple project to create an interactive and personalized news-viewing experience.Preview of final output: Let us have a look at how the final output
4 min read
Movie Search Engine Using React and API In this article, we will create Movie Search Engine using ReactJS. In this movie search engine the user can search for movies which they want to know about and the search engine will fetch a list of movies from that keyword and display the result in the forms of card. A default search has been loade
6 min read
Weather Application using ReactJS In this article, we will develop an Interactive Weather Application using ReactJS Framework. The developed application will provide real-time weather information to the user for the city they have searched. If the user searches, for the wrong city, an Error message is also displayed to the user, sta
5 min read