CSS button generator with hover, focus and disabled states

Set the colours, size and corner radius, pick a hover effect, and copy a complete .btn rule set with hover, active, keyboard-focus and disabled states, plus the button markup, and a warning if the label contrast is too low.

Live demo · real result from this tool

Press Generate to create a result.

Processed on your device — nothing is uploaded.

How to use the CSS button generator

  1. Type the Label and choose the Background and Text colour.
  2. Adjust Horizontal padding, Vertical padding, Corner radius, Font size and Font weight.
  3. Add a Border width above 0 to get a Border colour field.
  4. Pick a Hover effect: Darken, Lighten, Lift with shadow or None. Untick Resting shadow for a flat button.
  5. Check the preview, which shows a normal and a disabled copy, and read any contrast warning. Copy the CSS and the HTML.

What it does and when to use it

Most hand-written buttons cover the normal look and a hover colour, then forget the rest. This generator writes all five states people meet: resting, :hover, :active (the button snaps back down when pressed), :focus-visible for keyboard users, and :disabled at half opacity with a “not allowed” cursor. Use it for call-to-action buttons, form submit buttons, or as a starting point for a design system.

If you want only the corner shape, use the border radius generator. For a softer custom shadow, build one in the box shadow generator, and to explore label colours that pass, try the contrast checker.

How it works

The base rule sets display: inline-flex with centred content, your padding, a font shorthand such as 600 16px/1.2 system-ui, sans-serif, the colours, border and radius. A 150 ms transition covers background colour, transform and shadow so every hover effect animates. The focus ring is a 3px outline 2px away from the button, coloured with your background made 15 points lighter in OKLCH. If that colour would have less than 3:1 contrast against a white page, the tool darkens the background colour instead until it reaches 3:1. MDN lists :focus-visible as Baseline widely available since March 2022.

Worked examples

A red delete button. Background #dc2626, white text, Darken, no resting shadow, radius 6px. The hover rule becomes background: #bf0002; and the focus ring outline: 3px solid #ff6c60;.

A contrast problem. Background #facc15 with white text returns the warning “Text contrast is only 1.53:1 — WCAG AA needs 4.5:1 for button labels.” Dark text such as #1f2937 fixes it.

The markup. With the defaults, the HTML output is <button class="btn" type="button">Get started</button>. The type="button" stops it from submitting a form by accident; change it to submit inside a form.

Limits and tips

  • The focus ring assumes a white page. On pale buttons the ring is darkened to reach 3:1 against white; on a dark page, check the ring against your real background.
  • The contrast check uses 4.5:1 for every size. Large bold labels can pass WCAG AA at 3:1, so the warning is on the strict side.

Frequently asked questions

Why does the CSS use :focus-visible instead of :focus?
:focus-visible shows the ring when someone moves to the button with the keyboard, but usually not after a mouse click. Keyboard users still see where they are, and mouse users do not get a ring they did not ask for.
How is the hover colour chosen?
Darken and Lighten move your background's lightness down or up by 8 points in the OKLCH colour space, which changes brightness while keeping the hue. Lift with shadow keeps the colour and moves the button up 2px with a larger shadow.
Why do I get a contrast warning?
The tool compares the text colour with the background. Below 4.5:1, the WCAG AA level for normal text, it warns you. White on yellow, for example, is only 1.53:1 and is hard to read for many people.
Can I use the CSS on a link instead of a button?
Yes, add class btn to an a element. Use a link to go to another page and a real button for actions such as submitting a form.
Does it make gradient or icon buttons?
Not directly. The background is a single colour. The rule does set display inline-flex with a 0.5em gap, so an icon placed next to the label lines up neatly.

Sources

  1. :focus-visible - CSS | MDN — MDN Web Docs, accessed 2026-10-08

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close