PerfectVector
Studio
By Irene Kim

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.

6 min read
On this page

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.

CapShape at each endpointExtension along the straight segmentWhen to use it
buttFlat edge flush with the endpoint0The painted end should land on a layout guide.
roundSemicircle with a radius of half the stroke widthHalf the stroke widthYou need a curved end, such as the tip of an icon stroke.
squareFlat-ended rectangular extensionHalf the stroke widthYou 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
Butt, round, and square SVG strokes share dashed guides at their endpoints. The round cap has a transparent corner where the square cap is filled.
Native resvg renders of the same 240-unit line and 64-unit stroke. Dashed guides mark the path endpoints and a coral ring marks the sampled pixel.

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.

CapPainted widthAlpha at pixel (17, 57)
Butt240 px0 (transparent)
Round304 px0 (transparent)
Square304 px255 (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

  1. W3C SVG 2 — Drawing caps at the ends of strokes — Defines the three caps, their dimensions, and the zero-length case.
  2. W3C SVG 2 — Presentation attributes — Explains how a presentation attribute participates in the CSS cascade.
  3. Linebender — resvg — Documents the static SVG renderer used to make the PNGs.

More from the blog

PerfectVector

Start with a cleaner SVG
that is easier to edit

Vectorize and inspect previews for free, with no daily limit.