Contents:

Free course: “Quick start in Python»
Learn MoreDark mode is becoming increasingly popular and is found in various areas, including operating systems, social networks, video hosting sites, and websites. If you develop in these areas, it is important to understand how to create a dark theme. Sooner or later, users or clients may ask you to implement this design style. Knowing the principles of dark mode development will help you improve the user experience and keep up with modern design trends.
Choosing colors for your web design plays a key role in creating a harmonious and pleasing perception of the site. To avoid strong contrast, you should use a color palette based on close hues and complementary colors. This will help create a visually comfortable space for users.
Changing the theme on the site should be simple and intuitive. To ensure that the selected theme is preserved when the page is refreshed, it is important to use technologies such as Local Storage or Cookies. These methods allow you to save user preferences, ensuring stability and ease of use. Implementing such functionality will improve the user experience and increase visitor loyalty to your resource.
Colors That Won't Hurt
Correctly selected colors play a key role in the visual perception of a website, making it not only aesthetically pleasing but also user-friendly. One of the most important aspects of a color palette is contrast. Good contrast between the background and text ensures ease of reading and helps users focus on the content. When choosing a color scheme, it is important to consider not only visual harmony but also accessibility to ensure a comfortable interaction for all users.

When choosing a color palette, it is important to avoid strong contrasts between colors. For example, black text on a white background creates a sharp contrast that can cause discomfort and eye fatigue when reading for long periods. To ensure comfortable information perception, it's worth choosing softer color combinations that will create harmony and reduce eye strain.
It's recommended to limit the use of contrasting colors to themes intended for people with visual impairments. In most cases, a light gray background, such as #f4f4f4, paired with a charcoal font (#373737) or near-black font (#111111) is a more appropriate option. This not only improves readability but also creates a more pleasing visual experience for users.

Light themes use bright colors for the background and dark shades for text and other page elements. For example, the main background can be light, while text, headings, footers, buttons, and other elements are designed in dark tones. Dark themes reverse the concept: the background should be as neutral as possible, while other elements should be slightly less dark to ensure good readability and a comfortable perception. Choosing the right color scheme is important for improving the user experience and perceiving the information on the site.
Study how themes are implemented in other applications. This can inspire you to create your own unique themes. By analyzing a variety of approaches, you can identify elements you like and adapt them to your needs. Pay attention to color palettes, fonts, and the overall composition to improve the user experience in your project.
Once you've chosen a color palette, you can begin designing the website. The right combination of shades will not only improve visual perception but also help create a unified style, which is important for attracting and retaining users. Start by creating a layout that harmoniously integrates the chosen colors. This will ensure not only aesthetic appeal but also ease of navigation. Website creation requires a meticulous approach to every detail, and the color palette is one of the key elements influencing the overall impression of the resource.
Website Creation
We will create a page and develop several themes for its content. This will diversify the information and draw attention to various aspects related to the main topic. Each topic will be unique and will be aimed at satisfying the interests of the target audience. We will focus on the quality of the content to ensure its high relevance and value for users. This will help improve the visibility of the page in search engines and increase its attractiveness to visitors. As a result, we will get an effective tool for communication and knowledge sharing.
This page contains a header and short text content. The main feature is the inclusion of two style files: main.css and light.css. The main.css file contains the main styles that shape the page's appearance, while light.css is responsible for the color palette for the light theme. This allows for flexible design customization based on user preferences and improves content comprehension. Proper integration of styles helps create a user-friendly and attractive interface.
The second tag has an ID, which is necessary for the script to function correctly. This ID will change the path to the file. When the path changes, the browser will automatically download and apply the styles from the new file. This allows for dynamic style updates on the web page, providing flexibility and ease of development.
It's best to place basic styles in the main.css file, avoiding specific color specifications. Instead, use neutral shades to ensure a smoother transition between themes. This approach will prevent sharp contrast changes, for example, between a white background and black text, which will improve the user experience and make the interface more harmonious.

Creating a light theme
The light.css file sets the colors for all elements on the page. This file is the primary stylesheet, ensuring visual unity and harmony throughout the design. Proper use of the color palette in light.css helps create an attractive and user-friendly interface that improves the user experience. Make sure you choose colors that match the overall style of your website and help highlight important elements. Update color values as needed to keep your project looking modern and relevant.

This interface uses light shades to create a pleasant atmosphere, with the exception of the theme change button. In a dark theme, all elements will be presented in a reversed color scheme, while maintaining the overall structure and functionality.
Creating a Dark Theme
Create a dark.css file and define the colors for the dark theme. This will allow you to focus on implementing the functionality for changing and saving themes later. Correct design of styles in dark.css will ensure a convenient user interface that meets modern requirements.

Switching themes
To implement the theme change, we will create the ChangeTheme() function, which will be activated when the corresponding button is clicked. This function will check the currently active style and then switch it to the opposite. This will allow users to easily switch between light and dark themes, improving their experience with the web application. By providing this functionality, we will make the interface more flexible and convenient for users with different design preferences.
Finally, the Save() function is called to save the current theme. At this stage, you can leave this function empty to test the correctness of the script.

Saving themes
Now everything functions correctly, but when the page is refreshed, the user's theme switches back to light. To solve this problem, you will need a little PHP code. Insert the following code at the beginning of the page:
This code initiates a session and then checks for the presence of the theme element in the _SESSION superarray. If the element is missing, it is created and assigned the value light. This value represents the saved theme stored in the user's browser. This way, user preferences are managed, improving interaction with the site and providing a personalized experience.
Let's change the way styles are included so that a previously saved file is loaded by default. This will improve site performance and ensure faster page loading for users. Properly configured stylesheets play a key role in optimizing a web resource and increasing its visibility in search engines. Make sure that the path to the stylesheet file is specified correctly to avoid design rendering errors. Optimizing stylesheets not only improves user experience but also contributes to better site ranking in search engines.
Now we need to implement a mechanism that will send a request to another PHP file when the theme changes. This file will be responsible for changing the value of the $_SESSION[‘theme’] variable. Let's start by creating a file we'll call themes.php.
This file processes incoming requests and, if the request contains the theme parameter with the values light or dark, changes the current display style. This allows users to switch between light and dark interface themes, improving the user experience and adapting the appearance to user preferences.
It is necessary to complete the development of the function that will be responsible for sending the request. This function will be called Save.
Now, when the page is refreshed, the theme will remain unchanged. This improvement allows users to save their selected settings and provides a more stable experience when interacting with the site. Thanks to this feature, users will be able to continue working without unnecessary switching and losing settings, which will significantly increase the convenience and satisfaction of using the resource.
To implement theme switching on the site, you can use PHP instead of JS cookies. This is because when using JavaScript, default styles are loaded first, then the script is executed, and only then is the user-saved theme applied. This approach can negatively impact the user experience. Using PHP allows for theme changes immediately upon page load, ensuring the user's chosen configuration is instantly displayed. This not only improves interaction with the site, but also promotes better perception of information.

