PerfectVector
Studio
By Irene Kim

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.

5 min read
On this page

Add vector-effect="non-scaling-stroke" to the SVG path whose stroke should stay the same width when the shape scales. That lets a map boundary stretch or a diagram connector grow longer while keeping the same line weight. The SVG 2 painting specification defines how the renderer calculates that stroke.

The attribute changes how the stroke is drawn. The underlying path still scales. Use it for artwork that needs a consistent line weight at different display sizes. Keep ordinary strokes when the weight should grow in proportion to the artwork, as it often should for a logo.

What a 2× transform does to a 6-unit stroke

A non-scaling stroke is a stroke whose width is calculated after the path has been mapped into the outer SVG viewport. An ordinary stroke is calculated in the path's own coordinate system and scales along with the path. The SVG 2 coordinate system rules describe how this works with nested viewports.

Save this as stroke-scale.svg. All three paths have the same starting length and stroke width. The second and third paths use scale(2). The third path adds vector-effect.

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="324" viewBox="0 0 240 324">
  <path d="M40 54h80" stroke="#145956" stroke-width="6" />
  <path d="M20 81h80" transform="scale(2)"
        stroke="#145956" stroke-width="6" />
  <path d="M20 135h80" transform="scale(2)"
        stroke="#145956" stroke-width="6"
        vector-effect="non-scaling-stroke" />
</svg>

Open the SVG in a browser, or export it at its native 240 × 324 pixel size with Inkscape. To measure stroke width, count the painted pixels vertically across each line at x = 100. Measuring away from the endpoints isolates the stroke width from the line cap.

inkscape stroke-scale.svg --export-area-page --export-filename=stroke-scale.png

In our Inkscape 1.4.4 export, the original line measured 80 pixels long and 6 pixels thick. Both scaled lines reached 160 pixels in length. The ordinary stroke doubled to 12 pixels thick, and the non-scaling stroke stayed 6 pixels thick.

Three Inkscape renders showing an 80 by 6 pixel line, a 160 by 12 pixel line, and a 160 by 6 pixel line
Three crops from the same Inkscape 1.4.4 export, shown at equal magnification. Both scaled paths doubled in length. The last one kept its original stroke width.

Put the attribute on the shape you want to stroke

Add vector-effect="non-scaling-stroke" to each stroked path, line, or other SVG shape that needs it. A parent group can still carry the transform. The vector effect itself does not inherit from the group. If you prefer a shared CSS rule, set vector-effect: non-scaling-stroke on a selector that matches each shape. The MDN vector-effect examples show the attribute and CSS property.

Resize the artwork at the smallest and largest sizes you plan to ship. Check the width across the straight part of a stroke. For curves or sharp joins, leave room for the painted edge to extend beyond the path. The SVG bounding box guide shows how to check the visible footprint.

If the destination still changes the stroke width

  • The effect is on a group. Move it to the stroked shapes or match those shapes with a CSS selector. Check the computed vector-effect in browser developer tools if the result still changes.
  • The whole SVG is being scaled as a page element. Test a resize that changes the SVG viewport, such as setting its layout width, separately from a CSS transform on a surrounding HTML wrapper. A wrapper transform can magnify the rendered graphic as a whole. See the CSS Transforms rendering model.
  • The export disagrees with the preview. Run the same three-line file in the destination. Our resvg 0.47.0 export drew both scaled strokes 12 pixels thick. If your destination behaves the same way, make a copy at the final size, set the desired stroke width there, and remove the scaling transform after applying it to the path coordinates. For the sample's third path, d="M40 270h160" with stroke-width="6" gives the final coordinates and width directly.
  • The stroke vanishes or clips at the edge. Check paint, opacity, clipping and viewport margin with the SVG stroke troubleshooting guide.

FAQ

Does non-scaling stroke freeze the path size?

No. The path still follows the transform. The change is in how the renderer calculates the stroke width around that path.

Can I use non-scaling stroke on an entire SVG group?

Apply it to the stroked shapes inside the group. A shared selector can target them together, while the group keeps its existing transform.

Use ordinary strokes when the logo's line weight needs to grow in proportion to the mark. Consider a non-scaling stroke for an interface icon that needs a consistent line weight across several display sizes. Check those sizes in the destination.

Start with editable artwork

PerfectVector can turn flat raster artwork into editable SVG paths. Apply non-scaling stroke in your SVG editor or code to paths that use live strokes.

If the only source is a PNG or JPEG, vectorize the artwork and inspect the contours at both target sizes. When a traced line arrives as a filled shape, redraw it as a stroked path if you need control over its line weight.

Sources

  1. W3C SVG 2 — Vector effects in painting — defines which coordinate space the renderer uses to calculate a non-scaling stroke.
  2. W3C SVG 2 — Coordinate systems and vector effects — documents SVG viewport mapping and lists inheritance behavior for the property.
  3. MDN — SVG vector-effect — shows how to apply the setting to a path and how CSS can set it.
  4. W3C CSS Transforms — Transform rendering model — explains how an outer CSS transform maps a whole element's rendering.

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.