CSS / Reference / clip: Understand Legacy Clip and Migrate to Clip-Path
clip is a legacy CSS property that limits the visible part of an element to a rectangle. It applies to elements with position: absolute or position: fixed and is deprecated, so prefer clip-path in new code. When migrating existing code, distinguish the boundary coordinates in rect() from the inward distances in inset().
Conditions and main values
Clipping hides drawing outside a region. It differs from editing the original image file or actually reducing the element’s width and height. The initial value of clip is auto, and it is not inherited. Setting only position: relative does not make legacy clip apply.
| Value or condition | Meaning and choice |
|---|---|
auto |
Do not apply a clipping rectangle through this property. |
rect(top, right, bottom, left) |
Specify the top, right, bottom and left boundaries using lengths or auto. This is the legacy comma-separated syntax. |
position: absolute / position: fixed |
The positioning conditions for clip. The example uses absolute. |
clip-path: inset(20px) |
Show a rectangle 20px inward from each edge. This can also be used on in-flow elements. |
The legacy clip rect does not accept percentages. The complete value clip: auto also differs from rect(auto, auto, auto, auto). The latter clips at the inside border edges, so overflowing descendants can reveal the difference. MDN clip syntax and application conditions
Compare the original and clipped regions with a complete example
Save the following complete code as clip-example.html in UTF-8 and open it in a browser. No external images, libraries or server are needed. Keep these demonstration styles in this file rather than pasting them into a shared site stylesheet. Compare all three boxes, then change one value at a time, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare clip and clip-path</title>
<style>
body { margin: 0; padding: 16px; font: 16px/1.6 sans-serif; }
main { max-width: 760px; margin: auto; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1rem; }
.samples { display: flex; flex-wrap: wrap; gap: 24px; }
.frame { position: relative; width: 200px; height: 120px;
background: #e5e7eb; outline: 1px solid #666; }
.target { width: 100%; height: 100%; padding: 0; border: 0;
background: linear-gradient(135deg, #006c9c, #ffd166); }
.legacy { position: absolute; top: 0; left: 0;
clip: rect(20px, 180px, 100px, 20px); }
.modern { clip-path: inset(20px); }
</style>
</head>
<body>
<main>
<h1>Compare clip and clip-path</h1>
<div class="samples">
<section><h2>clip: auto</h2>
<div class="frame"><div class="target"></div></div></section>
<section><h2>clip: rect(20px, 180px, 100px, 20px)</h2>
<div class="frame"><div class="target legacy"></div></div></section>
<section><h2>clip-path: inset(20px)</h2>
<div class="frame"><div class="target modern"></div></div></section>
</div>
</main>
</body>
</html>
The first box is the reference, showing the entire 200×120px gradient. The second and third should show a 160×80px region after excluding 20px at each edge. The gray background and outline belong to a separate frame, so the original box size remains visible. On a narrow window the comparison boxes wrap onto another row.
Convert rect coordinates to inset distances
The two declarations produce the same visible region because their numbers mean different things. These rules correspond to .legacy and .modern in the complete example.
.legacy {
position: absolute;
clip: rect(20px, 180px, 100px, 20px);
}
.modern {
clip-path: inset(20px);
}
In the legacy rect, top and bottom are measured from the inside top border edge; left and right are measured from the inside left border edge. A right coordinate of 180px does not mean removing 180px from the right side. The four inset values instead measure inward from their respective edges, using the border-box unless another reference box is specified. The target in this example has no padding or border, so that reference-box difference does not affect the comparison.
| Edge | rect boundary coordinate | inset distance |
|---|---|---|
| Top | 20px | 20px |
| Right | 180px | 200 − 180 = 20px |
| Bottom | 100px | 120 − 100 = 20px |
| Left | 20px | 20px |
For a box of width W and height H without padding or border, rect(t, r, b, l) corresponds to inset(t, W − r, H − b, l). In actual CSS, insert the calculated lengths rather than copying explanatory expressions such as W − r. If there are borders, padding or a different reference box, align the coordinate systems first. MDN clip-path reference boxes and shapes
Change the element size and compare
First change only .frame to a width of 300px and height of 180px in the complete example. Keeping the old rect coordinates leaves a 160×80px visible region, while inset(20px) now leaves 260×140px by excluding 20px from each edge of the larger box. Compare why the same numbers do not guarantee the same clipped result.
To match the two methods again, change the right boundary of .legacy to 280px and the bottom boundary to 160px. Replace these two rules in the complete example as follows. Leave the .modern declaration unchanged.
.frame {
width: 300px;
height: 180px;
}
.legacy {
clip: rect(20px, 280px, 160px, 20px);
}
Both methods should now leave a 260×140px region. After comparing, restore .frame to 200×120px and rect to 20px, 180px, 100px, 20px. To preserve the same inward spacing in a responsive box, inset expresses the intention more directly than repeatedly recalculating fixed boundaries.
Clipping, layout and accessibility
Clipping with clip-path generally leaves the element’s layout space unchanged. The example’s .modern occupies 200×120px and only its painted area becomes smaller. The .legacy element is out of normal flow because of absolute positioning; its parent .frame reserves the comparison space. Distinguish that positioning effect from clipping itself.
For new code, consider clip-path shapes such as inset for a rectangle or circle for a circle. When the goal is to hide overflowing content or offer scrolling, consider the separate role of overflow. A clip-path value other than none can also create a stacking context, so check overlapping elements after migration. CSS Masking on clipping and element geometry
Clipping is not access control or privacy protection. Hidden text can remain in the document, and its treatment by assistive technology needs separate checking. Do not clip buttons, inputs, essential information or keyboard focus indicators in ways that prevent users from operating the interface. Provide a way to read the content when zooming or using touch and keyboard navigation.
When clipping fails or looks different
- Inspect the selected element’s computed position and clip in developer tools. Relative or static positioning does not satisfy legacy clip’s conditions. In the example, change only .legacy’s position to relative, compare whether clipping disappears, then restore absolute.
- The rect order is top, right, bottom, left. If right is less than left or bottom is less than top, the expected rectangle will not remain. Check whether old coordinates were retained after changing width or height.
- An ancestor’s overflow, clip-path or another clipping rule can further reduce the visible region. Disable and re-enable declarations individually in developer tools to isolate the cause.
- When replacing clip with clip-path, compare positioning, reference boxes, borders, padding, stacking and focus indicators. The legacy rect() used by clip and the newer rect() shape in clip-path have different syntax; matching names do not make their values interchangeable.




