SVG Stroke Linecap: Measure Butt, Round and Square
See how butt, round, and square SVG stroke caps affect open paths. Measure their painted bounds and choose a cap that fits the space around your line icon.
On this page
- Choose the shape at the path endpoint
- Render three copies of one straight path
- Fix a cap that overruns or looks unchanged
- FAQ
- Do round and square caps make a straight line equally long?
- What does a cap draw on a zero-length path segment?
- Can an open SVG path use a different cap at each end?
- Start with an editable source
- Sources
For an open SVG stroke, butt stops at the path endpoints. round and square extend half the stroke width past each end of a straight segment. The round extension has a curved edge. The square extension reaches just as far and has corners. That difference matters when a stroke fits against another shape or sits close to the edge of an icon.
Choose the shape at the path endpoint
A line cap is the shape of the paint at an open end of a stroked path. The SVG 2 stroke cap definition gives stroke-linecap three values and sets butt as the initial value.
| Cap | Shape at each endpoint | Extension along the straight segment | When to use it |
|---|---|---|---|
butt | Flat edge flush with the endpoint | 0 | The painted end should land on a layout guide. |
round | Semicircle with a radius of half the stroke width | Half the stroke width | You need a curved end, such as the tip of an icon stroke. |
square | Flat-ended rectangular extension | Half the stroke width | You need a flat tip that projects beyond the endpoint. |
For a straight segment, choose a cap using the space available beyond its centerline endpoints. A line 240 units long with a 64-unit stroke paints 240 units with butt caps and 304 units with round or square caps. The extra 64 units comes from 32 units at each end. The path coordinates stay the same.
Render three copies of one straight path
Copy this SVG into line.svg. Its path runs from x = 48 to x = 288. The width, height and viewBox match so one SVG unit maps to one pixel in a native-size render.
<svg xmlns="http://www.w3.org/2000/svg"
width="336" height="176" viewBox="0 0 336 176">
<path d="M48 88H288" fill="none" stroke="#154c46"
stroke-width="64" stroke-linecap="butt"/>
</svg>Render the SVG on a transparent background with resvg line.svg line-butt.png. Change the cap to round and then square and save the other outputs as line-round.png and line-square.png. The resvg renderer produces PNGs for static SVG.
With Pillow installed, run this check on each PNG. It counts the columns with any nonzero alpha, including antialiased pixels. Replace the file name to check the other cap styles.
python3 - line-butt.png <<'PY'
import sys
from PIL import Image
with Image.open(sys.argv[1]) as image:
alpha = image.convert("RGBA").getchannel("A")
left, top, right, bottom = alpha.getbbox()
print(f"columns {left} through {right - 1}, rows {top} through {bottom - 1}")
print(f"{right - left} px wide, corner alpha {alpha.getpixel((17, 57))}")
PY
In our 336 × 176 PNG renders, butt occupied columns 48 through 287. Round and square occupied columns 16 through 319. All three occupied rows 56 through 119.
| Cap | Painted width | Alpha at pixel (17, 57) |
|---|---|---|
| Butt | 240 px | 0 (transparent) |
| Round | 304 px | 0 (transparent) |
| Square | 304 px | 255 (opaque) |
The corner sample sits inside the square cap but beyond the edge of the semicircle. Width alone cannot distinguish the two shapes. For a diagonal, curve, scaled stroke, or cropped export, inspect the actual render instead of applying the horizontal span from this fixture. The SVG bounding box guide explains how to choose a measurement that includes painted edges.
Fix a cap that overruns or looks unchanged
If an open stroke ends past an alignment guide, use butt to end the paint on the guide, or move the path endpoint back by half the stroke width when you need a round or square tip on a straight line. If an end appears cut off at the SVG boundary, check the rendered bounds and leave room for the cap in the viewBox before exporting.
If swapping the attribute does nothing, inspect the element's computed stroke-linecap in your browser. An applicable CSS rule can override the attribute, which has zero specificity under the SVG presentation attribute rules. If you're changing a bend or the seam of an ordinary closed outline, check stroke-linejoin instead. Each dash in a dashed stroke also has ends. For spacing between rounded dashes, see the SVG dotted-outline guide.
FAQ
Do round and square caps make a straight line equally long?
Yes. Each extends a straight stroke by half its width at both endpoints, so both add one stroke width to the painted length. Their shapes cover different areas near each corner.
What does a cap draw on a zero-length path segment?
For a zero-length subpath with a drawing command, butt paints nothing, round paints a circle, and square paints a square. The circle's diameter and the square's side length equal the stroke width. A lone move command does not paint a stroke.
Can an open SVG path use a different cap at each end?
The stroke-linecap value applies to both ends of each open subpath. To get two different ends, you can use a butt-capped stroke and draw the desired end shape separately, or convert the stroke to an outline and edit the resulting shape.
Start with an editable source
PerfectVector can recover editable SVG paths if the source icon exists only as a raster image. Run the source through the image-to-vector tool and compare the recovered silhouette and clearances with the original. For any segment that needs a configurable line cap, draw an open path and check the painted edge in the SVG you plan to ship.
Sources
- W3C SVG 2 — Drawing caps at the ends of strokes — Defines the three caps, their dimensions, and the zero-length case.
- W3C SVG 2 — Presentation attributes — Explains how a presentation attribute participates in the CSS cascade.
- Linebender — resvg — Documents the static SVG renderer used to make the PNGs.
More from the blog
SVG Non-Scaling Stroke: Keep Line Width on Resize
Keep SVG strokes the same width as shapes resize. Compare ordinary and non-scaling strokes, then check the result in your browser or export tool.
Measure SVG Path Length Without Confusing Scale
Measure SVG paths in browser user units, account for transforms and nominal size, and distinguish contour totals from pathLength calibration or machine travel.


