Home › Developer Tools › CSS Toggle Switch Generator

CSS Toggle Switch Generator

Design an on/off switch without JavaScript: choose the size, colors, shape, knob and animation, click the live preview to try it, and copy clean HTML and CSS built on a real checkbox, so it works with forms, keyboards and screen readers.

Last updated:

Presets

Click, tap or Tab + Space to try the switches.

Size & shape

Animation

Colors

HTML

    
CSS

    
Advertisement
Advertisement

How to use the CSS Toggle Switch Generator

  1. Pick a preset such as iOS, Square or Labeled, or start from the default.
  2. Adjust the size and shape: width, height, knob padding and roundness.
  3. Choose colors for the off and on track, the knob and the inside text.
  4. Tune the animation: duration, easing and an optional stretch while pressing.
  5. Click the preview switches to test them, then copy the HTML and CSS into your page.

How the pure CSS toggle switch works

The switch is a normal <input type="checkbox"> wrapped in a <label>. Clicking anywhere on the label toggles the checkbox, and CSS reacts to its state:

PartRole
input[type=checkbox]Holds the on/off state; visually hidden but still focusable and submitted with forms
role="switch"Tells screen readers to announce "switch, on/off" instead of "checkbox, checked"
.switch-trackThe colored rail; input:checked + .switch-track changes its color
.switch-track::beforeThe knob; moved with transform: translateX(), which animates smoothly on the GPU
.switch-track::afterOptional ON/OFF text inside the track
:focus-visibleDraws a focus ring for keyboard users only

The knob travel distance is track width − knob size − 2 × padding, and the knob size is track height − 2 × padding. The generator computes these for you so the knob always lands exactly at the edge.

Accessibility checklist

  • Keep a visible text label, or at least an aria-label on the input when the label is hidden.
  • Make sure the on and off track colors differ enough from the page background (WCAG 1.4.11 asks for 3:1 contrast for UI components) and do not rely on color alone; the knob position also shows the state.
  • Toggle switches should act immediately. If a change needs saving, a checkbox is the clearer control.
  • The CSS includes prefers-reduced-motion, so the knob jumps instead of sliding for users who turn off animations.

Frequently asked questions

How do you make a toggle switch with only CSS?

Use a real checkbox inside a label, hide the checkbox visually (not with display: none, so it stays focusable), and style a sibling span as the track with a ::before pseudo-element as the knob. The selector input:checked + span changes the track color and moves the knob with transform: translateX(). Clicking the label toggles the checkbox, so no JavaScript is required.

Is a CSS toggle switch accessible?

Yes, if it is built on a native checkbox. The generated code adds role="switch" so screen readers announce on/off, keeps the input focusable for Tab and Space, shows a focus ring with :focus-visible, has a text label (or aria-label), and turns off the animation for users who prefer reduced motion.

How do I read the switch value in a form or with JavaScript?

It is a normal checkbox: give the input a name and value to submit it with a form, add checked for an on default, and read input.checked or listen for the change event in JavaScript.

Why not hide the checkbox with display: none?

display: none and visibility: hidden remove the input from the keyboard tab order and from the accessibility tree, so keyboard and screen reader users could not operate the switch. The generated CSS makes it transparent and 1px small instead.

Should I use a switch or a checkbox?

Use a switch for settings that take effect immediately, like turning notifications on. Use a checkbox when the choice is applied later with a submit button or when users choose several options in a list.

How do I make the switch bigger on mobile?

Change the width and height (and the knob padding) here, or wrap the values in a media query. Aim for a touch target of at least 44 by 24 pixels; the whole label, including the text, is clickable.

Are all the tools on FreeToolCabin tested for quality and accuracy?

Yes! Every tool on our site undergoes automated Q/A testing in real browser environments to ensure high precision, correct output, and zero errors. If you happen to spot a bug or have a suggestion, please contact us at [email protected] and our team will review it right away.

Related tools

CSS Button GeneratorDesign CSS buttons with gradients, borders, shadows and hover and active states, then copy the code.CSS Tooltip GeneratorBuild pure CSS tooltips: position, arrow, colors, padding and animation. Copy HTML and CSS.CSS Box Shadow GeneratorDesign layered CSS box shadows with a live preview, presets and one-click Copy CSS.CSS Loader GeneratorPick from 14 pure CSS spinners and loaders, set color, size and speed, then copy the HTML and CSS.Open Graph PreviewPaste OG tags and preview how links look on Facebook, X and LinkedIn.YouTube Embed Code GeneratorCreate responsive YouTube embed code with start time, autoplay, loop and privacy mode.CSS Border Radius GeneratorRound each corner, draw elliptical 8-value radii or random blob shapes and copy the CSS.CSS Flexbox GeneratorBuild flexbox layouts visually: set container and item properties, then copy the CSS and HTML.Twitter Card GeneratorCreate X / Twitter Card meta tags with Open Graph fallbacks, character counters and a live preview.CSS Grid GeneratorDraw CSS grid layouts: set tracks and gaps, drag to name areas, then copy the CSS and HTML.PX to REM ConverterConvert px to rem or em and back, with a 1-100px table and a whole-CSS converter.Glassmorphism CSS GeneratorDesign frosted-glass UI cards with blur, transparency and border sliders, then copy the CSS.CSS Text Shadow GeneratorLayer CSS text shadows with a live preview and neon, 3D, outline and retro presets.HTML Editor (Live Preview)Write HTML, CSS and JavaScript and see a live, sandboxed preview as you type.HTML to JSX ConverterConvert HTML to valid React JSX: className, htmlFor, style objects, self-closing tags and events.CSS Clip-Path GeneratorDraw CSS clip-path shapes by dragging points: polygons, circle, ellipse and inset. Copy the CSS.CSS Animation GeneratorCreate CSS keyframe animations from 23 presets with live preview, timing controls and Copy CSS.CSS Triangle GeneratorMake CSS triangles in 8 directions with the border trick or clip-path. Set size and color, copy CSS.Cubic Bezier GeneratorDrag the control points to design a CSS easing curve, compare it with linear and copy cubic-bezier().CSS Filter GeneratorAdjust blur, brightness, contrast, hue, sepia and drop-shadow on an image and copy the CSS filter.CSS Specificity CalculatorCalculate CSS selector specificity (a, b, c), including :is(), :not(), :has() and :where(), and compare.Google Maps Embed GeneratorTurn an address or coordinates into responsive Google Maps iframe embed code, no API key.Neumorphism CSS GeneratorDesign soft UI neumorphic shapes with live preview and copy the box-shadow CSS.SVG Wave GeneratorMake layered SVG waves for section dividers and export SVG, PNG or a CSS background.SVG Blob GeneratorGenerate random organic SVG blob shapes with gradients and export SVG, PNG or CSS clip-path.CSS Clamp GeneratorTurn min/max sizes and viewport widths into a fluid CSS clamp() value in rem and vw.CSS @font-face GeneratorTurn font files or URLs into ready-to-paste @font-face rules with weights, styles and a preview.CSS Media Query GeneratorBuild @media rules for widths, orientation, dark mode and hover, or get framework breakpoints.CSS Scrollbar GeneratorStyle scrollbars: width, colors, radius and hover, with ::-webkit-scrollbar and scrollbar-color CSS.CSS Sprite GeneratorPack many icons into one sprite sheet PNG and get CSS classes with background-position.CSS Text Gradient GeneratorMake gradient text with background-clip:text: color stops, angle, font preview and fallback CSS.HTML Form BuilderDrag and drop form fields with labels and validation, then copy accessible HTML and CSS code.Iframe GeneratorCreate iframe embed code: size or responsive ratio, border, allow, sandbox, lazy load, live preview.PX to VW ConverterConvert pixels to vw and vh for any viewport size, and vw/vh back to px, with breakpoint tables.SVG Path EditorEdit an SVG path d attribute visually: drag points, edit commands, convert, scale and minify.SVG to CSS Data URITurn SVG code into a small URL-encoded data URI for CSS backgrounds, with Base64 size comparison.SVG to JSXTurn SVG code into a clean React component: camelCase attributes, style objects, TypeScript.Tailwind CSS Cheat SheetSearchable Tailwind class to CSS reference with live previews, class decoder and color palette.CSS Shape Divider GeneratorCreate wave, tilt, curve and triangle section dividers and copy the SVG, HTML and CSS.CSS Transform GeneratorRotate, scale, skew and translate an element with sliders and copy the CSS transform code.Font Pairing ToolPreview Google Font heading and body pairs on real layouts and copy the embed code and CSS.HTML Accessibility CheckerPaste HTML to find WCAG accessibility problems with impact level, the failing element and a fix.HTML Image Map GeneratorDraw clickable rectangle, circle and polygon hotspots on an image and get image map HTML.SVG ViewerOpen or paste SVG code to preview it live with zoom, backgrounds, size info and PNG export.Pathfinding VisualizerAnimate BFS, DFS, Dijkstra, A* and greedy search on a grid with walls, weights and mazes.Web App Manifest GeneratorCreate a PWA manifest.json and every app icon size from one image, downloaded as a ZIP.CSS Background Pattern GeneratorMake pure-CSS stripes, dots, checkerboard, zigzag and grid backgrounds and copy the code.CSS Color Filter GeneratorTurn a HEX color into a CSS filter that recolors black icons and SVGs, with an accuracy score.CSS Gradient Animation GeneratorCreate animated gradient backgrounds: pick colors, angle and speed, preview live and copy the CSS.CSS Hover Effects GeneratorPick button, link and image hover effects, tweak colors and timing, and copy the HTML and CSS.CSS InlinerMove CSS from style blocks into inline style attributes so HTML emails render in every client.
See all 52 Web, HTML & CSS Tools →