Achieving overflow:hidden Effects Without Using overflow:hidden in CSS
This article explains how to clip overflowing content vertically while keeping horizontal overflow visible by using CSS techniques such as clip‑path and the contain property, providing code examples, demos, and a comparison with the traditional overflow:hidden approach.
An interesting topic: how to achieve the effect of overflow: hidden without actually using the overflow: hidden declaration.
In CSS, the overflow property determines what happens when an element’s content exceeds its block formatting context; overflow: hidden clips the excess content.
Controlling the Direction of overflow:hidden
The practical requirement is to clip content that exceeds the container vertically but not horizontally. The straightforward solution is to wrap the element in a parent that has overflow: hidden , but this can be difficult in complex layout flows.
If the layout permits, adding an outer wrapper with overflow: hidden works fine:
However, when the element is inside a complex layout, adding another wrapper may not be feasible, so an alternative method is needed.
Using clip-path for Clipping
Besides overflow: hidden , CSS provides other properties that can clip content, with clip-path being the most powerful.
With clip-path we can control clipping in any direction. The following example solves the vertical‑only clipping requirement:
<div class="g-container">
<div class="sub"></div>
</div>Key CSS code:
.g-container {
width: 200px;
height: 200px;
clip-path: polygon(-1000% 0, 1000% 0, 1000% 100%, -1000% 100%);
}The clip-path: polygon() creates a huge rectangle using negative coordinates, effectively defining a clipping region that behaves like overflow: hidden but only in the desired direction.
Two additional examples demonstrate how to clip only the left/right sides or only the top/left/right sides while leaving the opposite side unclipped:
{
// Clip left and right sides (vertical overflow hidden, horizontal not)
clip-path: polygon(0 -1000%, 100% -1000%, 100% 1100%, 0 1100%);
// Clip left, top, right sides (bottom not clipped)
clip-path: polygon(100% 0, 100% 1000%, 0 1000%, 0 0);
}The 1000% value is flexible; you can adjust it as needed.
Non‑overflow/clip‑path Clipping Methods
Besides overflow and clip-path , CSS also offers the contain property. The contain: paint value creates a containment context that prevents rendering of content outside the element’s bounds.
contain: paint establishes a new stacking context and a new formatting context, which can affect positioning and layout of child elements.
.g-container {
contain: paint;
}Demo of contain: paint :
Side effects of contain: paint include:
Creation of a new stacking context, which changes the reference point for position: fixed elements (they behave like position: absolute within the container).
Creation of a new formatting context, isolating the container’s children from external layout influences.
Summary
The article presents three ways to clip content that exceeds a container:
overflow: hidden
clip-path with custom polygon or inset shapes
contain: paint to prevent rendering outside the element’s bounds
Each method has different characteristics: overflow: hidden and contain: paint create a block formatting context (BFC), while clip-path only clips without affecting layout. Compatibility and side‑effects also differ, so choose the technique that best fits the scenario.
Additional demos are linked for each method, and the article concludes with a brief note on using short CSS snippets (e.g., clip-path: inset(50px 0) ) to achieve similar clipping effects in CSS‑battle challenges.
For more CSS articles, visit the author’s GitHub repository iCSS .
ByteFE
Cutting‑edge tech, article sharing, and practical insights from the ByteDance frontend team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.