Skip to main content
CodingMarble

Code snippets

CSS Button Hover Effects: Fill Slide, Press and Focus

A good CSS button has distinct hover, focus-visible, active and disabled states, each changing only cheap properties like background, transform and box-shadow. Build the base button first, add a pseudo-element for the fill-slide hover, wrap hover in a media query for devices that can hover, and never remove the focus ring without replacing it.

By CodingMarble · Published · 4 min read

Lines of code on a computer screen
Photo: Chris Ried on Unsplash

Start with a real button element

Use the button element for actions and the anchor element for navigation. A real button is focusable with the keyboard, responds to Enter and Space, announces itself as a button to assistive technology, and supports the disabled attribute. A div styled to look like a button gives up all of that and then needs scripts to win it back.

The base style below makes decisions you will want to keep through every effect: a minimum target size, a visible text colour with strong contrast, and a transition declared on the base rule rather than on :hover. Declaring the transition on the base rule means the element animates both into and out of the hover state. If you only declare it on :hover, the button snaps back instantly when the pointer leaves.

CSS· Base button: size, contrast and transitions in one place
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.75rem;
  padding: 0.5rem 1.5rem;
  border: 2px solid #1d4ed8;
  border-radius: 0.5rem;
  background: #1d4ed8;
  color: #ffffff;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  transition: color 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
}

The min-height of 2.75rem is 44px at the default font size, a comfortable touch target. WCAG 2.2 sets a lower minimum of 24 by 24 CSS pixels for target size, while platform guidelines recommend larger, so a 44px minimum is a sensible target for anything used on a phone. The font: inherit line matters because buttons do not inherit the page font by default.

The fill-slide hover with a pseudo-element

A fill-slide effect makes a coloured layer travel across the button on hover. Animating the background itself is awkward, so the layer lives on a ::before pseudo-element that starts off to the left and slides in with transform. The text must sit above the layer, so the layer gets a negative z-index and the button creates its own stacking context.

CSS· Outline button whose fill slides in from the left
.btn--slide {
  background: transparent;
  color: #1d4ed8;
  overflow: hidden;
  isolation: isolate;
}

.btn--slide::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #1d4ed8;
  transform: translateX(-101%);
  transition: transform 0.3s ease;
}

@media (hover: hover) {
  .btn--slide:hover {
    color: #ffffff;
  }
  .btn--slide:hover::before {
    transform: translateX(0);
  }
}

Each part has a job. The isolation: isolate declaration makes the button a stacking context, so the z-index of -1 places the layer behind the text but still in front of the button's own background. The overflow: hidden clips the layer while it is outside the button. The value -101% rather than -100% avoids a hairline of colour showing at the edge on some screens. Because the layer moves with transform, the animation stays cheap. To slide from another direction, change translateX to translateY, or use a positive value for the opposite side.

Check contrast in both states. Blue text on a white page and white text on a blue fill must each meet the 4.5:1 ratio for normal-size text, so test your own colours with a contrast checker rather than trusting this palette.

Hover on touch devices

Touch screens have no pointer hovering above the page, and some mobile browsers apply :hover after a tap and leave it stuck until the next tap elsewhere. The result is a button that looks highlighted long after it was pressed. The (hover: hover) media query matches when the primary input can hover comfortably, so wrapping hover rules in it keeps the effect on desktops and out of the way on phones.

The fill-slide example already uses it. The key point is what you do not wrap: the active and focus-visible states should work everywhere, because touch users and keyboard users still need feedback. Treat hover as an enhancement, never as the only signal that something is clickable.

The press effect with :active

The :active pseudo-class applies while the button is being pressed. A small downward movement and a flatter shadow imitate a physical key. This works well for solid buttons that carry a drop shadow.

CSS· Raised button that sinks when pressed
.btn--raised {
  box-shadow: 0 4px 0 #1e3a8a;
}

@media (hover: hover) {
  .btn--raised:hover {
    transform: translateY(-1px);
    box-shadow: 0 5px 0 #1e3a8a;
  }
}

.btn--raised:active {
  transform: translateY(4px);
  box-shadow: 0 0 0 #1e3a8a;
}

The shadow has no blur, which gives a solid ledge of colour underneath. On press, the button moves down by exactly the height of that ledge and the shadow shrinks to nothing, so the top surface lands where the ledge was. Keep the movement small. Large shifts feel clumsy and, on small buttons, can move the target away from the pointer mid-click.

Focus rings that survive keyboard use

Removing the outline with outline: none is one of the most common accessibility mistakes in CSS, because keyboard users lose all indication of where they are. The :focus-visible pseudo-class is the modern fix. Browsers apply it when a focus indicator is helpful, which is typically keyboard navigation, and generally not when a mouse click focuses a button. That lets you show a strong ring without it appearing on every click.

CSS· A two-tone ring that stays visible on light and dark backgrounds
.btn:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 3px;
}

.btn--slide:focus-visible {
  outline-color: #1d4ed8;
}

@media (forced-colors: active) {
  .btn:focus-visible {
    outline-color: Highlight;
  }
}

The outline-offset moves the ring away from the button edge, which keeps it from merging with the border. Outline is preferable to box-shadow for focus because it is not clipped by overflow: hidden on the parent in the same way and it is respected in Windows high contrast mode. The forced-colors block uses a system colour keyword so the ring follows the user's chosen palette. Aim for a ring colour with at least 3:1 contrast against the colour next to it.

The disabled state

Style the disabled state through the attribute so the look and the behaviour cannot drift apart. A button with the disabled attribute cannot be focused or clicked and is skipped by the keyboard, which is correct for an action that is genuinely unavailable.

CSS· Disabled buttons look inactive and ignore hover and press
.btn:disabled,
.btn[aria-disabled="true"] {
  border-color: #9ca3af;
  background: #e5e7eb;
  color: #4b5563;
  box-shadow: none;
  transform: none;
  cursor: not-allowed;
}

.btn--slide:disabled::before {
  display: none;
}
  • WCAG does not require disabled controls to meet contrast ratios, but readable text still helps people understand what is unavailable.
  • If a user needs to know why a button is disabled, put the reason in visible text nearby instead of relying on a tooltip.
  • Use aria-disabled="true" only when you deliberately want the button to stay focusable, and then block the action in your script.
  • Add a transition only to properties that change. Listing transition: all makes it easy to animate things by accident.

For motion-sensitive visitors, the effects above are small and short, so they are rarely a problem. If you add larger movement, wrap it in a prefers-reduced-motion: no-preference query so it applies only when the user has not asked for less motion.

Need this built properly for your business?

CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.