A text-formatting toolbar of pressable toggles: bold, italic, underline and strike. Each key sinks when pressed, pops into its filled on state and combines with the others. Four hidden checkboxes.
Also known as: css toggle button, button group css, aria pressed button.
<div class="btn-toggle-group" role="group" aria-label="Text formatting"> <label class="btn-toggle-group-key"><input class="btn-toggle-group-input" type="checkbox" aria-label="Bold" checked><b>B</b></label> <label class="btn-toggle-group-key"><input class="btn-toggle-group-input" type="checkbox" aria-label="Italic"><em>I</em></label> <label class="btn-toggle-group-key"><input class="btn-toggle-group-input" type="checkbox" aria-label="Underline"><u>U</u></label> <label class="btn-toggle-group-key"><input class="btn-toggle-group-input" type="checkbox" aria-label="Strikethrough"><s>S</s></label> </div>
.btn-toggle-group {
--c: var(--btn-color, #3b82f6);
display: inline-flex;
gap: 0.25em;
padding: 0.3em;
border-radius: 0.8em;
background: color-mix(in srgb, var(--c) 7%, transparent);
box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--c) 20%, transparent);
font: 600 var(--btn-size, 18px)/1 Georgia, "Times New Roman", serif;
}
.btn-toggle-group-key {
position: relative;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.btn-toggle-group-input {
position: absolute;
inset: 0;
z-index: 1;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
}
.btn-toggle-group-key > :not(input) {
display: grid;
place-items: center;
width: 2.4em;
height: 2.4em;
border-radius: 0.55em;
color: color-mix(in srgb, var(--c) 40%, currentColor);
font-size: 1.1em;
text-underline-offset: 0.15em;
transition: background-color 0.25s ease, color 0.25s ease, box-shadow 0.25s ease, transform 0.3s cubic-bezier(0.3, 1.6, 0.5, 1);
}
.btn-toggle-group-key b { font-weight: 800; }
.btn-toggle-group-key em { font-style: italic; }
.btn-toggle-group-key:active > :not(input) {
transform: scale(0.86);
transition-duration: 0.1s;
}
.btn-toggle-group-input:focus-visible + * {
outline: 2px solid var(--c);
outline-offset: 0.1em;
}
.btn-toggle-group-input:checked + * {
background-color: var(--c);
color: #fff;
box-shadow: inset 0 0.12em 0.25em rgba(0, 0, 0, 0.25), 0 0.2em 0.5em -0.2em var(--c);
animation: btn-toggle-group-pop 0.35s cubic-bezier(0.3, 1.6, 0.5, 1);
}
@keyframes btn-toggle-group-pop {
0% { transform: scale(0.8); }
100% { transform: scale(1); }
}
@media (prefers-reduced-motion: reduce) {
.btn-toggle-group, .btn-toggle-group *, .btn-toggle-group::before, .btn-toggle-group::after, .btn-toggle-group *::before, .btn-toggle-group *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}The same effect using Tailwind utility classes. No custom CSS or config needed.
<div class="inline-flex gap-1 rounded-xl bg-blue-500/5 p-1 font-serif text-lg ring-2 ring-inset ring-blue-500/20" role="group" aria-label="Text formatting"> <label class="relative cursor-pointer"><input class="peer absolute inset-0 z-10 cursor-pointer opacity-0" type="checkbox" aria-label="Bold" checked><b class="grid h-10 w-10 place-items-center rounded-lg font-extrabold text-slate-500 transition duration-300 ease-[cubic-bezier(.3,1.6,.5,1)] peer-checked:bg-blue-500 peer-checked:text-white peer-checked:shadow-inner peer-active:scale-90 peer-active:duration-100 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2">B</b></label> <label class="relative cursor-pointer"><input class="peer absolute inset-0 z-10 cursor-pointer opacity-0" type="checkbox" aria-label="Italic"><em class="grid h-10 w-10 place-items-center rounded-lg italic text-slate-500 transition duration-300 ease-[cubic-bezier(.3,1.6,.5,1)] peer-checked:bg-blue-500 peer-checked:text-white peer-checked:shadow-inner peer-active:scale-90 peer-active:duration-100 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2">I</em></label> <label class="relative cursor-pointer"><input class="peer absolute inset-0 z-10 cursor-pointer opacity-0" type="checkbox" aria-label="Underline"><u class="grid h-10 w-10 place-items-center rounded-lg text-slate-500 transition duration-300 ease-[cubic-bezier(.3,1.6,.5,1)] peer-checked:bg-blue-500 peer-checked:text-white peer-checked:shadow-inner peer-active:scale-90 peer-active:duration-100 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2">U</u></label> <label class="relative cursor-pointer"><input class="peer absolute inset-0 z-10 cursor-pointer opacity-0" type="checkbox" aria-label="Strikethrough"><s class="grid h-10 w-10 place-items-center rounded-lg text-slate-500 transition duration-300 ease-[cubic-bezier(.3,1.6,.5,1)] peer-checked:bg-blue-500 peer-checked:text-white peer-checked:shadow-inner peer-active:scale-90 peer-active:duration-100 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2">S</s></label> </div>
--btn-color on the element or a parent.
Currently previewing #3b82f6.--btn-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.