The Wayback Machine - https://web.archive.org/web/20240622024543/https://www.geeksforgeeks.org/react-styled-components-module/
Open In App

React styled-components Module

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

React Styled-component Module allows us to write CSS within JavaScript in a very modular and reusable way in React. Instead of having one global CSS file for a React project, we can use styled-component to enhance the developer experience. It also removes the mapping between components and styles – using components as a low-level styling construct

Features of styled-component:

  • Automatic vendor prefixing
  • Replacement of writing style prop everywhere.
  • Support for media query (unlike style prop).
  • Easy to maintain

Prerequisites:

Approach:

React-styled component is used to create react-reusable components with defined CSS. These components are created using JavaScript syntax with styling in multiline strings. Here’s an example to do it.

Steps to create React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app styled

Step 2: After creating your project i.e. styled, move to the same directory:

cd styled

Step 3: 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:

Project Structure

The updated dependencies in package.json file will look like.

"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"
}

Example: This example shows simple inplementation of styled-components to create reusable button components with paramete background color.

Javascript




// Filename - App.js
 
import Button from "./Button";
 
function App() {
    return (
        <div
            style={{
                textAlign: "center",
                margin: "auto",
            }}
        >
            <h1 style={{ color: "green" }}>
                GeeksforGeeks
            </h1>
            <h3>React Styled-components example</h3>
            <Button bg="green"> Click </Button>
            <Button bg="yellow"> Click </Button>
        </div>
    );
}
 
export default App;


Javascript




// Filename - Button.js
 
import styled from "styled-components";
 
const Button = styled.div`
    height: 50px;
    width: 100px;
    cursor: pointer;
    text-decoration: none;
    color: blue;
    background-color: ${(props) =>
        props.bg === "green" ? "green" : "yellow"};
    margin: auto;
    font-size: 2rem;
`;
 
export default Button;


Step to Run Application: Run the application using the following command from the root directory of the project:

npm start

Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Screenshot-from-2023-11-07-18-02-39



Similar Reads

How do Styled Components work in terms of styling React components?
Styled Components is a library for React and React Native that allows you to write CSS in your JavaScript code in a more component-oriented way. Working of Styled Components:Component-Based Styling: Styled Components allow you to write actual CSS directly into your JavaScript files using tagged template literals. This means you can define styles fo
2 min read
Animated modal using react, framer-motion & styled-components
In this article, we are going to learn how to create an animated model using react, framer-motion &amp; styled components. Prerequisites:JavaScript (ES6). HTMLCSSReactJS. React useStateSteps to Create React Application And Installing Module:Step 1: Now, you will start a new project using create-react-app so open your terminal and type. npx create-r
4 min read
What are Styled Components in React?
Styled Components is a popular library for styling React components using tagged template literals and CSS syntax. It allows developers to write CSS in JavaScript, creating components with encapsulated styles directly within the component definition. Features of Styled Components in React:CSS-in-JS Approach: Styled Components follow the CSS-in-JS a
1 min read
Global Styling with styled-components in React
In web development, styling is an important aspect of building visually appealing user interfaces. styled-components, a popular library that allows you to write CSS directly in the JavaScript code. In this article, we'll explore how to use styled-components for global styling in React applications. Prerequisites:ReactJSReact Styled-Components Modul
2 min read
Explain the benefits of using Styled Components.
Using Styled Components in a React application offers several benefits, which contribute to improved development experience, code maintainability, and performance. Key advantages of Styled Components:Component-Based Styling: Styled Components allow you to define styles directly within the component definition, creating a more modular and component-
1 min read
React MUI Styled Engine
The Material UI library provides a way to style applications using material design principles. The "Styled Engine" refers to the way that Material UI allows you to customize the styles of your components using a CSS-in-JS approach, which involves writing JavaScript objects that represent CSS styles. The Material UI Styled Engine is built on top of
3 min read
Semantic-UI Accordion Styled Type
Semantic UI is a modern framework used in developing seamless designs for the website, It gives the user a lightweight experience with its components. It uses predefined CSS and jQuery to incorporate into different frameworks. The accordion type is used to specify the type of accordion used for the particular condition. The Semantic-UI Accordion St
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 ReactJSAlready created ReactJS appSteps to use styled components in ReactJS:Step 1
2 min read
How to apply custom color to glyphicon icon embed within a link styled with Bootstrap ?
Given an HTML document with relevant Bootstrap glyphicon and the task is to apply the custom color to them. Approach: First, we need to assign the id attribute to the particular glyphicon which you need to customize by using CSS. We can apply the color property to the particular id and change the icon color by using a hex value or normal color. The
2 min read
Blaze UI Lists Un-styled
Blaze UI is a free open source UI Toolkit that provides a great structure for building websites quickly with a scalable and maintainable foundation. All components in Blaze UI are developed mobile-first and rely solely on native browser features, not a separate library or framework. It helps us to create a scalable and responsive website fast and e
2 min read