PHP CSS JS Code For Dark & Light Switch Button Website Website

CSS Code – Dark & Light Mode Design:


/* =================================
   ASTRA DARK MODE SYSTEM
================================= */

/* LIGHT MODE DEFAULT */
:root {
    color-scheme: light;

    --dm-bg: #ffffff;
    --dm-surface: #f7f8fa;
    --dm-card: #ffffff;
    --dm-text: #252a34;
    --dm-heading: #171b24;
    --dm-muted: #666f7d;
    --dm-border: #e2e5e9;
    --dm-link: #1769aa;
    --dm-input: #ffffff;
}

/* DARK MODE COLORS */
html[data-theme="dark"] {
    color-scheme: dark;

    --dm-bg: #12151c;
    --dm-surface: #191e28;
    --dm-card: #1e2531;
    --dm-text: #e1e6ef;
    --dm-heading: #ffffff;
    --dm-muted: #aab4c3;
    --dm-border: #343d4b;
    --dm-link: #83bdff;
    --dm-input: #252d39;
}

/* GLOBAL BACKGROUND */
html[data-theme="dark"] body {
    background: var(--dm-bg);
    color: var(--dm-text);
}

/* ASTRA MAIN CONTAINERS */
html[data-theme="dark"] #page,
html[data-theme="dark"] #content,
html[data-theme="dark"] .site,
html[data-theme="dark"] .site-content,
html[data-theme="dark"] .site-main,
html[data-theme="dark"] #primary,
html[data-theme="dark"] #secondary,
html[data-theme="dark"] .ast-container,
html[data-theme="dark"] .ast-separate-container,
html[data-theme="dark"] .ast-plain-container,
html[data-theme="dark"] .ast-article-single,
html[data-theme="dark"] .ast-article-post {
    background-color: var(--dm-bg);
    color: var(--dm-text);
}

/* HEADER */
html[data-theme="dark"] .site-header,
html[data-theme="dark"] .ast-primary-header-bar,
html[data-theme="dark"] .ast-desktop-header-content,
html[data-theme="dark"] .ast-mobile-header-wrap,
html[data-theme="dark"] .ast-mobile-header-content,
html[data-theme="dark"] .ast-above-header-bar,
html[data-theme="dark"] .ast-below-header-bar {
    background-color: var(--dm-surface);
    color: var(--dm-text);
    border-color: var(--dm-border);
}

/* FOOTER */
html[data-theme="dark"] .site-footer,
html[data-theme="dark"] .ast-footer-overlay,
html[data-theme="dark"] .ast-small-footer {
    background-color: var(--dm-surface);
    color: var(--dm-text);
}

/* TEXT AND HEADINGS */
html[data-theme="dark"] body,
html[data-theme="dark"] p,
html[data-theme="dark"] li,
html[data-theme="dark"] label,
html[data-theme="dark"] h1,
html[data-theme="dark"] h2,
html[data-theme="dark"] h3,
html[data-theme="dark"] h4,
html[data-theme="dark"] h5,
html[data-theme="dark"] h6,
html[data-theme="dark"] .entry-title,
html[data-theme="dark"] .entry-content,
html[data-theme="dark"] .widget-title {
    color: var(--dm-text);
}

html[data-theme="dark"] h1,
html[data-theme="dark"] h2,
html[data-theme="dark"] h3,
html[data-theme="dark"] h4,
html[data-theme="dark"] h5,
html[data-theme="dark"] h6 {
    color: var(--dm-heading);
}

/* LINKS */
html[data-theme="dark"] a {
    color: var(--dm-link);
}

/* CARDS AND WIDGETS */
html[data-theme="dark"] .widget,
html[data-theme="dark"] .wp-block-group,
html[data-theme="dark"] .wp-block-column,
html[data-theme="dark"] .ast-related-posts,
html[data-theme="dark"] .comments-area,
html[data-theme="dark"] .ast-author-box {
    background-color: var(--dm-card);
    border-color: var(--dm-border);
}

/* NAVIGATION */
html[data-theme="dark"] .main-header-menu,
html[data-theme="dark"] .main-header-menu .menu-link,
html[data-theme="dark"] .ast-builder-menu .menu-item > .menu-link,
html[data-theme="dark"] .ast-mobile-popup-content {
    color: var(--dm-text);
}

/* FORMS */
html[data-theme="dark"] input,
html[data-theme="dark"] textarea,
html[data-theme="dark"] select {
    background-color: var(--dm-input);
    color: var(--dm-text);
    border: 1px solid var(--dm-border);
}

html[data-theme="dark"] input::placeholder,
html[data-theme="dark"] textarea::placeholder {
    color: var(--dm-muted);
}

/* TABLES */
html[data-theme="dark"] table,
html[data-theme="dark"] th,
html[data-theme="dark"] td {
    background-color: var(--dm-card);
    color: var(--dm-text);
    border-color: var(--dm-border);
}

/* BUTTONS */
html[data-theme="dark"] button,
html[data-theme="dark"] .wp-block-button__link {
    border-color: var(--dm-border);
}

/* PROFESSIONAL TOGGLE BUTTON */
.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 9px;

    padding: 9px 15px;
    min-height: 40px;

    border: 1px solid #d8dce3;
    border-radius: 30px;

    background: #ffffff;
    color: #242a35;

    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;

    cursor: pointer;
    white-space: nowrap;

    transition: all 0.25s ease;
}

.theme-toggle:hover {
    background: #f0f3f7;
    border-color: #aeb7c5;
    transform: translateY(-1px);
}

.theme-toggle-icon {
    font-size: 19px;
    line-height: 1;
}

html[data-theme="dark"] .theme-toggle {
    background: #283243;
    color: #ffffff;
    border-color: #465268;
}

html[data-theme="dark"] .theme-toggle:hover {
    background: #354258;
}

.theme-toggle:focus-visible {
    outline: 2px solid var(--dm-link);
    outline-offset: 3px;
}

/* SMOOTH TRANSITION */
html[data-theme="dark"] body,
html[data-theme="dark"] .site,
html[data-theme="dark"] .site-header,
html[data-theme="dark"] .site-footer {
    transition: background-color 0.25s ease, color 0.25s ease;
}

/* MOBILE */
@media (max-width: 544px) {
    .theme-toggle {
        padding: 8px 11px;
        font-size: 13px;
        min-height: 36px;
    }

    .theme-toggle-icon {
        font-size: 17px;
    }
}

/* ACCESSIBILITY */
@media (prefers-reduced-motion: reduce) {
    .theme-toggle,
    body,
    .site {
        transition: none !important;
    }
}


/* DARK MODE BUTTON - FIXED ON SCREEN */

.theme-toggle {
    position: fixed !important;

    bottom: 25px !important;
    right: 25px !important;
    top: auto !important;

    z-index: 999999 !important;

    display: inline-flex;
    align-items: center;
    justify-content: center;
    
    padding: 12px 20px;
    border-radius: 30px;

    box-shadow: 0 4px 15px rgba(0,0,0,0.25);
}

/* MOBILE RESPONSIVE */

@media (max-width: 768px) {
    .theme-toggle {
        bottom: 18px !important;
        right: 15px !important;
        padding: 10px 15px;
        font-size: 13px;
    }
}

JavaScript Code – Mode Change Function

document.addEventListener(‘DOMContentLoaded’, function () {

var button = document.getElementById('theme-toggle');

if (!button) {
    return;
}

var icon = button.querySelector('.theme-toggle-icon');
var text = button.querySelector('.theme-toggle-text');

function applyTheme(theme) {

    if (theme !== 'dark') {
        theme = 'light';
    }

    document.documentElement.setAttribute('data-theme', theme);

    if (theme === 'dark') {

        if (icon) {
            icon.textContent = '☀';
        }

        if (text) {
            text.textContent = 'Light Mode';
        }

        button.setAttribute('aria-pressed', 'true');

    } else {

        if (icon) {
            icon.textContent = '☾';
        }

        if (text) {
            text.textContent = 'Dark Mode';
        }

        button.setAttribute('aria-pressed', 'false');
    }
}

var savedTheme = 'light';

try {
    var storedTheme = localStorage.getItem('website-theme');

    if (storedTheme === 'dark') {
        savedTheme = 'dark';
    }
} catch (error) {}

applyTheme(savedTheme);

button.addEventListener('click', function () {

    var currentTheme =
        document.documentElement.getAttribute('data-theme');

    var newTheme = 'dark';

    if (currentTheme === 'dark') {
        newTheme = 'light';
    }

    applyTheme(newTheme);

    try {
        localStorage.setItem('website-theme', newTheme);
    } catch (error) {}

});

});

PHP Code – functions.php


/**
 * Astra Child Theme - Dark & Light Mode
 */

/* 1. Load Dark Mode CSS and JavaScript */
add_action('wp_enqueue_scripts', 'pramod_dark_mode_assets');

function pramod_dark_mode_assets() {

    $css_path = get_stylesheet_directory() . '/assets/css/dark-mode.css';
    $js_path  = get_stylesheet_directory() . '/assets/js/dark-mode.js';

    wp_enqueue_style(
        'pramod-dark-mode',
        get_stylesheet_directory_uri() . '/assets/css/dark-mode.css',
        array(),
        file_exists($css_path) ? filemtime($css_path) : '1.0'
    );

    wp_enqueue_script(
        'pramod-dark-mode',
        get_stylesheet_directory_uri() . '/assets/js/dark-mode.js',
        array(),
        file_exists($js_path) ? filemtime($js_path) : '1.0',
        true
    );
}

/* 2. Add Dark Mode Button Shortcode */
add_shortcode('dark_mode_toggle', 'pramod_dark_mode_button');

function pramod_dark_mode_button() {

    return '
    <button
        id="theme-toggle"
        class="theme-toggle"
        type="button"
        aria-label="Switch to dark mode"
        aria-pressed="false">

        <span class="theme-toggle-icon" aria-hidden="true">☾</span>
        <span class="theme-toggle-text">Dark Mode</span>

    </button>';
}

/* 3. Apply saved theme before page rendering */
add_action('wp_head', 'pramod_dark_mode_early_script', 1);

function pramod_dark_mode_early_script() {
    ?>
    <script>
    (function () {
        try {
            var theme = localStorage.getItem('website-theme') || 'light';

            if (theme !== 'dark' && theme !== 'light') {
                theme = 'light';
            }

            document.documentElement.setAttribute('data-theme', theme);
        } catch (e) {
            document.documentElement.setAttribute('data-theme', 'light');
        }
    })();
    </script>
    <?php
}

Short Code:

[dark_mode_toggle]

Leave a Comment

Prove your humanity: 3   +   1   =