Source Code
<div class="bsuser-stage" id="bsuserStage">
<div class="dropdown">
<button class="btn d-flex align-items-center gap-2 bsuser-trigger" data-bs-toggle="dropdown" aria-expanded="false">
<span class="bsuser-avatar">DR</span>
<span class="d-none d-sm-inline small fw-semibold">Dana Reyes</span>
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>
</button>
<ul class="dropdown-menu dropdown-menu-end bsuser-menu">
<li class="px-3 py-2 border-bottom">
<div class="small fw-semibold">Dana Reyes</div>
<div class="small text-muted">dana@fenwick.com</div>
</li>
<li><a class="dropdown-item" href="javascript:void(0)">Profile</a></li>
<li><a class="dropdown-item" href="javascript:void(0)">Billing</a></li>
<li><a class="dropdown-item" href="javascript:void(0)">Settings</a></li>
<li>
<div class="dropdown-item d-flex justify-content-between align-items-center">
<span>Dark mode</span>
<div class="form-check form-switch mb-0"><input class="form-check-input" type="checkbox" id="bsuserTheme"></div>
</div>
</li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item text-danger" href="javascript:void(0)">Sign out</a></li>
</ul>
</div>
</div>.bsuser-stage { min-height: 100vh; padding: 24px; background: #f6f7f9; transition: background .2s, color .2s; }
.bsuser-stage.bsuser-dark { background: #111827; color: #e5e7eb; }
.bsuser-trigger { border: 1px solid #e5e7eb; background: #fff; }
.bsuser-avatar { width: 26px; height: 26px; border-radius: 50%; background: #111827; color: #fff; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; }
.bsuser-menu { width: 220px; }
.bsuser-menu .form-switch .form-check-input { cursor: pointer; }const themeToggle = document.getElementById('bsuserTheme');
const stage = document.getElementById('bsuserStage');
// Clicking the switch itself would normally also bubble up and close the
// Bootstrap dropdown (any click inside a dropdown-item closes it by
// default) — stop that specific propagation so toggling doesn't dismiss
// the menu, matching how a settings toggle should behave.
themeToggle.addEventListener('click', e => e.stopPropagation());
themeToggle.addEventListener('change', () => {
stage.classList.toggle('bsuser-dark', themeToggle.checked);
});Bootstrap User Menu Dropdown with Theme Switch — Free Snippet
Bootstrap User Menu Dropdown with Theme Switch · Navigation · Plain HTML, CSS & JS · Live preview
What's included
Features
About this UI Snippet
Bootstrap User Menu Dropdown with Theme Switch — HTML, CSS & JavaScript
An avatar dropdown menu is usually a plain list of links — this one embeds a real, working control inside it: a dark-mode form-switch toggle that flips a class on the page and changes its colors immediately, without the dropdown closing when you click it. That last detail matters and takes one specific line: Bootstrap's Dropdown closes automatically on any click inside a .dropdown-item, which is the right default for a navigation link but wrong for an interactive control like a toggle switch. Calling e.stopPropagation() on the switch's own click event stops that click from bubbling up to whatever triggers the auto-close, while the toggle's separate change listener still fires normally and applies the theme.
Built entirely on real Bootstrap 5.3 — the Dropdown component for the menu itself, and the real form-switch component for the toggle — with a user info header, standard account links, and a sign-out action rounding out the menu.
Build with AI
Build, Understand, Optimize, and Extend It With AI
Hand this snippet's HTML, CSS, and JS to an AI coding assistant like Claude and ask it to persist the dark-mode choice to localStorage so it's remembered on the next visit, or to add a real user avatar image with a fallback to initials if the image fails to load. It's also a good exercise to ask the assistant to add a language selector as a second interactive control inside the same menu, following the same stopPropagation pattern.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a Bootstrap 5.3 user account dropdown menu with an inline dark-mode toggle, using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble Bootstrap.
Requirements:
- An avatar button (initials-based, no external image required) that opens a real Bootstrap Dropdown menu (data-bs-toggle="dropdown") showing the user's name and email, at least three navigation links (e.g. Profile, Billing, Settings), a sign-out link, and a real Bootstrap form-switch toggle labeled "Dark mode" embedded as one of the menu items.
- Toggling the dark-mode switch must genuinely change the page's visual theme (background and text colors) immediately via a class toggle, and critically must NOT cause the dropdown menu to close — while clicking any of the other menu links must close the dropdown normally, matching Bootstrap's default behavior.
- Solve the close-on-toggle problem using the correct approach: stopping the toggle's own click event from propagating, rather than disabling Bootstrap's dropdown auto-close globally (which would break the normal links).
- The trigger button must be responsive, hiding the visible name text (but keeping the avatar) below a small screen breakpoint.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.
Step by step
How to Use
- 1Load the snippetClick the snippet in the sidebar Library tab. The preview loads a light page with an avatar button top-right.
- 2Open the menuClick the avatar — Bootstrap's real Dropdown opens showing the user's name, email, links, and a dark-mode switch.
- 3Click the dark mode switchThe whole page background and text flip to dark — and the dropdown stays open.
- 4Click "Profile" or another linkA normal dropdown-item click closes the menu as expected, unlike the toggle.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
Yes — the avatar button and menu use Bootstrap 5.3's actual Dropdown component (data-bs-toggle="dropdown"), and the toggle is Bootstrap's real form-switch component, both loaded from the genuine CDN.
By default, Bootstrap closes a dropdown on any click inside a .dropdown-item. A click listener on the switch itself calls e.stopPropagation(), preventing that click from triggering the auto-close, while its separate change listener still applies the theme normally.
It genuinely toggles a class on the page container that switches the background and text colors via CSS, applied immediately when the switch changes — not a decorative control.
Only the toggle switch has the stopPropagation() fix applied; every other menu item is a normal dropdown-item link, so it keeps Bootstrap's default close-on-click behavior, which is the correct behavior for a navigation link.
The visible name text next to the avatar hides below Bootstrap's sm breakpoint (d-none d-sm-inline), leaving just the avatar circle as the trigger, saving horizontal space in a mobile header.