CSS variable with Styled Components

Use Them Easily in Next.js/React.js With TypeScript

Dev By RayRay
Β·Mar 12, 2021Β·

2 min read

CSS variable with Styled Components

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Since I’m working with Next.js and Styled-components (also TypeScript, React, GraphQL, Apollo, Storybook & Storyblok CMS) I was wondering, is it possible to use CSS variable’s in Styled Components.

import React, { Component } from 'react';
import styled from 'styled-components';

const CSSVariables = styled.div`
  --color: ${props => props.color};

const Heading = styled.h1`
  margin: 2rem 0 0 0;
    color: var(--color)

const SingleStep = (props: Istep) => {
    return (
            <Heading>My Heading</Heading>
    // All component logic

As you can see in the example above, it is possible and super simple, just like in normal CSS. So make sure that you CSS variable are created in a parent component. It can also be created on a page/layout level if you like.

So I hope this helps you further with building a cool Next.js or React application with Styled-components.


hashnode-footer.png I hope you learned something new or are inspired to create something new after reading this story! πŸ€— If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode.

Did you know that you can create a Developer blog like this one, yourself? It's entirely for free. πŸ‘πŸ’°πŸŽ‰πŸ₯³πŸ”₯

If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a DM on Twitter @DevByRayRay when you want to keep it private. My DM's are always open 😁

Did you find this article valuable?

Support Dev By RayRay by becoming a sponsor. Any amount is appreciated!

See recent sponsors |Β Learn more about Hashnode Sponsors
Share this


I learn by writing, and write while learning more and more each day!πŸ‘Š A developer is never finished with learning, thinking that is the most stupid thing to do 🀣