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

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.
.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.
.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.
.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.
.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.
Keep reading
- Code snippetsCSS Loading Spinner Examples: Ring, Dots and SkeletonA CSS loading spinner is an element with a looping keyframe animation, usually a rotating bordered circle, paired with visible or screen-reader text that says what is loading. Animate only transform and opacity, honour prefers-reduced-motion, and use a skeleton instead of a spinner when you already know the shape of the content that is coming.
- Code snippetsDark Mode With CSS Variables: A Practical GuideDefine your colours once as CSS custom properties on :root, override them inside a prefers-color-scheme: dark query, and let a data-theme attribute on the html element take priority when the visitor picks a theme manually. A tiny inline script in the head applies the saved choice before the first paint so the page never flashes the wrong theme.
- Web developmentResponsive Web Design Checklist for Real SitesA responsive site adapts to any screen width without horizontal scrolling, tiny text or unreachable buttons. Work through the viewport tag, fluid layout, breakpoints set by content, touch sizes, images, tables, forms and real-device testing, and check each one in your own pages.
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.