CSS Overflow: clip, overflow-clip-margin, and Scroll Overflow Patterns

overflow: clip and overflow-clip-margin: cut off without creating a scroll container

overflow: hidden is the usual way to cut off a zoomed image or a decoration that sticks out of a card. It has a side effect that keeps surprising theme developers: the element becomes a scroll container. position: sticky inside it stops working, it can still be scrolled by script or by keyboard focus, and it creates a new block formatting context. overflow: clip only clips, with none of these side effects, and overflow-clip-margin lets the content stick out a little before it is cut.

Problem: A theme’s cards zoom their image on hover and use overflow: hidden to keep it inside the rounded corners. A sticky “Buy” button inside a long product card no longer sticks, and focusing a link inside a clipped area scrolls the card’s content out of view.

Solution: Replace overflow: hidden with overflow: clip where the goal is only to cut off overflowing content, use overflow-clip-margin to allow a focus ring or a shadow to extend beyond the edge, and keep hidden only where a scroll container is actually wanted.

Clip the hover zoom

.ha-card {
    border-radius: .75rem;
    overflow: clip;
}

.ha-card__image {
    transition: scale .3s ease;
}
.ha-card:hover .ha-card__image {
    scale: 1.05;
}

The zoomed image is cut off at the card’s rounded border exactly like with hidden. Because the card is not a scroll container, position: sticky children use the next scroll container up (usually the page), and keyboard focus cannot scroll the card’s content.

Sticky inside a clipped element

.ha-product-card {
    overflow: clip;           /* with hidden, the sticky footer below would not stick */
}
.ha-product-card__buy {
    position: sticky;
    bottom: 0;
}

Sticky positioning attaches to the nearest scrolling ancestor. With overflow: hidden that is the card itself, which never scrolls, so the button behaves like position: relative. With clip, the page is the scrolling ancestor and the button sticks while the card is in view. Sticky positioning in general: position: sticky.

Let focus rings and shadows extend: overflow-clip-margin

.ha-gallery {
    overflow: clip;
    overflow-clip-margin: 6px;
}

Content may overflow by up to 6px before it is cut, enough for a focus outline or a small shadow of the items at the edge. With hidden, focus outlines of edge elements are cut, which is an accessibility problem.

Clip only one axis

.ha-slider {
    overflow-x: clip;
    overflow-y: visible;   /* dropdowns inside may overflow vertically */
}

With hidden, setting one axis to hidden forces the other from visible to auto, and a dropdown inside a horizontally clipped slider gets cut or scrolls. clip combines with visible on the other axis as written.

When hidden is still right

overflow: hidden is the correct choice when a script scrolls the element programmatically (a slider that sets scrollLeft), or when the new block formatting context is wanted, for example to contain floats. For the latter, display: flow-root is the explicit modern way.

NOTE: overflow: clip works in all current browsers (Chrome 90, Firefox 81, Safari 16). overflow-clip-margin works in Chrome and Firefox; check Safari’s current support, and treat the margin as an enhancement: without it, the content is clipped at the edge as before.