Radio Theme Swap
A copy-paste theme toggle component in pure HTML & CSS. Zero dependencies, framework-agnostic, MIT-licensed.
Theme ToggleHTMLCSSany framework
Preview card
Copy into your project
HTML
<!-- Radio Theme Swap — pure CSS via :has(); no JS required.
:has() reads the checked radio and swaps the preview's color-scheme. -->
<div class="nuda-tt2-hasswap">
<fieldset class="nuda-tt2-hasswap__fieldset">
<legend class="nuda-tt2-hasswap__legend">Theme</legend>
<label class="nuda-tt2-hasswap__opt" for="tt2-hasswap-light">
<input class="nuda-tt2-hasswap__radio" type="radio" name="tt2-hasswap-theme" id="tt2-hasswap-light" checked />
Light
</label>
<label class="nuda-tt2-hasswap__opt" for="tt2-hasswap-dark">
<input class="nuda-tt2-hasswap__radio" type="radio" name="tt2-hasswap-theme" id="tt2-hasswap-dark" />
Dark
</label>
</fieldset>
<div class="nuda-tt2-hasswap__preview">
<span class="nuda-tt2-hasswap__dot" aria-hidden="true"></span>
Preview card
</div>
</div>CSS
.nuda-tt2-hasswap {
display: inline-flex;
flex-direction: column;
gap: 10px;
padding: 10px;
background: rgba(255,255,255,.04);
border: 1px solid rgba(255,255,255,.08);
border-radius: 10px;
color: #cfcfcf;
}
.nuda-tt2-hasswap__fieldset {
display: flex;
gap: 10px;
border: none;
margin: 0;
padding: 0;
}
.nuda-tt2-hasswap__legend {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
}
.nuda-tt2-hasswap__opt {
display: flex;
align-items: center;
gap: 6px;
font: 600 11px ui-sans-serif,system-ui;
cursor: pointer;
min-height: 44px;
}
.nuda-tt2-hasswap__radio {
width: 16px;
height: 16px;
accent-color: #e4ff54;
cursor: pointer;
}
.nuda-tt2-hasswap__radio:focus-visible {
outline: 2px solid #e4ff54;
outline-offset: 2px;
}
.nuda-tt2-hasswap__preview {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 12px;
border-radius: 8px;
background: #fdf6e3;
color: #3a3a1a;
color-scheme: light;
transition: background .4s ease,color .4s ease;
font: 600 11px ui-sans-serif,system-ui;
}
.nuda-tt2-hasswap__dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #f59e0b;
transition: background .4s ease,box-shadow .4s ease;
}
.nuda-tt2-hasswap:has(#tt2-hasswap-dark:checked) .nuda-tt2-hasswap__preview {
background: #141418;
color: #e8e8ea;
color-scheme: dark;
}
.nuda-tt2-hasswap:has(#tt2-hasswap-dark:checked) .nuda-tt2-hasswap__dot {
background: #e4ff54;
box-shadow: 0 0 8px rgba(228,255,84,.5);
}
@media (prefers-reduced-motion:reduce) {
.nuda-tt2-hasswap__preview,.nuda-tt2-hasswap__dot {
transition: none;
}
}
How to use Radio Theme Swap
Paste the HTML where you need it and the CSS into a global stylesheet (or a <style> tag). Every class is prefixed nuda- so it never collides with Tailwind or your own styles. Tweak the CSS custom properties to match your design system.
Works in React, Vue, Svelte, Astro, Next.js, Nuxt, Laravel Blade, Django, Rails — or a single .html file. No npm install, no build step.