Theming styled-components with CSS custom properties

Theming styled-components with CSS custom properties

Recently, I made the transition from Vue to React (more about it here). With Vue, I used to write pure CSS in the old-school style tag.

Recently, I made the transition from Vue to React (more about it here). With Vue, I used to write pure CSS in the old-school style tag. Now with the transition to React, I wanted to experience something different, so I decided to give styled-components a try. Honestly, so far I love it. It helps to keep a maintainable styling which is both easy to refactor and expand. The only downside is that if you overuse their props system, your styling can become less readable and messy. This is exactly why I love CSS custom properties (variables), they help you to keep your CSS clean and neat, especially when it comes to theming!

CSS custom properties

It’s just like any other variable system you know

CSS custom properties often called CSS variables, contain values that can be reused throughout the application. It’s just like any other variable system you know. They are supported on 94.75% of the browser market share, so it’s even production-ready.

Defining a custom property is like setting a CSS attribute the only difference is that you need to prefix with --.

:root {
  --theme-primary: #F5005E;
}

In the example above, I initialized a new custom property called theme-primary and set its value to #151618. The :root pseudo-class is used to make sure the new custom property is available throughout our document and not just in a specific scope.

javascript web-development css react

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Hire Dedicated React Native Developer

Have you ever thought of having your own app that runs smoothly over multiple platforms? React Native is an open-source cross-platform mobile application framework which is a great option to create mobile apps for both Android and iOS. **[Hire...

Make an App with React JS / JavaScript: API Results, Conditionals, and CSS

Make an App with React JS / JavaScript 04: API Results, Conditionals, and CSS. Setting up API and Key. useData Hook. Reorganizing the code. Display results on Screen. JSON Response from API. CSS Updates for Search Result

Hire Dedicated React Native Developer in India | React Native Development

Hire dedicated React Native developers for your next project. As the top react native development company we offer you the best service as per your business needs.