CSS Variable Theme Builder - Online Light & Dark Mode
Define a set of colors for light and dark themes. Get the CSS custom properties snippet. Toggle live.
UD5 Toolkit
Online tool demonstrating prefers-color-scheme respect — Light / Dark / Auto with system preference detection
This card demonstrates how content renders under the active color scheme. Backgrounds, text, and borders adapt automatically.
| # | Item | Status |
|---|---|---|
| 1 | Alpha | Active |
| 2 | Beta | Pending |
| 3 | Gamma | Idle |
// Detect system preference
const mq = window.matchMedia('(prefers-color-scheme: dark)');
function getSystemPreference() {
return mq.matches ? 'dark' : 'light';
}
// Apply theme
function applyTheme(mode) {
const resolved = mode === 'auto'
? getSystemPreference()
: mode;
document.documentElement
.setAttribute('data-bs-theme', resolved);
localStorage.setItem('theme-preference', mode);
}
// Listen for system changes
mq.addEventListener('change', () => {
const saved = localStorage.getItem('theme-preference');
if (saved === 'auto' || !saved) {
applyTheme('auto');
}
});
// Init on page load
const saved = localStorage.getItem('theme-preference') || 'auto';
applyTheme(saved);
/* CSS Media Query */
@media (prefers-color-scheme: dark) {
:root {
--bg: #1a1a2e;
--text: #e0e0e0;
}
}
@media (prefers-color-scheme: light) {
:root {
--bg: #ffffff;
--text: #1a1a1a;
}
}
/* HTML with Bootstrap 5.3 */
<html data-bs-theme="light">
<!-- or data-bs-theme="dark" -->
</html>
/* Toggle buttons */
<button onclick="applyTheme('light')">Light</button>
<button onclick="applyTheme('dark')">Dark</button>
<button onclick="applyTheme('auto')">Auto</button>
prefers-color-scheme?It's a CSS media feature that detects whether the user's operating system is set to light or dark mode. It returns light or dark, allowing websites to automatically match the user's system preference.
Auto mode reads the system's prefers-color-scheme value via JavaScript's matchMedia API. It applies the corresponding theme and listens for real-time changes — so if you toggle your OS setting, the website updates instantly.
Use localStorage to save the user's choice (light, dark, or auto). On page load, check localStorage first — if a value exists, apply it; otherwise fall back to system preference detection.
prefers-color-scheme?All modern browsers support it: Chrome 76+, Firefox 67+, Safari 12.1+, Edge 79+, Opera 62+, and Samsung Internet. Global support is over 96% as of 2024.
Bootstrap 5.3 uses the data-bs-theme attribute. Set it to dark or light on the <html> element. Bootstrap's CSS variables automatically adjust colors, backgrounds, and component styles accordingly.
Offer three options: Light, Dark, and Auto (system). Default to Auto to respect user preference. Persist the choice in localStorage. Use CSS variables or Bootstrap's data-bs-theme for seamless transitions.
Use window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', callback). The callback fires whenever the user changes their OS-level appearance setting.
Respecting system preferences improves accessibility, reduces eye strain for users who prefer dark mode, and provides a seamless experience. It shows users that you value their personal settings and comfort.
Yes! Use @media (prefers-color-scheme: dark) { ... } in your CSS. However, this doesn't allow users to manually override the setting. For a toggle, you'll need minimal JavaScript.
data-bs-theme and CSS variables?data-bs-theme is Bootstrap's built-in mechanism that toggles all Bootstrap CSS variables at once. Custom CSS variables require manual definition for each theme, offering more control but requiring more setup.
Define a set of colors for light and dark themes. Get the CSS custom properties snippet. Toggle live.
Set times to automatically switch light/dark theme on any website using a bookmarklet or live demo.
Place multiple grid items into the same cells to create overlapping layouts. Learn the technique visually. Copy code.
Control image‑orientation: from-image vs none. See how the browser interprets EXIF rotation. Fix portrait photos.
Experiment with scroll‑state container queries to style elements when they are stuck, snapped, or overflowed. Experimental.
Hover over tiles to see every CSS cursor value in action. Quick visual reference for choosing the right UI feedback.
Configure how your PWA launches: focus existing or create new. Test with the launch_handler manifest field.
Build a horizontal scroll‑snap container with configurable snap‑type and alignment. Perfect for image galleries.
Compare all CSS easing presets side by side on a bouncing ball. See which curve fits your UI animation.
Query the permission state of camera, microphone, geolocation, and more. See the response and learn the API.
Process audio faster than real‑time with OfflineAudioContext. Apply filters and export the result. Dev tool.
Toggle image‑rendering: auto, pixelated, crisp‑edges on a scaled image. Essential for pixel art display.
Drop files onto a zone and see a preview with name, size, and type. Copy the JavaScript pattern for your site.
Demonstrate how to add custom headers and POST‑like functionality to EventSource using a polyfill. Code and example.
Scroll a container and see how sticky elements behave. Adjust top, bottom, and scroll margins. Copy the code.
See how overflow: visible, hidden, scroll, and auto behave with real content. Clone to test with your text.
Try all object‑fit values (fill, contain, cover, scale‑down) on an image. Adjust object‑position. Copy the CSS.
See the difference between clone and slice on inline boxes that break across lines. Useful for multi‑line headings.
Use CSS masks and fixed backgrounds to create a unique parallax reveal effect. Copy the code. No JavaScript.
Render the classic Stanford Bunny with a basic WebGPU pipeline. Rotate and zoom. Check if your browser supports WebGPU.
See how scroll‑padding and scroll‑margin affect the position of elements when using anchor links or scroll‑snap. Visual.
See how align‑items: baseline works in grid to align different font sizes on the same baseline. Visual guide.
Test the experimental Translation API to translate text between languages directly in the browser, without cloud calls. Check support and copy the JavaScript starter.
Apply a convolution filter (blur, sharpen) using a Web Worker. See the UI stay responsive while processing. Learn multithreading in the browser.
Toggle scrollbar‑gutter: stable to reserve space for the scrollbar and avoid content jumps. Visual demo with two columns.
Acquire and release locks across tabs. Prevent race conditions in IndexedDB or localStorage. Visual queue and lock state.
Register a periodic background sync to fetch fresh data even when the tab is closed. Understand the API and limits.
Test the Fullscreen API: request fullscreen on a colored div, detect changes, and copy the JavaScript boilerplate.
See the View Transitions API in action. Cross‑fade and morph between two states. Copy the JavaScript starter code.
Design a light and dark theme by picking colors for backgrounds, text, and links. Get the CSS for both themes. Local.