Skip to content

Day/Night Toggle

Animated pure CSS day and night toggle switch where the sun turns into a cratered moon, clouds drift away and stars appear.

Also known as: dark mode toggle, sun and moon switch, theme switcher, light dark mode switch

Day/Night Toggle live demo

Open in new tab

Pure CSS code (HTML, CSS)

An animated day and night toggle switch built with pure CSS. In day mode a glowing sun sits in a blue sky above fluffy clouds; when switched on, the sun rolls across the track and turns into a cratered moon, the clouds sink away and stars fade in on a navy sky. Use it as a dark mode switch in headers, settings panels or portfolio sites.

How it works

The root section.dn-scene holds a .dn-card with the main switch and a row of smaller examples. Every switch is a label.dn-toggle wrapping a visually hidden input.dn-input (a checkbox with role="switch") and a decorative span.dn-track. The track contains .dn-stars, .dn-clouds and the .dn-orb with three .dn-crater spans.

The track paints the day sky from --dn-sky-day, and its ::before layer holds --dn-sky-night with opacity: 0. Clouds and stars are single elements multiplied with box-shadow copies, and the halo rings around the sun are extra spread shadows on .dn-orb. When the input is checked, the .dn-input:checked + .dn-track selectors fade in the night sky, move the orb with translateX(5.25em), recolor it to --dn-moon, show the craters, push the clouds down and drop the stars into place. Every dimension uses em, so the font size on .dn-toggle scales the whole switch. Finally, .dn-scene:has(.dn-toggle--main .dn-input:checked) swaps the page and card colors, so the main toggle themes the panel without JavaScript.

Customization

  • --dn-size on .dn-toggle: the base font size that scales every part of the switch; .dn-toggle--sm and .dn-toggle--lg set 6px and 15px.
  • --dn-sky-day and --dn-sky-night on .dn-scene: the two sky gradients of the track.
  • --dn-sun, --dn-moon and --dn-crater-color: the orb colors in each state and the crater color.
  • --dn-speed: the duration of every transition, including the theme change of the card.
  • --dn-focus: the outline color shown around the track on keyboard focus.
  • Add checked to an .dn-input to start in night mode, as on the default-size example.
  • Move the .dn-toggle--main class to another toggle to make it control the panel theme.

Use cases

  • Site headers: the sun turns into a cratered moon as visitors switch to dark mode
  • Settings panels: a playful toggle replaces a plain checkbox for choosing a theme
  • Portfolio sites: clouds drift away and stars fade in for a memorable detail
  • Blogs and docs: a compact switch lets readers pick light or dark reading

Accessibility

Each switch is a native checkbox with role="switch" and an aria-label such as "Dark mode", so it is announced as a switch with its on or off state and toggles with Space. The track is aria-hidden, as is the "Day mode / Night mode" caption, since the state is already announced. Keyboard focus draws an outline around the track with --dn-focus. Under prefers-reduced-motion: reduce all transitions become near-instant.

Usage notes

To theme a real site, read the checkbox in a few lines of JavaScript and set a class or data-theme on html. The :has() theming needs a current browser; the switch animation works without it.

Day/Night Toggle at a glance

Framework
Pure CSS
Code
HTML, CSS
JavaScript
Not needed, CSS only
Dependencies
None
Layout
Responsive, works on mobile
Category
Toggles & Switches
License
Free for personal and commercial use

Questions about this template

Can I use the Day/Night Toggle code for free?

Yes. You can copy or download the code and use it in personal and commercial projects, including client work. No sign-up is needed. Reselling it as part of a component library or template collection is not allowed; see the license.

How do I add it to my website?

There is nothing to install. Paste the HTML where the component should appear and add the CSS to your stylesheet. Download HTML gives you a complete page with everything already linked.

Does it need jQuery or a JavaScript framework?

No. It is built with HTML and CSS only, so there is no JavaScript to load.

Does it work on mobile?

Yes. The layout adapts to small screens. Use the Mobile and Tablet buttons above the live demo to check it at phone and tablet width.

Requirements

No dependencies. Pure HTML, CSS and JavaScript.

Day/Night Toggle – free Pure CSS toggle & switch template
Day/Night Toggle · free Pure CSS toggle & switch template

More Toggles & Switches

iOS Style Toggle Switch – free Pure CSS toggle & switch template

iOS Style Toggle Switch

Pure CSS iOS style toggle switches built on real checkboxes, in a grouped settings list with three sizes, five colors and a disabled state.

Settings Toggles with Description – free Tailwind CSS toggle & switch template

Settings Toggles with Description

Tailwind CSS settings panel with accessible switches, label descriptions, a locked required option, restore defaults and an auto-save status message.

Toggler Button Demos – free Bootstrap 6 toggle & switch template

Toggler Button Demos

Four Bootstrap 6 Toggler demos on a dark panel: a favorite heart, a read-more expander, a light/dark theme preview and a grid/list view switch, all with data attributes.

Settings Panel with Switches – free Bootstrap 6 toggle & switch template

Settings Panel with Switches

Bootstrap 6 notification settings panel with switches grouped in cards, master switches per group, a pause-all toggle, unsaved-change tracking and a simulated save.

Neumorphic Toggle Switch – free Pure CSS toggle & switch template

Neumorphic Toggle Switch

Pure CSS neumorphic toggle switches with inset shadow tracks, raised knobs and glowing indicators, built on accessible native checkboxes.

Toggle Switches with Labels – free Bootstrap 6 toggle & switch template

Toggle Switches with Labels

Dark Bootstrap 6 settings panel with .switch toggles in three sizes, labels with descriptions, disabled states and a simulated auto-save status.