How to style components using the emotion library in react

Key takeaways:

  • Emotion is a CSS-in-JS library that allows us to style react components using javascript or typescript, enabling scoped and dynamic styling.

  • To use emotion, create a react project using create-react-app and install the emotion library with npm install @emotion/react @emotion/styled.

  • Use the styled function from emotion to create styled components, defining CSS rules as template literals within the component.

  • Import and use the styled components within the main application component (e.g., App.js), ensuring they render with the desired styles.

Emotion is a popular library for styling components in react, providing a way to write CSS styles using javascript or typescript. It allows developers to write CSS styles using javascript or typescript directly within their components

Prerequisite

To use the emotion library, we need to create a react project, and we need to install the emotion library in that project folder. The following steps will create a react project and install the emotion library.

  1. . Execute the following command and create a new react application using create-react-app:

npx create-react-app emotion-styled-components-demo
  1. After creating a project, navigate into your project folder:

cd emotion-styled-components-demo
  1. Let's install the emotion library using the command below in our react project folder.

npm install @emotion/react @emotion/styled

After creating the react project, your project files will look like this:

# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
A template react project

Steps to style components using the emotion library

Here’s a step-by-step guide on how to style components using the emotion library:

  1. In our project, open the src directory and create a new file called emotionComponent.js.

  2. In emotionComponent.js, import the necessary libraries, and create a styled component. Here's an example:

//emotionComponent.js
import React from 'react';
import styled from '@emotion/styled';
const StyledButton = styled.button`
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
`;
function emotionComponent() {
return (
<div>
<StyledButton>Styled Button</StyledButton>
</div>
);
}
export default emotionComponent;
  1. Open the src/App.js file and import the emotionComponent function and use it in your application:

import logo from './logo.svg';
import './App.css';
import emotionComponent from './emotionComponent';
function App() {
return (
<div className="App">
<h1>Emotion Styled Component Example</h1>
<emotionComponent />
</div>
);
}
export default App;

Updated code

We followed the above-mentioned steps, and the resultant project is as follows:

# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
A template React project

Code explanation

Following is the code explanation in the following files:

emotionComponent.js file

  • Lines 2–3: We import the react library, which is necessary for creating react components. We also import the styled function from the emotion library, which is used to create styled components.

  • Lines 5–14: The styled.button code is used to create a styled component named StyledButton. This component represents a styled HTML <button> element. Inside the backticks (``), CSS rules are defined as template literals. These rules specify the styling for the button:

    • background-color: The button's background color is set to blue (#007bff).

    • color: The text color is set to white (#fff).

    • padding: Add 10 pixels of padding on top and bottom and 20 pixels on the left and right.

    • border: Removes the button's border.

    • border-radius: Rounds the corners of the button.

    • cursor: Sets the cursor to a pointer when hovering over the button.

    • &:hover {background-color: #0056b3;}: Change color when hovering over the button.

  • Lines 17–22: We defined a functional component named emotionComponent . Inside the component, a div element is wrapped around the StyledButton component. The StyledButton component is used to render a button with the specified styles. The text “Styled Button” is placed inside the button.

App.js file

  • Line 3: EmotionComponent is imported, which is the react component you created and styled using emotion.

  • Lines 7–11: We define the App functional component. It is the main component of our application. Inside the component, a div element with the class name App is created. This class is used to apply CSS styles from the App.css file. An h1 element displays the text “emotion Styled Component Example.” The emotionComponent is rendered, which means the styled button component you created with emotion will be displayed here.

Conclusion

In conclusion, by following the steps to incorporate the emotion library in our react application, we have successfully empowered our components with dynamic and scoped styling capabilities.

Emotion allows us to efficiently manage styles, create styled components, and achieve a more modular and maintainable codebase. With our application now showcasing styled components, we're well-equipped to build engaging and visually appealing user interfaces while benefiting from the advantages of CSS-in-JS.

This accomplishment marks a significant step toward crafting a responsive and well-styled react application.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


Can we use emotion with existing CSS?

Yes, we can use emotion alongside existing CSS files without any issues.


Is emotion performant for large applications?

Yes, emotion is designed for performance and efficiently handles large stylesheets.


Can we create responsive designs with emotion?

Yes, we can create responsive designs using media queries in emotion.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved