
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to switch between dark and light mode with CSS and JavaScript?n
To switch between dark and light mode with JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { padding: 25px; background-color: white; color: black; font-size: 25px; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .dark-mode { background-color: black; color: white; } .toggleButton { padding: 12px; font-size: 18px; border: 2px solid green; } </style> </head> <body> <h1>Toggle Dark/Light Mode Example</h1> <button class="toggleButton">Toggle dark mode</button> <h2>Click the above button to toggle dark mode</h2> <script> document .querySelector(".toggleButton") .addEventListener("click", toggleDarKMode); function toggleDarKMode() { var element = document.body; element.classList.toggle("dark-mode"); } </script> </body> </html>
Output
The above code will produce the following output −
On clicking the “Toggle dark mode” button −
- Related Articles
- How to Create a Dark / Light Mode for Websites using CSS
- How to Create Dark/Light Mode for a Website using JavaScript/jQuery?
- Differentiate between process switch and mode switch in OS
- What is Light Reaction, and Dark Reaction?
- How to toggle between a like/dislike button with CSS and JavaScript?
- What is Light Reaction, Dark Reaction, and Hill Reaction?
- How to create toggle switch by using HTML and CSS?
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create Dark Mode in ReactJS using Material UI?
- The colours of aqueous solutions of CuSO4 and FeSO4 as observed in the laboratory are:(a) Pale green and light blue respectively. (b) Light blue and dark green respectively.(c) Dark blue and dark green respectively. (d) Dark blue and pale green respectively.
- How to create tab headers with CSS and JavaScript?
- Difference between Router and Switch
- Difference between Hub and Switch
- Difference between Gateway and Switch

Advertisements