CSS in JavaScript (CSS-in-JS) has become a popular approach for styling modern web applications, and Emotion css is the one of the best libraries for it. If you’ve heard about Emotion but aren’t sure how it works or why it’s useful, this blog is just for you!
By the end, you’ll understand:
First of all Emotion css is a library specially designed for writing css in styles with javascript. It provides predictable style composition for great developer experience.
It provides two ways to style components:
1. Styled Components : Similar to the styled-components library.
2. CSS Prop : A lightweight way to use Emotion without creating separate styled components.
Since styles are written inside JS files, you get dynamic theming, scoped styles, and better maintainability all things while keeping performance higher.
1. Scoped Styles (No More Global Conflicts!)
What if I say forget about class name conflicts ? Emotion generates unique class names, you don’t have to worry about styles affecting accidental elements.
2. Dynamic Styling with Props
With Emotion css, styles can change based on component props, which makes it easy to create theme aware components.
3. Automatic Vendor Prefixing
No need to manually add browser prefixes like -webkit- or -moz-. Emotion handles it for you.
4. Better Performance Than Traditional CSS
Emotion removes unused styles and only loads what's necessary, making pages load faster.
5. Works with Server-Side Rendering (SSR)
Unlike some CSS-in-JS solutions, Emotion works great with SSR (important for Next.js apps!).
There are two packages available, one is @emtion/core and the other one is @emotion/styled, which powers the styled-components.
We will see the second one here,
Run the following command to install Emotion in your project:
1
npm install @emotion/react @emotion/styled
OR if you're using yarn:
1
yarn add @emotion/react @emotion/styled
If you've used Styled Components, Emotion’s styled API will feel familiar.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** @jsxImportSource @emotion/react */
import styled from "@emotion/styled";
const Button = styled.button`
background-color: #0070f3;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
&:hover {
background-color: #005bb5;
}
`;
export default function App() {
return <Button>Click Me</Button>;
}
The Button component is now fully styled and reusable!
You can also style elements inline using the css prop instead of styled components.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/** @jsxImportSource @emotion/react */
import { css } from "@emotion/react";
const buttonStyle = css`
background-color: #0070f3;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
&:hover {
background-color: #005bb5;
}
`;
export default function App() {
return <button css={buttonStyle}>Click Me</button>;
}
Want to apply global styles and themes? Emotion supports Theme Providers to make styling even more flexible.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { ThemeProvider } from "@emotion/react";
const theme = {
colors: {
primary: "#0070f3",
secondary: "#ff4081",
},
};
export default function App() {
return (
<ThemeProvider theme={theme}>
<Button>Styled with Emotion!</Button>
</ThemeProvider>
);
}
Now, your styled components can access the theme like this:
1
2
3
4
5
6
const Button = styled.button`
background-color: ${(props) => props.theme.colors.primary};
color: white;
padding: 10px 20px;
border-radius: 5px;
`;
Pros:
Cons:
As far we have discussed a lot regarding emotion css. Let's summurize in points...