Introduction
Dark mode is a popular feature that enhances user experience by providing a darker color scheme, which is easier on the eyes, especially in low-light environments. This guide will walk you through the process of implementing and customizing dark mode in your web application using HTML, CSS, and JavaScript.
Step 1: HTML Setup
In your HTML file (index.html), include the necessary Font Awesome CSS and create a button to toggle dark mode:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="styles.css" rel="stylesheet">
<!-- Include Font Awesome CSS -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link href=" https://cdn.jsdelivr.net/npm/rens-ui@0.0.5/dist/css/styles.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com"></script>
<title>Tailwind CSS Dark Mode</title>
</head>
<body >
<button id="toggleDarkMode" class="btn btn-primary">
<i class="fas fa-sun"></i> <!-- Default icon, you can adjust this -->
</button>
<script src="scripts.js"></script>
</body>
</html>
Step 2: JavaScript Implementation
Create a JavaScript file (scripts.js) to handle the dark mode functionality:
const toggleDarkModeButton = document.getElementById('toggleDarkMode');
toggleDarkModeButton.addEventListener('click', () => {
const html = document.documentElement;
html.classList.toggle('dark');
// Save user preference to localStorage
if (html.classList.contains('dark')) {
localStorage.theme = 'dark';
} else {
localStorage.theme = 'light';
}
// Update data-theme attribute and button icon
updateDataThemeAttribute();
updateButtonIcon();
});
// Function to check system preference and set dark mode accordingly
function setDarkModePreference() {
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (prefersDarkMode) {
document.documentElement.classList.add('dark');
localStorage.theme = 'dark';
} else {
document.documentElement.classList.remove('dark');
localStorage.theme = 'light';
}
// Update data-theme attribute and button icon
updateDataThemeAttribute();
updateButtonIcon();
}
// Function to update the data-theme attribute
function updateDataThemeAttribute() {
const currentTheme = localStorage.theme || 'system';
document.documentElement.setAttribute('data-theme', currentTheme);
}
// Function to update the button icon based on the theme
function updateButtonIcon() {
const buttonIcon = document.documentElement.classList.contains('dark') ? 'fas fa-moon' : 'fas fa-sun';
toggleDarkModeButton.innerHTML = `<i class="${buttonIcon}"></i>`;
}
// Initialize dark mode preference, data-theme attribute, and button icon
setDarkModePreference();
Example Implementation
Conclusion
Adding dark mode to your web application can greatly improve user experience, especially in low-light situations. By following this guide, you can seamlessly integrate and customize dark mode functionality using HTML, CSS, and JavaScript. Remember to replace the icon class names with the appropriate ones from the Font Awesome library to match your design.