Posted in code
2454
1:51 am, July 17, 2019
 

how to add a darkmode toggle to switch css and remember it dark mode

So it seems the trend lately is to add darkmode to everything...

Dark Mode

Light Mode

Its actually not that hard to switch style sheets and add a dark version of the same style, it seems to be a bit laggy in switching though as its quite a large script, but it does work.

This was actually harder than i thought, you can just switch out the whole style sheet, but if its a big one it flashes and then loads the alternate one after each page load.

So i had to add a function that replaced the class names with the dark version.

You can view the code here.

Update: actually i never finished this post but i did solve this problem. I originally had one that just switched the style sheet and saved it to local storage (in the browser) as well to remember it.

Code is below... for real this time! 😉

Actually i just realised this was content rather than code, so ill link the code post.

darkmode toggle switch with local storage to remember the last selection 

View Statistics
This Week
70
This Month
297
This Year
690

No Items Found.

Add Comment
Type in a Nick Name here
 
Search Articles
Search Articles by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
You drown not by falling in the river, but by staying submerged.
Unknown
Random CSS Property

scroll-margin-top

The scroll-margin-top property defines the top margin of the scroll snap area that is used for snapping this box to the snapport. The scroll snap area is determined by taking the transformed border box, finding its rectangular bounding box (axis-aligned in the scroll container's coordinate space), then adding the specified outsets.
scroll-margin-top css reference