Detect the preference (DARK | LIGHT) of a user's computer

Offering a dark version of your website is gradually becoming a standard that sites need to adopt for the benefit of their users. In this article we'll look at how to create a dark theme and how to detect your visitor's preferences.
•
Updated on August 14, 2026
•
2 min read
Believemy logo

Offering a dark version of your website is gradually becoming a standard that sites must adopt for the benefit of their users. Let's see in this article how to create a dark theme and how to detect your visitor's preferences.

Image

 

Step 1

Let's start with the HTML code!

HTML
<section>
  <h1>Detecting a user's preference for a dark or light theme on their computer.</h1>
  <p>
    Tutorial by Believemy
  </p>
  <a href="https://believemy.com">Visit</a>
</section>

Everything will be handled in the CSS.

In the previous step, we are just having fun creating a small demonstration site.

 

Step 2

Let's style the page with body.

CSS
body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  font-family: arial, sans-serif;
  background: var(--background);
  color: var(--foreground);
  text-align: center;
}

The secret lies in using variables (--background and --foreground here).

 

Step 3

Proceeding to the title.

CSS
h1 {
  color: var(--color);
  margin-bottom: 0.6rem;
  font-size: 3rem;
}

 

Step 4

Let's take care of the link.

CSS
a {
  display: inline-block;
  text-decoration: none;
  color: var(--foreground);
  margin-top: 4rem;
  padding: 1rem 2.4rem;
  border-radius: 0.2rem;
  background: var(--color);
}

We continue just by playing with the variables for colors.

 

Step 5

Let's create the variables.

CSS
:root {
  --background: #131416;
  --foreground: #fff;
  --color: #ff4754;
}

 

Step 6

CSS
@media (prefers-color-scheme: light) {
  :root {
    --background: #fff;
    --foreground: #131416;
    --color: #ff4754;
  }
}

You were expecting something complicated? :-)

We simply invert just the values of background and foreground.

Category: Developers

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.

Believemy logo
Comments (0)

You need to be logged in to comment on this article: log in or sign up.

Read next

Detecting the "swap" to close a menu with CSS

Think about your users when they visit your website on their mobile devices: allowing them to close your menu using the "swap" will make their experience on your site much easier, so you'll have a better user experience, which increases your chances of building loyalty. Let's find out how in this article!

December 9, 2024

Creating a NEON button with CSS

Creating a button is one thing! But creating a button that your visitors want to click is something else entirely. In any case, this neon button will catch the eye of everyone who visits your pages.

December 9, 2024

Create a full-screen menu on click

Having a menu has become an indispensable classic. But having a menu that takes up the whole screen is ideal when you want your site to be totally responsive (and therefore suitable for mobile devices, for example). This article will help you to set up a full-screen menu that is activated on click!

December 9, 2024

Create a menu that spans the entire page using CSS

From time to time we have to ask our visitors which page they want to go to, by displaying the main sections of our website. This menu is often not very pleasant to use, so let's find out together in this article how to make it much more modern, with beautiful animations that will make your passers-by want to visit your site!

December 9, 2024

How can you use CSS variables to make everything more dynamic?

Perhaps you've already had that moment when you wanted to change the main colour of one of your projects? Or perhaps you've already experienced those moments when you go back to retrieve the colours of your site's theme? It's all very time-consuming and impractical. Thanks to CSS variables, this will be a thing of the past! Let's find out how.

December 9, 2024

Create an animated gradient background on hover avec CSS

Whether it's to highlight a part of your website or a quote as in this example, generating and adding a gradient to the background can give your website a very modern touch. Find out in this article how to do it easily!

December 9, 2024

Syntax highlighting with HTML and CSS

If you have a blog or a portfolio on which you want to present code, whether HTML / JavaScript or CSS code extracts, you have undoubtedly come up against a problem: how do you activate syntax highlighting to make it look as if you had put the code in a code editor? In this article, we'll look at how to do it.

December 9, 2024

Creating an "Old School" button with CSS

Let's find out how to create an original button that's right up to date!

December 9, 2024

Become the solopreneur you dream of being.

First project, career change, business launch: access our courses, practice on real-world cases, and move forward with clear guidance.