Dark mode has become increasingly popular with interface design, bringing visual comfort and allowing users to choose the theme they prefer.
In this article, you will learn how to create dark mode on a page using JavaScript, HTML, CSS and modern web techniques.

Dark Mode Introduction
Dark mode has been gaining importance in digital design, reflecting a change in users' preferences.
In addition, mobile devices consume less battery when using dark mode, especially on OLED screens.
With so many benefits, many applications and websites already offer this option, allowing users to customize their experiences.

Why adopt the dark mode?
Using dark mode offers a visually more comfortable experience, especially in low light environments.
Dark way advantages
Implementing dark mode has several advantages.
Dark background text reduces visual effort and increasing users' engagement.

Research shows that people spend more time on websites that offer this functionality, indicating that marks that adopt the dark mode are more aligned with the needs of their customers.
Another significant benefit is the saving of battery on mobile devices with OLED screens, as they consume less energy in dark mode.
This economy makes Dark Mode a smart choice for users and developers.
Dark mode advantages:
- Visual comfort: Reduces eye tiredness in low light environments.
- Increased engagement: Users remain more time on pages with Dark Mode.
- Battery savings: Reduces consumption on devices with OLED screens.
- Personalization: It offers users a choice that adapts to their preferences.
Using CSS and LocalStorage variables
CSS variables are a practical way to store values and facilitate design adaptation. -BG-GERY-LIGHT
to define the clear background color and --Text-Dark
for the dark text.
O local
It is a powerful tool for storing data on the browser.
HTML BASIC STRUCTURE
A well -organized HTML structure is essential for implementing dark mode.
Essential Elements:
: Button to switch between light and dark modes.
<h1>
: Main page title.<p>
: Text paragraphs that facilitate reading.<div>
: Divisions to group and style content.
Stylizing with CSS
Choosing the right colors is critical to making dark mode pleasant. .Light
e .Dark
Facilitates the exchange between themes.
Color Example:
- Main text: #000000 on the clear theme, #FFFFF in the dark theme.
- Bottom: #FFFFF in Claro Theme, #121212 on the dark theme.
- Links: #007BFF on the Clear theme, #82B1FF on the dark theme.
Implementando o Modo Escuro com JavaScript
To create the dark mode with JavaScript, it is necessary to manipulate the gift, adding classes to the element body
from the page. local
ensuring that the theme chosen by the user is maintained after the page recharge.
A basic code to implement dark mode would be:
IF (localSoStorage.Getite ('Darkmode') === 'Enabled') {
CSS Events and Class Manipulation
Dark mode functionality is enabled with JavaScript using Event Listeners, which detect user actions, such as buttons clicks to change the theme.
Basic Steps:
- Choose the theme exchange button.
- Add an event listener to detect clicks.
- Change the class class on the page.
Manipulating classes in JavaScript is critical to the implementation of Dark Mode. classlist
, you can switch between topics easily.
Document.body.classlist.Toggle ('Dark');
Testing and improving implementation
It is essential to test the implementation of dark mode to ensure that everything works properly.
Possible improvements:
- Implement gentle transitions between themes to a more pleasant visual experience.
- Offer color customization options, allowing users to choose from various palettes.
- Include accessibility features such as colorblinds or people with visual difficulties.
Benefits of improvements:
- Soft transitions: Improve usability and reduce visual wear.
- Customizable palettes: Increase user satisfaction and customization of the interface.
- Accessibility: It makes experience more inclusive, meeting various visual needs.
Conclusion
Adopting dark mode goes beyond visual aspect;
Using HTML, CSS and JavaScript, you can turn the navigation experience by offering a more flexible and adapted design to the digital future.