Skip to content

Instantly share code, notes, and snippets.

@iiic
Last active May 29, 2026 08:41
Show Gist options
  • Select an option

  • Save iiic/e427875bce6ff663157350520a88352f to your computer and use it in GitHub Desktop.

Select an option

Save iiic/e427875bce6ff663157350520a88352f to your computer and use it in GitHub Desktop.
switch vzniklý modifikací input type checkbox
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Switch element example</title>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="icon" type="image/x-icon" href="./favicon.ico">
<link rel="stylesheet" href="./mvp.css" data-title="světlý styl" fetchpriority="high" crossorigin="anonymous">
<style>
:root {
--border-radius: 5px;
--color-accent: #118bee15;
--color-link: #118bee;
--color-text: #000;
--color-success: green;
}
label.switch {
display: block;
padding: 4px 4px 6px 6px;
border: 0 solid var(--color-link);
border-radius: var(--border-radius);
width: min-content;
white-space: nowrap;
cursor: pointer;
user-select: none;
&>:first-child {
display: inline-block;
padding-right: 1em;
}
&:hover,
&:has([role=switch]:focus) {
padding: 2px 2px 4px 4px;
border-width: 2px;
outline: none;
background-color: var(--color-accent);
}
.visual {
position: relative;
top: 2px;
display: inline-block;
border: 2px solid var(--color-text);
width: 2.5em;
height: 1.25em;
border-radius: 2em;
vertical-align: sub;
&::before {
content: "";
position: absolute;
top: 1px;
left: 2px;
display: inline-block;
border: 2px solid var(--color-text);
border-radius: 1em;
width: 0.88em;
height: 0.88em;
background: var(--color-text);
opacity: 0.6;
transition: left 0.2s ease, background-color 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}
}
.state {
display: inline-grid;
grid-template-areas: "overlay";
&>.boolean {
grid-area: overlay;
}
}
}
[role=switch] {
opacity: 0;
position: absolute;
&:not(:checked)~.state .on {
opacity: 0;
}
&:checked~.state .off {
opacity: 0;
}
&:checked~.visual::before {
left: 1.25em;
border-color: var(--color-success);
background: var(--color-success);
opacity: 1;
}
}
</style>
</head>
<body>
<fieldset>
<legend>Accessibility Preferences</legend>
<label for="id-switch-1" class="switch">
<strong>Reduced motion</strong>
<input id="id-switch-1" type="checkbox" role="switch">
<span class="visual"></span>
<span class="state">
<small class="boolean on" aria-hidden="true">aktivní</small>
<small class="boolean off" aria-hidden="true">vypnuto</small>
</span>
</label>
<br>
<a href="https://www.w3.org/WAI/ARIA/apg/patterns/switch/examples/switch-checkbox/">https://www.w3.org/WAI/ARIA/apg/patterns/switch/examples/switch-checkbox/</a>
jen jsem originál upravil, aby nebyl potřeba javascript
</fieldset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment