Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

W3.CSS Dark Mode



Share

Dark Mode

Dark mode uses dark background colors with light text.

With W3.CSS, you can switch between light and dark mode by adding or removing the w3-black class.

Example

<button class="w3-button w3-black" onclick="darkMode()">Toggle Dark Mode</button>

<script>
function darkMode() {
  document.body.classList.toggle("w3-black");
}
</script>

Try it Yourself »


How It Works

The document.body property refers to the <body> element.

The classList.toggle() method adds the w3-black class if it is not there, and removes it if it is already there.

This lets the same button switch between light and dark mode.


Dark Mode for a Section

You can also switch dark mode on a single section instead of the whole page.

Example

<div id="demo" class="w3-container">
  <h2>Dark Mode Section</h2>
  <p>Click the button to change this section.</p>
</div>

<button class="w3-button w3-black" onclick="darkMode()">Toggle Dark Mode</button>

<script>
function darkMode() {
  document.getElementById("demo").classList.toggle("w3-black");
}
</script>

Try it Yourself »



What You Have Learned

  • Dark mode uses dark backgrounds with light text
  • The w3-black class can create a simple dark mode
  • classList.toggle() can switch dark mode on and off
  • Dark mode can be applied to the whole page or to a single section

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.