DEV Community

shadowtime2000
shadowtime2000

Posted on • Originally published at h.shadowtime2000.com on

3 1

What is CSS-in-JS?

What is CSS-in-JS?

So you have all probably heard of CSS-in-JS frameworks and libraries such as emotion, styled-components, and goober.

What is CSS-in-JS

CSS-in-JS libraries follow many different patterns. But usually, they end up allowing you to do one thing:

  1. Allow you to create unique CSS classes from JavaScript

Basically, you would typically use it like this:

const myButtonsClassName = cssInJsLibraryFunctionHere({
    color: "red",
    "text-align": "center"
});

// Or with tagged template literals

const myButtonsClassName = css`
    color: red;
    text-align: center;
`
Enter fullscreen mode Exit fullscreen mode

Some libraries (like styled-components) let you create React components from those styles, and allow you to create dynamic values for styling.

const Button = styled.button`
    color: ${({ dark }) => dark ? "dark" : "white"}
`;

<Button />
<Button dark />
Enter fullscreen mode Exit fullscreen mode

Conclusion

CSS-in-JS is a pattern that allows you to write CSS styles inside your JavaScript.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs