|
A simple light mode/dark mode switch for PHP websites. category 'KB', created 14-Nov-2026, version V1.0, by Luc Pattyn |
|
License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author. Disclaimer: This work is provided |
A simple way to achieve dark mode in a PHP-based website. No fancy stuff, just a single checkbox that toggles between two coloring schemes, a light one and a dark one.
Everything is based on minimal code snippets in PHP, JavaScript and CSS.
For every color that needs changing when switching themes, we introduce a variable in :root, giving it a default value that corresponds to the "light mode". Later we refer to those variables using the var() syntax, as demonstrated here:
/* these variable colors are controlled by JavaScript code, driven by the light/dark switch.
// use them within this file with the var() function.
*/
:root {
--main-bg-color: white;
--main-fg-color: black;
--main-fg-blue: blue; /* gets lighter in dark mode */
}
body {
-webkit-print-color-adjust:exact; /* get background colors printed on Chrome */
background-color: var(--main-bg-color);
color: var(--main-fg-color);
}
a:link {
color: var(--main-fg-blue);
}
We rely on a simple checkbox with ID = "cb_dark" to select one of two color sets; typically the checkbox could be part of the page header of every page.
function setLightDarkColors() {
var cb_dark=document.getElementById("cb_dark");
var dark=cb_dark!=null && cb_dark.checked;
var root=document.querySelector(':root');
root.style.setProperty('--main-fg-color', dark?'#EEE':'#222');
root.style.setProperty('--main-bg-color', dark?'#222':'#EEE');
root.style.setProperty('--main-fg-blue', dark?'#77F':'#03F');
}
And we post the checkbox changes so later pages may start with the same selection:
function installDarkHandler() {
const checkbox = document.getElementById('cb_dark');
if (checkbox!=null) {
checkbox.addEventListener('change', function() {
const isChecked = this.checked;
fetch('setDarkMode.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
isChecked: isChecked
}),
})
catch(error => {
// catch errors to report network failures
console.error('Error:', error);
});
setLightDarkColors();
});
}
setLightDarkColors();
}
The page "'setDarkMode.php'" that registers the checkbox toggles; the state might get persisted in a database.
<?php
require_once("includes/base1.php");
$data = file_get_contents('php://input');
$data_array = json_decode($data, true);
if ($data_array) {
$isChecked = $data_array['isChecked'];
if ($isChecked!="1") $isChecked="0";
storeDatabaseString("DarkMode1", $isChecked);
} else {
log("Invalid data received.");
}
?>
And finally, a PHP example generating the checkbox; the form action is optional, as the JavaScript code already handles it.
<?php
$chk=null;
if (getDatabaseString("DarkMode1")=="1") $chk="checked";
?>
<form action='setDarkMode.php' method='post'>Dark
<input type='checkbox' id='cb_dark' name='dark' <?php echo $chk;?> />
</form>
This is the minimalistic approach offering the user a choice between two sets of colors. They are fixed, there is no provision to modify them dynamically.
Perceler |
Copyright © 2012, Luc Pattyn |
Last Modified 03-Feb-2026 |