CSS Format Toggle Buttons
A text editor toolbar of bold, italic, underline and strike toggles that stay pressed when on.
<fieldset class="format-toggles">
<legend class="format-toggles-legend">Text style</legend>
<label><input type="checkbox" aria-label="Bold" checked><b aria-hidden="true">B</b></label>
<label><input type="checkbox" aria-label="Italic"><i aria-hidden="true">I</i></label>
<label><input type="checkbox" aria-label="Underline"><u aria-hidden="true">U</u></label>
<label><input type="checkbox" aria-label="Strikethrough"><s aria-hidden="true">S</s></label>
</fieldset>
.format-toggles {
display: inline-flex;
gap: 2px;
margin: 0;
padding: 4px;
border: 1px solid #e2e8f0;
border-radius: 12px;
background: #fff;
box-shadow: 0 1px 2px #0f172a14;
}
.format-toggles-legend {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
.format-toggles label {
position: relative;
display: grid;
place-items: center;
width: 38px;
height: 38px;
border-radius: 8px;
color: #475569;
font: 17px/1 Georgia, "Times New Roman", serif;
cursor: pointer;
transition: background-color 0.15s ease, color 0.15s ease;
}
.format-toggles input {
position: absolute;
inset: 0;
margin: 0;
opacity: 0;
cursor: pointer;
}
.format-toggles label:hover {
background: #f1f5f9;
color: #0f172a;
}
.format-toggles label:has(input:checked) {
background: #e0e7ff;
color: #3730a3;
}
.format-toggles label:has(input:focus-visible) {
outline: 2px solid #4f46e5;
outline-offset: 1px;
}
About this CSS Format Toggle Buttons
This CSS Format Toggle Buttons is 55 lines of pure CSS (1.0 KB) with no JavaScript and no dependencies. It uses transitions, box shadows, clip paths and CSS Grid. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- transitions
- box shadows
- clip paths
- CSS Grid
- Flexbox
- the :has() selectorChrome 105+, Safari 15.4+, Firefox 121+.
- the :checked state (no JavaScript)
- keyboard focus styles
Customize it
The colors in this snippet. Search and replace them to match your brand:
#e2e8f0#fff#0f172a14#475569#f1f5f9#0f172a#e0e7ff#3730a3#4f46e5
How to use this format toggle buttons
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.format-togglesclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.