CSS Button Generator

Style a button’s colors, padding, corners, border and shadow and get CSS for the default, hover and focus-visible states together.

Button Design Best Practices: Size, Contrast, States and Clean CSS

Buttons are where users act: buy, save, send, sign up. A slightly unclear button costs real conversions and real frustration. Good button design is less about decoration and more about a handful of practical rules: size, contrast, clear states, hierarchy and honest labels.

Primary, secondary and text button designs with hover and keyboard focus states

Size: make it easy to hit

Fitts’s law, a model of human movement published in 1954, says the time to reach a target depends on its distance and its size. Bigger targets are faster and easier to hit, especially on touchscreens and for people with motor impairments.

GuidelineMinimum target size
WCAG 2.2 Target Size (Minimum), level AA24 × 24 CSS px (or enough spacing around smaller targets)
WCAG 2.5.5 Target Size (Enhanced), level AAA44 × 44 CSS px
Apple Human Interface Guidelines44 × 44 pt
Material Design48 × 48 dp

For main actions, aim for at least 44px tall on touch devices. Padding does most of the work: 12px vertical and 20–24px horizontal padding with a 16px font gives a comfortable 40–48px button.

Contrast: two different checks

  1. Label versus button background: the text must meet 4.5:1 for normal-size text (3:1 if the label is large or bold enough to count as large text).
  2. Button versus page background: under WCAG 1.4.11, the visual boundary needed to identify the button should have 3:1 contrast with its surroundings. A pale grey button on white may need a border.

Many brand colors fail the first check with white text. A common fix is to use a darker shade of the brand color for buttons than for illustrations. Our WCAG contrast guide explains the numbers.

The five states every button needs

StatePurposeTypical treatment
DefaultShows it’s clickableSolid fill or clear border
HoverConfirms the pointer is on it (desktop)Slightly darker or lighter, maybe a subtle lift
Focus (keyboard)Shows where keyboard focus isVisible outline with an offset, never removed
Active / pressedFeedback at the moment of clickingDarker still, or pressed-in look
DisabledShows the action isn’t availableReduced contrast, cursor: not-allowed, plus an explanation nearby

Use :focus-visible rather than :focus for the focus ring. It shows the ring for keyboard users but not after a mouse click, which removes the main reason designers used to hide focus outlines. WCAG 2.4.7 requires a visible focus indicator, and WCAG 2.2 adds rules about it not being hidden.

A note on disabled buttons: they are often confusing because users can’t tell why they can’t proceed. Many teams now keep the button enabled and show validation messages when it’s clicked instead.

The CSS button generator above produces the default, hover and focus-visible states together, with a short transition.

Hierarchy: primary, secondary, tertiary

A screen with five equally loud buttons has no clear next step. Use visual weight to show importance:

LevelStyleUse
PrimarySolid fill in the brand or action colorThe main action on the screen. Usually one per view
SecondaryOutline or light fillAlternatives: Cancel, Back, Save draft
Tertiary / textText only, underline on hoverLow-priority actions: Learn more, Skip
DestructiveRed fill or red outlineDelete, Remove; pair with confirmation

In dialogs, platforms differ on button order (Windows traditionally puts the primary action on the left, macOS on the right). On the web, be consistent within your product.

Labels: say what will happen

The label is the most important part of a button. Good labels:

  • start with a verb: “Download invoice”, “Create account”, “Send message”,
  • describe the result, not the mechanism: “Save changes” rather than “Submit”,
  • are short, usually one to three words,
  • match the heading or question: a dialog asking “Delete this file?” should have a “Delete” button, not “OK”.

Avoid “Click here” and “Submit” where possible. They are vague for everyone and meaningless out of context for screen reader users who navigate by a list of buttons.

Sentence case (“Create account”) is generally easier to read than title case or all caps. If your brand uses uppercase buttons, apply it with CSS text-transform so screen readers don’t spell words out. Our title case vs sentence case guide goes deeper.

ElementUse whenKeyboard behaviour
<button>It performs an action on the page: submit, open a menu, toggleActivates with Enter and Space
<a href>It navigates to another page or URLActivates with Enter; can open in a new tab

Styling a link to look like a button is fine. Making a <div> behave like a button is not: you lose keyboard support and screen reader semantics unless you rebuild them by hand.

Icons in buttons

Icons speed up recognition when paired with text. Icon-only buttons need an accessible name, such as aria-label="Close", and a tooltip helps sighted users too. Keep icons and text vertically aligned with display: inline-flex; align-items: center; gap: 8px.

Loading states

When an action takes time, change the button to show progress: disable repeat clicks, swap the label to “Saving…” or add a small spinner, and keep the button’s width fixed so the layout doesn’t jump. See our article on loading spinners versus skeleton screens for when to use which indicator.