Live Preview
Ready Ln 1, Col 1
UTF-8 HTML/JS

Pure CSS Day and Night Theme Toggle Switch with Animated Sun, Moon, Stars, and Clouds

Modern websites often offer a light/dark theme toggle for better accessibility and user comfort. If you’re looking for a stylish and interactive way to switch between day and night modes, this advanced toggle switch built with only HTML and CSS is a perfect solution.

This unique theme switch button combines animated visuals such as:

  • A glowing sun

  • A sliding moon with crater spots

  • Moving clouds

  • Appearing stars

And the best part? No JavaScript is needed.


What This Theme Switcher Does

The toggle starts in day mode:

  • Blue background represents the sky

  • Fluffy clouds float at the bottom

  • A bright yellow sun icon appears in the circle

When clicked:

  • The background smoothly turns dark

  • Clouds slide down out of view

  • Stars rise up and twinkle in the background

  • The sun morphs into a moon with detailed crater spots

The entire transformation is handled using CSS transitions, ::before, and key CSS properties for layout and animation.


Key Features

1. Pure HTML + CSS Only

This theme switcher works without any JavaScript. It uses a hidden checkbox to toggle styles and trigger the animation flow using :checked selectors in CSS.

2. Highly Customizable

All design elements such as:

  • Toggle size

  • Background colors

  • Moon and sun styling

  • Animation timing

…are controlled via CSS custom properties (variables), making the component easily adjustable for your site’s theme.

3. Responsive & Lightweight

Because it’s built entirely with HTML and CSS, the component is lightweight and works across modern browsers with excellent performance on both desktop and mobile.


How It Works Behind the Scenes

  • A <label> wraps the entire component and uses a hidden <input type="checkbox"> to toggle between day and night.

  • When the checkbox is checked, CSS rules apply dark theme colors, hide the clouds, and reveal the stars.

  • The circular slider (sun/moon) smoothly glides from left to right and changes its visual representation using nested divs.

  • Stars are drawn with inline SVG paths and styled via fill: currentColor for flexibility.

  • The moon’s craters are created using .spot elements with carefully placed absolute positions.


Perfect For:

  • Personal portfolios

  • Blogs and content-heavy websites

  • Dashboards and admin panels

  • Mobile web apps that support dark mode

This toggle is not just functional — it’s visually rich, delightful, and sure to impress your visitors.


Conclusion

Offering a theme toggle switch is no longer just a luxury — it’s becoming an expectation. This fully animated CSS day-night toggle button makes your site feel more interactive and engaging while giving users control over their experience.

You can easily implement it in your project and even connect it to CSS variables or a JS-based theme system if needed later.

This code uses HTML and CSS only — no JavaScript included.

MIT License

Copyright – 2025 https://youtu.be/S6T6hrc8cQo
Copyright – 2025 Galahhad (Galahad)

Permission is freely granted to anyone who obtains a copy of this software and its accompanying documentation files (referred to as “the Software”), allowing them to use, copy, modify, merge, publish, distribute, sublicense, and even sell copies of the Software. Additionally, users are allowed to permit others to use the Software under these same terms.

It is required that the above copyright notices and this permission notice be included in all versions or significant portions of the Software.

Please note, the Software is provided “as is”, without any form of warranty—express or implied. This includes, but is not limited to, warranties of merchantability, fitness for a specific purpose, or non-infringement. Under no circumstances shall the original authors or rights holders be held responsible for any claims, damages, or other liabilities that may arise from the use of the Software, whether through legal action, negligence, or otherwise.

All code on Freeofcosts.com is reviewed before publishing. Each post includes a live code editor with real-time preview, so you can experiment and learn by doing. Most code is open-source and free to use or modify under respective licenses.

Leave a Reply

Your email address will not be published. Required fields are marked *