Multiple choice answers with instant feedback. A wrong pick turns red and shakes, the right answer is revealed in green with a tick, and the rest fade back. The answer lives in one class.
Also known as: quiz radio buttons css, multiple choice css, correct wrong answer css.
Which is cheapest to animate?
<div class="rad-quiz-answers" role="radiogroup" aria-label="Which property is cheapest to animate?"> <p class="rad-quiz-answers-q">Which is cheapest to animate?</p> <label><input type="radio" name="rad-quiz-answers" value="a"><span class="rad-quiz-answers-key" aria-hidden="true">A</span>width</label> <label class="rad-quiz-answers-right"><input type="radio" name="rad-quiz-answers" value="b"><span class="rad-quiz-answers-key" aria-hidden="true">B</span>transform</label> <label><input type="radio" name="rad-quiz-answers" value="c"><span class="rad-quiz-answers-key" aria-hidden="true">C</span>top</label> </div>
.rad-quiz-answers {
--c: var(--rad-color, #3b82f6);
font-size: var(--rad-size, 18px);
--ok: #16a34a;
--bad: #e11d48;
display: grid;
gap: 0.4em;
width: 15em;
font-family: system-ui, sans-serif;
line-height: 1.2;
}
.rad-quiz-answers-q {
margin: 0 0 0.2em;
font-weight: 700;
}
.rad-quiz-answers label {
position: relative;
display: flex;
align-items: center;
gap: 0.6em;
padding: 0.35em 0.55em;
border: 1px solid color-mix(in srgb, currentColor 16%, transparent);
border-radius: 0.6em;
font: 500 0.9em/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
cursor: pointer;
transition: border-color 0.25s ease, background-color 0.25s ease, opacity 0.3s ease;
}
.rad-quiz-answers label:hover {
border-color: color-mix(in srgb, var(--c) 60%, transparent);
}
.rad-quiz-answers input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.rad-quiz-answers-key {
position: relative;
display: grid;
place-items: center;
width: 1.6em;
height: 1.6em;
border-radius: 0.45em;
background: color-mix(in srgb, var(--c) 14%, transparent);
color: var(--c);
font: 700 0.85em/1 system-ui, sans-serif;
}
.rad-quiz-answers-key::after {
content: "";
position: absolute;
inset: 0;
display: grid;
place-items: center;
border-radius: inherit;
color: #fff;
transform: scale(0);
transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-quiz-answers:has(:checked) .rad-quiz-answers-right {
border-color: var(--ok);
background: color-mix(in srgb, var(--ok) 12%, transparent);
}
.rad-quiz-answers:has(:checked) .rad-quiz-answers-right .rad-quiz-answers-key::after {
content: "\2713";
background: var(--ok);
transform: scale(1);
}
.rad-quiz-answers .rad-quiz-answers-right:has(:checked) {
animation: rad-quiz-answers-pop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-quiz-answers label:not(.rad-quiz-answers-right):has(:checked) {
border-color: var(--bad);
background: color-mix(in srgb, var(--bad) 12%, transparent);
animation: rad-quiz-answers-shake 0.45s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}
.rad-quiz-answers label:not(.rad-quiz-answers-right):has(:checked) .rad-quiz-answers-key::after {
content: "\2715";
background: var(--bad);
transform: scale(1);
}
.rad-quiz-answers:has(:checked) label:not(.rad-quiz-answers-right):not(:has(:checked)) {
opacity: 0.45;
}
.rad-quiz-answers input:focus-visible + .rad-quiz-answers-key {
outline: 0.14em solid var(--c);
outline-offset: 0.12em;
}
@keyframes rad-quiz-answers-shake {
0%, 100% { transform: translateX(0); }
20% { transform: translateX(-0.35em); }
40% { transform: translateX(0.3em); }
60% { transform: translateX(-0.18em); }
80% { transform: translateX(0.08em); }
}
@keyframes rad-quiz-answers-pop {
0%, 100% { transform: scale(1); }
45% { transform: scale(1.04); }
}
@media (prefers-reduced-motion: reduce) {
.rad-quiz-answers, .rad-quiz-answers *, .rad-quiz-answers::before, .rad-quiz-answers::after, .rad-quiz-answers *::before, .rad-quiz-answers *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--rad-color on the element or a parent.
Currently previewing #3b82f6.--rad-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 18px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.