<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml" -->

---
title: Code a cookie-based darkmode switch in CSS and vanilla...
description: Code a cookie-based darkmode switch in CSS and vanilla JavaScript. The darkmode button is located inside the <header> element on line 13. In the stylesheet, I...
canonical: https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Code a cookie-based darkmode switch in CSS and vanilla JavaScript | daily.dev
og:description: Code a cookie-based darkmode switch in CSS and vanilla JavaScript. The darkmode button is located inside the <header> element on line 13. In the stylesheet, I...
og:url: https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml
og:image: https://api.daily.dev/og/posts/7BkkwCoMl.png
og:image:alt: Code a cookie-based darkmode switch in CSS and vanilla JavaScript
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Code a cookie-based darkmode switch in CSS and vanilla JavaScript

**[gitconnected](https://daily.dev/sources/gc)** · 1 min read · 27 upvotes · 0 comments

## Summary

Code a cookie-based darkmode switch in CSS and vanilla JavaScript. The darkmode button is located inside the <header> element on line 13. In the stylesheet, I define the body in its light and dark state. Also I set the design for dark mode button in lightmode und darkmode (classname active) status.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://levelup.gitconnected.com/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-b2d89f99ea7d>

---

Tags: [#css](https://daily.dev/tags/css), [#general-programming](https://daily.dev/tags/general-programming), [#html](https://daily.dev/tags/html), [#javascript](https://daily.dev/tags/javascript)

[View this post on daily.dev](https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Code a cookie-based darkmode switch in CSS and vanilla JavaScript","url":"https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml"},"datePublished":"2021-11-24T22:02:31.266Z","dateModified":"2026-03-15T02:33:25.744Z","description":"Code a cookie-based darkmode switch in CSS and vanilla JavaScript. The darkmode button is located inside the \u003cheader\u003e element on line 13. In the stylesheet, I...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d9f3caa80352581ca271aa8afae08520","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/d9f3caa80352581ca271aa8afae08520","isAccessibleForFree":true,"articleSection":"gitconnected","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"gitconnected","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/gc","url":"https://daily.dev/sources/gc"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/code-a-cookie-based-darkmode-switch-in-css-and-vanilla-javascript-7bkkwcoml","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":27},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"css,general-programming,html,javascript","timeRequired":"PT1M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"gitconnected","item":"https://daily.dev/sources/gc"},{"@type":"ListItem","position":3,"name":"Code a cookie-based darkmode switch in CSS and vanilla JavaScript"}]}
```

