Keep the same fraction visible when clipping SVG shapes

Use clipPathUnits to keep three quarters visible at two object widths.

Asset preparation2 min read

A crop that keeps 60 units of an 80-unit panel hides its right quarter. Apply that same clip to a panel twice as wide and most of it disappears. Use the clipped shape's bounding box when the visible fraction should stay the same.

Compare a fixed crop with a fraction

Open the editable SVG. In both rows, the left rectangle is 80 × 48 and the right one is 160 × 48. Teal marks the area kept by the clip. Slate shows the original area outside it.

The top row shows two equally wide teal areas. The bottom row shows a teal area twice as wide on the right. Both bottom panels keep three quarters of their width.

ClipVisible in the 80-unit panelVisible in the 160-unit panel
Fixed width of 606060
Three quarters of the bounds60120

Apply the clip to the shape that controls it

svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 48">
  <defs>
    <clipPath id="fraction" clipPathUnits="objectBoundingBox">
      <rect width=".75" height="1"/>
    </clipPath>
  </defs>
  <rect width="160" height="48" fill="#62ebcb" clip-path="url(#fraction)"/>
</svg>

With objectBoundingBox, the top-left corner of the target's bounds is (0,0) and (1,1) is the bottom-right. A clip width of .75 keeps 75% of the target width. The default, userSpaceOnUse, reads coordinates in the target's current user space. A clip width of 60 then means 60 user units. In this fixture, they correspond to CSS pixels at the original size. CSS Masking clipPathUnits.

Apply the clip to the rectangle or group whose geometry should determine the crop. If the edge moves when you add artwork, inspect the full group's bounding box. Change the clip target or remove the stray geometry. Resizing the SVG viewport alone does not change the target's fraction.

Image tools
All guides
Keep the same fraction visible when clipping SVG shapes | Formatry Media — Formatry