Giter VIP home page Giter VIP logo

no-js-dark-mode-toggle's Introduction

This is an excerpt from my blog post published on 2024-01-11.

A Dark Mode Toggle Without JavaScript

Have you ever seen a toggle on a website that allows you to switch between a dark and light mode? Now it's possible to implement one without JavaScript, thanks to :has() CSS selector.

:has() premise is simple. Select a parent that has a given selector. Be it children or sibling using + or ~. With Firefox stable adding support for :has() last month, the browser support reached 90% according to caniuse.com!

Without further ado, let's start with HTML for the toggle:

<!-- ... -->
<body>
  <label>
    <input id="dark" type="checkbox">
    Dark mode
  </label>
</body>
<!-- ... -->

I hope you see where this is going! Now, the CSS for this is packed. Packed with tiny bits and tricks I learned. These are: declaring variables on elements for an override, using :has(), and using :checked state selector.

body {
  --fg: black;
  --bg: white;
  color: var(--fg);
  background-color: var(--bg);
}

body:has(#dark:checked) {
  --fg: white;
  --bg: black;
}

There are a couple more things that we can do such as:

  • Making use of label's for attribute to decouple the label from the checkbox
  • Hiding the checkbox
  • Styling the toggle
  • Changing labels based on the theme we are in
  • @media (prefers-color-scheme: dark) support

In this repository you will find an implementation with these improvements. You can view a deployed version at tymekdev.github.io/no-js-dark-mode-toggle.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.