How to use the CSS Button Generator
- 1
Start from a preset
Primary, Pill, Outline, Dark, Gradient, Neo-brutal or Danger set every control at once.
- 2
Adjust the look
Change text, colours, gradient end, font size and weight, padding, radius, border and uppercase.
- 3
Choose a shadow
None, Soft, Lift on hover or Hard (neo-brutal) — each also changes the hover and active behaviour.
- 4
Copy CSS and HTML
The CSS targets a .btn class; the HTML shows both a <button> and an <a> version.
Features
Every state included
Base, :hover, :active and :focus-visible rules are generated together.
Gradient mode
A 135° linear gradient; hover uses filter: brightness(1.08) because gradients can’t transition smoothly.
Neo-brutal hard shadow
Offset solid shadow that grows on hover and collapses on click for a pressed feel.
Lift shadow
Hover raises the button 2px and deepens a tinted shadow; active drops it back.
Brand-tinted focus ring
A 3px outline in your background colour at 45% opacity, offset by 2px.
Escaped HTML output
Button text is HTML-escaped in the snippet, so symbols like & are safe to paste.
Who uses it — and for what
Developers without a framework
Get a polished, accessible button without pulling in a UI library.
Landing page builders
Match call-to-action buttons to brand colours quickly.
CSS learners
See how states, transitions and shadows work together in real code.
Designers prototyping
Try styles live, then hand developers exact values.
What makes a good button
A button should look clickable, read clearly and react when touched. Aim for a text/background contrast of at least 4.5:1 (check it with the Contrast Checker), a comfortable target of at least 44 px tall on touch screens, and visible hover, active and focus states. The generated CSS includes all three, plus a :focus-visible outline so keyboard users can see where they are without showing rings on mouse clicks.
Using the code
The CSS uses a single class, .btn, so you can put it on <button> or <a> elements. Use <button> for actions (submit, open, delete) and <a> for navigation to another page. A short transition makes hover changes feel smooth; the “lift” shadow moves the button up 2px with transform, which is cheap to animate. Pick a style preset, then fine-tune colours to match your brand.
How the states fit together
Every preset generates four rules, and each one solves a different problem:
- Base uses
display: inline-flexwith centred alignment and an 8px gap, so you can drop an icon next to the label without extra CSS. Thefontshorthand sets weight, size and a 1.2 line height in one line. - :hover changes background (or brightness for gradients) and, for Lift and Hard shadows, moves the button. A single
transitioncovers background, box-shadow, transform and filter over 0.2s. - :active gives instant feedback on press — a 1px drop by default, or the bigger pressed effect for Lift and Hard.
- :focus-visible draws an outline only when the browser thinks a focus ring is useful, typically for keyboard users, so mouse clicks stay clean.
Hover effects don’t exist on touch screens, so never put essential information in a hover state. The active and focus states are what touch and keyboard users actually see.
Variations you can build from the output
Because the CSS hangs off one class, size and colour variants are easy to layer on with a second class:
.btn--small { padding: 8px 16px; font-size: 14px; }
.btn--danger { background: #dc2626; }
.btn--danger:hover { background: #b91c1c; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
A few practical points when you adapt the code:
- Disabled buttons are exempt from WCAG contrast rules, but users still need to see that they exist; around 50–60% opacity is common. Add
transform: noneso a lift effect doesn’t fire on a disabled button. - Outline buttons rely on the border for their shape. Under WCAG 1.4.11, the border should reach 3:1 against the page background.
- Reduced motion: wrap the transform-based hover in
@media (prefers-reduced-motion: no-preference)if you want to respect users who turn animations off. - Long labels: buttons don’t wrap nicely; keep labels to a few words, or add
white-space: nowrapand test the narrowest screen.
Worked examples
Neo-brutal preset output
.btn {
...
font: 800 16px/1.2 system-ui, sans-serif;
text-transform: uppercase;
letter-spacing: 0.04em;
color: #111827;
background: #facc15;
border: 2px solid #111827;
border-radius: 6px;
box-shadow: 4px 4px 0 #111827;
}
.btn:hover {
background: #fde047;
transform: translate(-1px, -1px);
box-shadow: 5px 5px 0 #111827;
}
.btn:active {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 #111827;
}
On click the button moves 3px down-right while the shadow shrinks by 3px, so it looks pressed into the page. Dark text on this yellow scores 11.58:1 — comfortably AAA.
Pill preset with lift
.btn {
padding: 12px 30px;
background: #0f766e;
border-radius: 40px;
box-shadow: 0 2px 6px rgba(15, 23, 42, 0.12);
}
.btn:hover {
background: #115e59;
transform: translateY(-2px);
box-shadow: 0 10px 24px rgba(15, 118, 110, 0.35);
}
The hover shadow is tinted with the button’s own colour (rgba of #0f766e), which looks more natural than grey on a coloured button.
Contrast check on the presets
| Preset | Text / background | Ratio |
|---|---|---|
| Primary | white on #4f46e5 | 6.29:1 |
| Pill | white on #0f766e | 5.47:1 |
| Danger | white on #dc2626 | 4.83:1 |
| Gradient | white on #6366f1 (start) | 4.47:1 |
The Gradient preset’s lighter starting colour falls just under 4.5:1 for 16px text, and the brightening hover lowers it a little further. Use bolder, larger text (which counts as large text at 18.66px bold) or darken the start colour if AA matters.
Pro tips
- The Hover background control is ignored in gradient mode, where hover uses a brightness filter instead.
- For the Outline preset, set a hover background that is a very light tint of the text colour so the button fills gently.
- Rename .btn to match your project’s naming (for example .c-button) with a find-and-replace after pasting.
Glossary
- :focus-visible
- A CSS pseudo-class that matches when an element has focus and the browser decides a visible indicator is helpful.
- Neo-brutalism
- A design style using flat colours, thick borders and hard offset shadows with no blur.
- inline-flex
- A display value that keeps the element inline with text while laying out its children with Flexbox.
Frequently asked questions
Should I use a button or a link?
Buttons perform actions; links go somewhere. Style both the same if you like, but keep the right element.
What is :focus-visible?
A focus style that appears for keyboard navigation but not for mouse clicks.
How big should buttons be on mobile?
At least about 44 × 44 px so they are easy to tap.
Why does the gradient button use filter instead of changing the background on hover?
Browsers cannot smoothly transition between two gradient backgrounds, so the generator brightens the existing gradient with a filter, which does animate.
Can I add an icon inside the button?
Yes. The base rule uses inline-flex with an 8px gap, so an inline SVG or icon font placed before or after the text lines up automatically.
Does the hard shadow affect the layout around the button?
No. Box shadows and transforms never move neighbouring elements, so the pressed effect stays inside the button’s own space.
Last updated 2026-11-28 · Built by Vinayak Agarwal · Part of CSS & Design (37 tools)