How to usestyled-components in ReactJS

The styled-components is a third-party package that helps us create a new Styled component based on the React element and CSS styles provided to it. 

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.

Javascript




// 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>w3wiki</GeeksHeading>
        </PageDiv>
    );
};
 
export default App;




How to use styles in ReactJS ?

React is a JavaScript front-end library that is used to build single-page applications (SPA). React apps can easily be styled by assigning the styles to the className prop. 

Similar Reads

Prerequisites

CSS fundamentals Knowledge of React JS...

Steps to create React Application

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

Approach 1: Using Inline Styles

In order to apply the inline styles to the elements, we use the style prop. We pass an object with key as CSS properties in camelCase and value as the values that can be assigned to these CSS properties....

Approach 2 : Using CSS file

...

Approach 3: Using CSS module

To style the React elements using the CSS file, we first import the CSS file and then assign the classes contained in the CSS file to the className prop of React elements....

Approach 4: Using styled-components

...