Menu

Emotion for Beginners: CSS-in-JS That Feels Like Writing CSS
📰

Emotion for Beginners: CSS-in-JS That Feels Like Writing CSS

DEV Community: css·Arhan Khan·14 days ago
#p0ZIfYzM
#devtocss#devto#dev#eee#emotion
Long article (341 words) — no AI summary yet.
Reading 0:00
15s threshold

Emotion is a CSS-in-JS library. It lets you write real CSS — the same selectors, media queries, and pseudo-classes you already know — but inside your JavaScript files, scoped to a component. If you like CSS but hate managing class names and global stylesheets, Emotion is the middle ground.

There are two ways to use it. The css prop lets you style any element inline:

/** @jsxImportSource @emotion/react */ import { css } from "@emotion/react" ;

function Card ( ) { return ( < div css = { css ` padding: 24px; border-radius: 12px; background: #fff; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); &:hover { transform: translateY(-2px); } ` } > Hover me </ div > ) ; }

Enter fullscreen mode

Exit fullscreen mode

Or use styled to create reusable components that look like normal HTML tags:

import styled from "@emotion/styled" ;

const Button = styled . button ` padding: 10px 20px; background: ${(props) => (props.primary ? "#0070f3" : "#eee")}; color: ${(props) => (props.primary ?…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.