15 YouTube views, likes subscribers in 10 minutes. Free!
Get Free YouTube Subscribers, Views and Likes

How to create a theme switcher with HTML u0026 CSS

Follow
Kevin Powell

A few custom properties and the :has() selector make color theming super easy to do! Add in a bit of JS and we can save the user’s preferences in localStorage, and we’re good to go!

Of course, :has() support isn’t perfect, but we can build in a quick fallback with just a little bit more JS.

Links
✅ The code from this video: https://codepen.io/kevinpowell/pen/MW...
✅ More on visuallyhidden: https://www.scottohara.me/blog/2017/0...
✅ Current support for :has() https://caniuse.com/?search=%3Ahas()

⌚ Timestamps
00:00 Introduction
00:57 What we’re starting with
01:58 Setting up the color theme selector
04:04 Hiding the labels
05:39 Styling the color theme picker
12:05 Adding functionality with :has()
13:48 Using localStorage to remember the users choice
23:52 Adding a fallback for when there is no :has() support

Corrections:
24:54 I should have made separate selectors here, as this won't work in Firefox see codepen in description for a working version
27:20 Because I moved this here, the theme should have been changed to activeTheme see codepen in description for a working version

#css



Come hang out with other dev's in my Discord Community
  / discord  

Keep up to date with everything I'm up to
https://www.kevinpowell.co/newsletter

Come hang out with me live every Monday on Twitch!
  / kevinpowellcss  



Help support my channel
‍ Get a course: https://www.kevinpowell.co/courses
Buy a shirt: https://teespring.com/stores/makingt...
Support me on Patreon:   / kevinpowell  



My editor: VS Code https://code.visualstudio.com/



I'm on some other places on the internet too!

If you'd like a behind the scenes and previews of what's coming up on my YouTube channel, make sure to follow me on Instagram and Twitter.

Twitter:   / kevinjpowell  
Codepen: https://codepen.io/kevinpowell/
Github: https://github.com/kevinpowell



And whatever you do, don't forget to keep on making your corner of the internet just a little bit more awesome!

posted by verovivan5