Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

sakethk
sakethk

Posted on

     

Building multiple themes with CSS..

This article is about building a site with multiple color schemes. It will change the theme without reloading the document, without adding theme class names dynamically and without duplicating the code.

I am using CSS variables in this example. You can try this even if you are using SASS or LESS.

In this example we will create a document that has background color, Dropdown (to pick scheme) , card, heading and paragraph. Here I am creating six color schemes and have given random names to them. Below are the names

  • Light
  • Dark
  • Sunset
  • Moon light
  • Cartoon
  • bluecrystal

In dropdown you will find the system option i.e., your OS theme. If your are using Dark mode in your OS, it will be Dark. Else, it will beLight.

For each scheme i defined four colors

  • --main-bg-color (Body backgound color)
  • --card-shadow-color (Card shadow)
  • --primary-text-color (Heading text color)
  • --secondry-text-color (Paragraph text color)

Important

  • We are getting system theme with the help ofprefers-color-scheme media query.
  • I am usingdata-theme attribute to set color scheme. Whenever scheme changes just we need to updatedata-theme attribute value.

Here is the Codepen. You can find color schemes and some other styles for Card, Heading and Paragraph in CSS section.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Computer jock, Programmer, Technology enthusiast and Math ❤️ er.
  • Location
    India
  • Work
    UI Developer at Observe.ai
  • Joined

More fromsakethk

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp