How to Style CSS Buttons: Hover, Focus, Active States and Accessibility

Build buttons that look good and work for everyone: the right element, padding and target size, hover and focus states, transitions and colour contrast.

Updated September 29, 2026 6 min read

Use the free CSS Button GeneratorDesign buttons with colors, padding, radius, shadow and hover effects

Buttons are the most clicked elements on almost any site, and they are often styled last, in a hurry, by copying a class from somewhere else. The result is familiar: a hover colour that barely changes, no visible focus when you press Tab, a tiny tap target on mobile, and white text on a light brand colour that nobody can read in sunlight.

This guide covers what makes a button work well: choosing the right HTML element, sizing, the four states every button needs, smooth transitions, and the accessibility checks that separate a pretty button from a usable one.

Start with the right element

Use <button> for actions and <a href> for navigation. The rule is simple:

  • If activating it goes to another page or URL, it is a link, even if it looks like a button.
  • If it does something on the current page (submits a form, opens a dialog, toggles a menu), it is a button.

A real <button> is focusable, activates with both Enter and Space, is announced as a button by screen readers, and submits forms. A <div> styled as a button has none of that unless you add tabindex, key handlers and ARIA roles yourself, and it is easy to miss something.

Inside forms, set type="button" on buttons that should not submit, because the default type is submit.

The base styles

Browsers give buttons their own font, border and background. A good base resets those deliberately:

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  padding: 0.75rem 1.5rem;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  color: #ffffff;
  background-color: #4f46e5;
  border: 2px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 200ms ease, transform 200ms ease, box-shadow 200ms ease;
}

font: inherit makes the button use the page's typography instead of the system default. inline-flex with gap keeps icons and labels aligned. A transparent border reserves space so an outline variant with a visible border stays the same size.

Size and touch targets

A button needs to be easy to hit. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels at level AA (with some exceptions), and many platform guidelines recommend around 44 to 48 pixels for touch. Generous padding is the easiest way to get there, and it also makes buttons easier to scan.

The four states

Hover

Hover confirms that something is interactive. A darker or lighter background, sometimes with a small lift, is enough:

.btn:hover {
  background-color: #4338ca;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

Hover does not exist on touch screens, so never put essential information in a hover state.

Active

The active (pressed) state gives instant feedback on click or tap. Removing the lift is a natural "press":

.btn:active {
  transform: translateY(0);
}

Focus

Keyboard users navigate with Tab and need to see where they are. Use :focus-visible, which shows the ring for keyboard focus but not after a mouse click in most browsers:

.btn:focus-visible {
  outline: 3px solid rgba(79, 70, 229, 0.5);
  outline-offset: 2px;
}

Never write outline: none without a replacement. The focus indicator itself should contrast with the background around it (3:1 is the usual benchmark).

Disabled

Disabled buttons are usually shown with reduced opacity and cursor: not-allowed. Be aware that a disabled button cannot receive focus and gives no explanation. In forms, it is often better to keep the button active and show validation messages when it is pressed.

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

Design one with the generator

The CSS Button Generator produces the base, hover, active and focus rules together, with a live preview you can hover and click.

  1. Open the CSS Button Generator. Choose a starting preset: Primary, Outline, Pill, Danger, Ghost or Retro.
  2. In the Style tab, set the Background, Border colour, Horizontal padding, Vertical padding, Border radius and Border width.
  3. In the Text tab, type the Button text and choose Text colour, Font family, Font weight and Font size. Switch on Uppercase with letter spacing if you want that style.
  4. In the Hover & shadow tab, choose the Hover background and Hover text colours, set Hover lift (a negative value pushes the button down instead) and Transition time, and toggle Box shadow with its Y offset, blur and opacity.
  5. Test the button in the Live preview: hover it, click it and tab to it. Change the Preview background to see it on your page colour.
  6. Copy the CSS code (a .btn class with :hover, :active and :focus-visible rules) and the HTML, or download the CSS file.

Accessibility checks before you ship

Contrast of text and background

Button labels are text, so they need 4.5:1 contrast against the button colour at normal sizes. Check both the normal and hover colours in the colour contrast checker; white text on mid-tone greens, oranges and light blues frequently fails.

Contrast of the button against the page

For outline and ghost buttons, the border or background that shows where the button is should reach 3:1 against the page. A pale grey button on a white page may be invisible to some users. Soft UI styles are especially prone to this; the neumorphism guide discusses how to compensate.

Clear labels

"Submit" and "Click here" say little. "Create account", "Download invoice" or "Save changes" tell users what will happen. If a button shows only an icon, add an aria-label.

Motion

Lifts and transitions should be short (100-300ms). For users who ask for reduced motion, turn off the movement:

@media (prefers-reduced-motion: reduce) {
  .btn {
    transition: none;
  }
  .btn:hover {
    transform: none;
  }
}

More on timing and easing is in the guide to CSS keyframe animations.

Variations and systems

Most interfaces need a small, consistent set: a primary button for the main action, a secondary or outline button for alternatives, a destructive style for delete actions, and a text or ghost style for low-priority actions. Define them as modifier classes sharing one base, so padding, radius and focus styles stay consistent. Shadows and corners can be fine-tuned with the box shadow generator and border radius generator, and gradient backgrounds come from the CSS gradient generator, though solid colours are easier to keep accessible.

Frequently asked questions

Use a link for navigation to another page or URL, and a button for actions on the current page. You can style either one to look like the other.

How do I remove the default button style?

Reset the background, border, font and padding explicitly, as in the base styles above, rather than using all: unset, which also removes focus behaviour you want to keep.

What is :focus-visible?

A pseudo-class that matches when the browser decides a focus indicator should be shown, typically keyboard focus. It lets you show a clear ring for keyboard users without showing it after every mouse click.

How big should a button be?

At least 24 by 24 CSS pixels to meet WCAG 2.2 AA target size, and ideally around 44 pixels high for comfortable touch use.

Why does my button move the layout when hovered?

Changing border width, padding or font weight on hover changes the button's size. Keep dimensions constant and use colour, shadow and transform for the hover effect.

Try it now: CSS Button Generator

Loading tool…