CSS Buttons: Hover Effects, States and Accessible Code | HorizonX
← The Journal

CSS

CSS Buttons: Hover Effects, States and Accessible Code

Style CSS buttons that work for everyone: a solid base, hover, focus, pressed and disabled states, four hover effects with code and a checklist for any button.

HorizonX

HorizonX

November 22, 2026 · 9 min read

CSS Buttons: Hover Effects, States and Accessible Code

To style CSS buttons that hold up in production, start from a real <button> element, reset its font and border, and design five states: rest, hover, keyboard focus, pressed and disabled. Animate transform and opacity, apply hover effects only on devices that can hover, and turn movement off when the user asks for reduced motion.

This guide builds that base, adds four hover effects you can copy, and ends with a checklist for judging any button you find in a gallery. If you would rather start from a finished component, the HorizonX buttons collection groups editable button components in one place.

Pick the element before you write CSS

Most button bugs start in the HTML. Use <button> when the control performs an action on the page and <a href> when it takes the user somewhere else. A styled <div> looks the same but needs extra script to behave like a button: the WAI-ARIA button pattern expects both Space and Enter to activate it, and a native <button> does that for free.

HTML has three button types, set with the type attribute. MDN's button reference describes them:

  • submit sends the form. It "is the default if the attribute is not specified for buttons associated with a <form>".
  • reset returns every control in the form to its initial value. MDN adds: "This behavior tends to annoy users."
  • button has no default behavior, so script decides what it does.

Write type="button" on every button that should not submit a form. It costs nothing and prevents the classic bug where a "Show password" toggle submits the login form.

A base button in CSS

Browsers give buttons their own font, border and padding. This base resets them and sets up custom properties so every variant below can reuse it:

.btn {
  --btn-bg: #192c46;
  --btn-fg: #ffffff;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  min-block-size: 44px;
  padding: 0.625rem 1.25rem;
  border: 2px solid transparent;
  border-radius: 0.5rem;
  background-color: var(--btn-bg);
  color: var(--btn-fg);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition: transform 160ms ease-out;
}

.btn:focus-visible {
  outline: 3px solid #192c46;
  outline-offset: 3px;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}

A few choices in there are deliberate:

  • font: inherit makes the button use the page font instead of the browser's control font.
  • The transparent border keeps the size stable when a variant adds a visible border, and it becomes a visible system-colored edge in Windows forced colors mode, where background colors are replaced.
  • min-block-size: 44px is comfortably above the minimum in WCAG 2.5.8 Target Size (Minimum), which asks for targets of "at least 24 by 24 CSS pixels" unless they are spaced apart.
  • :focus-visible with an outline shows a ring for keyboard users without flashing it on every mouse click. WCAG 2.4.7 Focus Visible requires a visible focus indicator, and an outline survives forced colors mode, while a box-shadow ring does not: MDN's forced-colors page lists box-shadow as "forced to 'none'".
  • aria-disabled="true" is an alternative to disabled when the button should stay focusable, for example to explain why an action is unavailable. Your script then has to ignore clicks on it.

The :active scale gives press feedback on every input, touch included, so keep it outside any hover query.

Apply hover only where hover exists

On a phone, a tap can leave a button stuck in its :hover style until the user taps somewhere else. Wrap hover styles in a media query. For a solid button, a slightly lighter navy is enough:

@media (hover: hover) {
  .btn-solid:hover {
    --btn-bg: #24406a;
  }
}

The hover media feature tests "whether the user's primary input mechanism can hover over elements". The key word is primary: a touchscreen laptop used with a trackpad reports hover, and a phone reports none. Treat hover as a bonus layer and make sure the button still reads as a button without it.

Four hover effects with code

Each effect below extends .btn. They follow one rule from web.dev's high-performance animation guide: "Avoid any property that triggers layout or paint unless it's absolutely necessary." In practice that means moving and fading things with transform and opacity, never animating width, padding or top.

1. Lift with a soft shadow

The shadow lives on a pseudo-element, so the hover only changes its opacity instead of repainting a box-shadow on every frame.

.btn-lift::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: 0 10px 24px -8px rgb(25 44 70 / 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease-out;
}

@media (hover: hover) {
  .btn-lift:hover {
    transform: translateY(-2px);
  }

  .btn-lift:hover::after {
    opacity: 1;
  }
}

2. Fill sweep for outline buttons

An outline button fills from the left. The fill is a scaled pseudo-element behind the label; isolation: isolate keeps it above the button's background and below the text.

.btn-sweep {
  --btn-bg: transparent;
  --btn-fg: #192c46;
  overflow: hidden;
  isolation: isolate;
  border-color: currentColor;
  transition: transform 160ms ease-out, color 250ms ease-out;
}

.btn-sweep::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background-color: #192c46;
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 250ms ease-out;
}

.btn-sweep:focus-visible {
  --btn-fg: #ffffff;
}

.btn-sweep:focus-visible::before {
  transform: scaleX(1);
}

@media (hover: hover) {
  .btn-sweep:hover {
    --btn-fg: #ffffff;
  }

  .btn-sweep:hover::before {
    transform: scaleX(1);
  }
}

The focused state shows the same fill, so keyboard users see the effect too. The label switches from navy to white, and both colors have strong contrast against the background they end up on. The label color transition is the one paint-triggering animation here, kept on purpose so the text never flashes white on white.

3. Rotating ring with a registered custom property

A conic gradient on the border box draws a ring. Registering the angle with @property lets the browser interpolate it, so the ring turns while the button itself stays still.

@property --ring-angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

.btn-ring {
  --btn-fg: #192c46;
  background:
    linear-gradient(#ffffff, #ffffff) padding-box,
    conic-gradient(from var(--ring-angle), #192c46, #c9d1dc, #192c46) border-box;
}

@media (hover: hover) {
  .btn-ring:hover {
    animation: ring-turn 2s linear infinite;
  }
}

@keyframes ring-turn {
  to {
    --ring-angle: 360deg;
  }
}

Two honest caveats. @property is Baseline only since July 2024 (Chrome 85, Firefox 128, Safari 16.4, per MDN's compatibility data checked on 2026-10-09). In an older browser the angle is not interpolated and the ring simply stays still, which is a fine fallback. And unlike the first two effects, this one repaints the gradient on every frame, which is acceptable for one call to action but not for a grid of thirty buttons.

4. Icon nudge

A small arrow that moves a few pixels tells the user where the action leads. Mark the icon aria-hidden="true" so it is not announced.

.btn-arrow svg {
  inline-size: 1em;
  block-size: 1em;
  transition: transform 200ms ease-out;
}

@media (hover: hover) {
  .btn-arrow:hover svg {
    transform: translateX(4px);
  }
}

A button shown in three states side by side: resting, hovered with a slight lift, and focused with an outline ring

Reduced motion and forced colors

Put these blocks after everything else so they win the cascade:

@media (prefers-reduced-motion: reduce) {
  .btn,
  .btn::before,
  .btn::after,
  .btn svg {
    transition: none;
  }

  .btn:hover,
  .btn:active,
  .btn:hover svg {
    transform: none;
  }

  .btn-ring:hover {
    animation: none;
  }
}

@media (forced-colors: active) {
  .btn {
    border-color: ButtonText;
  }

  .btn:focus-visible {
    outline-color: Highlight;
  }
}

With reduced motion, color and fill changes still happen, but instantly and without movement. That matches the intent of WCAG 2.3.3 Animation from Interactions: "Motion animation triggered by interaction can be disabled". In forced colors mode the gradient ring disappears because background-image is removed, so the explicit border keeps the button outlined. The prefers-reduced-motion and forced-colors queries are both Baseline features with wide support.

A checklist for judging any CSS button

Galleries of CSS buttons are a good source of ideas, but a snippet that looks right in a preview can still fail a keyboard or touch user. Before you paste one in, check it against these questions:

  1. Element. Is it a <button> (or an <a> for navigation), with an explicit type?
  2. Focus. Tab to it. Is there a visible ring, and is it an outline rather than a shadow?
  3. Contrast. Does the label meet text contrast, and does the focus ring reach 3:1 against the page, as WCAG 1.4.11 Non-text Contrast asks for component states?
  4. Size. Is the clickable area at least 24 by 24 CSS pixels, ideally larger for primary actions?
  5. Touch. Is hover gated with @media (hover: hover), and is there press feedback with :active?
  6. Motion. Does it animate only transform and opacity, and does it stop under prefers-reduced-motion?
  7. Disabled. Is the disabled state obvious without relying on color alone?
  8. Forced colors. Does it still look like a button when backgrounds and shadows are removed?

A snippet that fails one or two of these is often still worth using; fix those points before it ships.

An outline button and a filled button with visible focus rings, above a grid of both buttons in resting, hover, focus, pressed and disabled states

Ready-made buttons on HorizonX

If you need a more expressive button than the four above, these HorizonX components handle the states covered in this guide, and you can change their settings in the browser before exporting HTML, Svelte or React code:

  • Jelly Button is a real <button> with a jelly wobble on hover, a slight shrink on press, a focus ring and a reduced-motion branch that drops the animation.
  • Magnet leans toward the pointer before the cursor reaches it. It is a pointer effect, so it rests in place under reduced motion and keeps a focus ring for keyboard users.
  • Relief Button is a soft, neumorphic button whose shadows move inward on press. Because forced colors mode removes shadows, test it there before using it for a primary action.
  • Halo Button uses the registered custom property technique from effect 3 for its ring, with the same static fallback.

Browse the full buttons collection to compare them side by side, then run your pick through the checklist above.

FAQ

What are the three types of buttons in HTML?

submit, reset and button, set with the type attribute. A button inside a form without a type acts as submit, so set type="button" on any button that runs script instead of sending the form.

How do I write CSS for a button?

Select the button with a class, reset font, border and padding, set a background and text color with enough contrast, and give it at least 24 by 24 CSS pixels of target size. Then style :hover inside @media (hover: hover), :focus-visible with an outline, :active for press feedback and :disabled.

How can I style buttons in HTML using CSS?

Add a class to the <button> element and put the rules in a stylesheet rather than inline style attributes, so the hover, focus and pressed states can live in one place. The base and the four effects in this guide are written that way and can be combined on one element, for example class="btn btn-solid btn-lift btn-arrow".