How to create a theme switcher with HTML & CSS

How to create a theme switcher with HTML & CSS

Kevin Powell

1 год назад

108,746 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Arch Ange‍l‍ 
Arch Ange‍l‍  - 13.09.2023 17:07

Too bad one can't easily switch stylesheets or palettes.

Ответить
Jader Rubini
Jader Rubini - 05.09.2023 20:02

Quick honest question: does it even make sense to have a theme selector for users on a screen reader? Shouldn’t we just hide it altogether?

Ответить
Dominika Wojewska
Dominika Wojewska - 27.08.2023 19:36

What a fantastic tutorial! I learned so much and finally understood :has() a tiny bit better. However, when I'm trying it out in does work perfectly in Chrome but not at all in Firefox, even with the fallback :( I tried playing around with it to make it work in Firefox, but it just doesn't want to...

Ответить
HotPattyStudios
HotPattyStudios - 19.06.2023 20:08

So humble man...How he is apologizing for one mistake!❤❤

Ответить
Morrile1
Morrile1 - 19.06.2023 14:30

How can we restore the current colours assuming these five new colours are additional and not the default? Going to assume we can delete all and any saved cookies related to theme. My knowledge of JS is very limited.

Ответить
Mikinitsu Noru
Mikinitsu Noru - 15.06.2023 00:38

Is it possible to use this kind of button to make language switchers?

Ответить
Gongodler
Gongodler - 26.05.2023 22:15

Is there a way to place the theme switcher inside the navbar container and only change the "Content" area to black theme? I
If I place the color switcher outside of the container I have the problem, that I cannot make the switcher responsive. Do you have an idea how to solve this problem? I would like to only use CSS and HTML, if possible.

Ответить
Bunker
Bunker - 23.05.2023 23:50

Good luck Kevin. I referred your channel to my interns. You have wonderful contents. Big thumbs up for your effort 🤜🤛

Ответить
Gary Reid Backing Tracks
Gary Reid Backing Tracks - 07.05.2023 07:03

Oh, Lord Algorithm has popped this into my paws right on time...

Ответить
TobbeLund
TobbeLund - 28.04.2023 14:53

Fantastic tutorial!

Ответить
TheALJO
TheALJO - 23.04.2023 07:52

I am having a issue when I set my dark theme and refresh page it loads the light theme first for .5s because I have the styling with transitions , can anyone help I can’t figure it out

Ответить
Mr. CC
Mr. CC - 21.04.2023 10:58

The problem is that the page flashes until Javascript makes the selected theme selectable ;-), it's not usable
(especially on a larger site )

Ответить
Frank Drebin
Frank Drebin - 21.04.2023 05:03

Instead of apologizing for making a mistake, please consider editing your video.

Ответить
Gabriel Medina Braga
Gabriel Medina Braga - 12.04.2023 18:02

Thanks! You absolutely made me love css a bit today, it's exactly what I've been trying to figure out the past few days (in the end all I needed was to know :has in css).
Here is my token of gratitude (it's not gonna be a lot because of my currency, sry about that 😅)

Ответить
privus priv
privus priv - 04.04.2023 20:31

thanks for sharing this knowledge

Ответить
CZghost
CZghost - 29.03.2023 11:51

Another way to store the theme selection would be through the use of cookies, but that's beyond the scope of HTML/CSS, it's more of a backend solution, but that will get rid of the flashing.

Ответить
Mia Xoxo
Mia Xoxo - 20.03.2023 21:30

my has didn't work :( doesn't seem to recognise the theme id

Ответить
ion utale
ion utale - 17.03.2023 23:10

:Has() is not even needed for this 😅

Ответить
Sean McPherson
Sean McPherson - 10.03.2023 18:59

:root:has() 🤯

Ответить
Māris Samats
Māris Samats - 10.03.2023 14:04

Would a screen reader care about what theme is there or what is the label for that theme? :)

Ответить
Mayukh Devan Sahu
Mayukh Devan Sahu - 02.03.2023 14:28

I just love how Kevin always makes sure to include accessibility features to his website for visually impaired persons. In general, it's very rare in tutorials where you see things like that.

Ответить
wongwanchap
wongwanchap - 27.02.2023 19:54

it should be
document.onload = retrieveTheme;
or
document.onload = () => retrieveTheme();

Ответить
No Name
No Name - 22.02.2023 13:13

Nice video. Thank you.
Info: You don't need to loop through all elements, when you want to apply your theme on load. You saved already the id, so you can search for the radiobutton with this id.
document.getElementById(activeTheme).checked = true;

Ответить
Kevin Busch
Kevin Busch - 21.02.2023 17:44

Wow, that's such a cool "hack"!

Ответить
Bruno B
Bruno B - 04.02.2023 22:30

Hi kevin, loved the tutorial.

changing the root on checked input does not seen to work on firefox to me, only currently working on chrome. Have you heard of it?

Ответить
Felix T-Rex
Felix T-Rex - 29.01.2023 13:56

I had a really weird dream last night

Ответить
speedstyle
speedstyle - 16.01.2023 05:32

If you put the JS after the form, you can set the --vars immediately/synchronously rather than in the onload, avoiding the 'FOUC'. Or if your server isnt static you can put it in a cookie, and respond with the right theme in the header

Ответить
Kyrill Goosseff
Kyrill Goosseff - 10.01.2023 15:02

This is incredible Kevin, thank you so much. I just watched it through in 1 go. And I will watch it again slower to exactly implement the Code and try it myself. I really love your channel and videos. Just now I shared your channel with me co-workers cause at least 3-5 of us are all working, studying and learning about CSS :)

Ответить
Toothless Old Man
Toothless Old Man - 09.01.2023 03:10

not working at all, keep getting 'name="theme"' is not a valid selector, both firefox and chrome display same error

Ответить
/ шмырк
/ шмырк - 16.12.2022 07:54

No JS? 🤦🏻🤦🏻🤦🏻

Ответить
Enrique Rodríguez
Enrique Rodríguez - 14.12.2022 14:57

Great again Kevin! 😎 :has... I can't wait for all browsers to fully implement it.

Ответить
Yuriy Kolomytsyn
Yuriy Kolomytsyn - 12.12.2022 19:42

Thank you so much for sharing this great experience with us :) have a productive day

Ответить
Nelson Espinosa
Nelson Espinosa - 08.12.2022 16:45

Awesome! Thank you for share this!!

Ответить
EpicNicknameFail Tunchi
EpicNicknameFail Tunchi - 03.12.2022 21:03

Really cool !!

Ответить
Gagik Harutyunyan
Gagik Harutyunyan - 02.12.2022 18:09

to solve the problem with jumping, you can remove the default checked property inside html, then add else {} block, and if(...) {...} else { colorThemes[0].checked = true }

Ответить
Tran Thanh Lan
Tran Thanh Lan - 01.12.2022 04:50

This king completes people's skills. Respect.

Ответить
Énio Carlos
Énio Carlos - 28.11.2022 03:10

Amazing Tutorial.

Ответить
Jimmy Tan
Jimmy Tan - 24.11.2022 09:43

Thanks for the video!

Ответить
Purpinkn H
Purpinkn H - 20.11.2022 12:57

themes not theme's

Ответить
Collins K
Collins K - 17.11.2022 21:27

Great tutorial! 👍👍

Ответить
Shibu N
Shibu N - 17.11.2022 09:26

Have you checked browser compatibility :has()

Ответить
Capitulate [Tenn Os]
Capitulate [Tenn Os] - 17.11.2022 01:31

You forgot to add that fallback code to the storeTheme function as well. Without Has support you'd need to refresh the page to see the theme change. Either way, always a big fan of your videos, good work.

Ответить
GuruKuen
GuruKuen - 16.11.2022 18:34

Using cookies as opposed to localstorage could be a way around the window.onload issue.
This would of course require the back-end renderer to do some of the job.
But it doesn't have to be more than setting the initial html class or checking the correct select option.

Ответить
Sahad Pop
Sahad Pop - 13.11.2022 13:05

i coded theme switcher to my site and it didnt work on vs code but when i copied the whole thing to codepen it worked!!
idk how.... whats the problem ???
Edit: i came to know that it doesnt work on firefox but works on chrome idk why

Ответить
Adarsh Singh
Adarsh Singh - 13.11.2022 09:43

Good One...

Ответить
N V
N V - 12.11.2022 16:54

main.js was not getting detected. Even after adding '<script src="main.js"> </script>' to the head of index.html

Ответить
YeudaChrist
YeudaChrist - 12.11.2022 14:22

Another great tutorial! 👌🏼

To fix the flash, using 'DOMContentLoaded' event instead of 'load' or put the script as inline script after the color picker element.

Ответить