DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Difference Between Cross-Browser Testing and Responsive Testing
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Optimizing Java Applications for Arm64 in the Cloud

Trending

  • You Are Using Claude Wrong (And So Is Everyone You Know)
  • Introduction to Retrieval Augmented Generation (RAG)
  • Reactive Kafka With Spring Boot
  • Using the Spring @RequestMapping Annotation

Switching Between Light and Dark Themes Automatically

Learn how to detect the OS theme configuration to set a dark or light theme automatically in plain HTML and Vaadin Flow apps

By 
Alejandro Duarte user avatar
Alejandro Duarte
DZone Core CORE ·
Nov. 02, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
9.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this quick tutorial, I'll show you how to automatically set a dark or light theme in a web application depending on the configuration that the user has in their operating system:

setting light and dark themes

If you prefer, here's a video version of this tutorial:


Automatic Dark and Light Themes in HTML Documents With CSS

The following steps show how to enable automatic dark and light themes in HTML documents using CSS.

Step1: Create an HTML Document

Create a demo.html file with the following content:

HTML
xxxxxxxxxx
1
14
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="UTF-8">
5
    <title>Dark and light</title>
6
</head>
7
<body>
8
9
<h1>Hello!</h1>
10
<p>This is an example app.</p>
11
<button>Click me</button>
12
13
</body>
14
</html>


Here's a screenshot:

hello world dark theme

Step2: Implement a Dark Theme

Add the following to the head section of the demo.html file:

HTML
xxxxxxxxxx
1
 
1
<style>
2
    body {
3
        color: aliceblue;
4
        background-color: midnightblue;
5
    }
6
</style>


Here's the result:

Hello world dark theme

Step 3: Use a Media Query to Apply the Dark Theme

Edit the code of the previous step to include the dark styles in a media query that uses a media feature:

HTML
xxxxxxxxxx
1
 
1
<style>
2
    @media (prefers-color-scheme: dark) {
3
        body {
4
            color: aliceblue;
5
            background-color: midnightblue;
6
        }
7
    }
8
</style>

Step 4: Test the Spp

Open the HTML document in the browser and use your operating system settings to change the preferred theme. The changes should be automatically reflected in the browser.

Automatic Dark and Light Themes in Web Components and Vaadin Flow Apps

The following steps show how to enable automatic dark and light themes using JavaScript. This is useful if your application uses Web Components that require special configurations or if you are using Vaadin Flow.

Step 1: Create a New Java Web Application With Web Components

Go to https://start.vaadin.com and generate a new application. You can leave all the defaults or tweak it as you wish by adding and removing views or changing the appearance. Follow the instructions and links on the page to import the generated Maven project in your IDE of choice.

Step 2: Implement a Media Query in JavaScript

Create a new os-theme-switcher.js file inside the PROJECT_ROOT/frontend/ directory with the following content:

JavaScript
xxxxxxxxxx
1
 
1
function applyTheme() {
2
    let theme = matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
3
    document.documentElement.setAttribute("theme", theme);
4
}
5
6
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", applyTheme);
7
applyTheme();

Step 3: Load the JavaScript File

Add the following annotation to the MainView Java class:

Java
 




xxxxxxxxxx
1


 
1
...
2
@JsModule("./os-theme-switcher.js")
3
public class MainView extends AppLayout {
4
    ...
5
}


Step 4: Test the App

Run the app with Maven:

Shell
x
 
1
mvn spring-boot:run


Go to http://localhost:8080 and use your operating system settings to change the preferred theme. The changes should be automatically reflected in the browser.

Light (web browser) operating system Web application

Opinions expressed by DZone contributors are their own.

Related

  • Difference Between Cross-Browser Testing and Responsive Testing
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • From 13,000 to 20,000+ Endpoints: Architecting Forensics for the Remote Workforce
  • Optimizing Java Applications for Arm64 in the Cloud

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook