How Changing SVG Stroke-Width Affects Elements
The stroke-width property in SVG directly controls the
thickness of the outline applied to a shape or path. Adjusting this
value alters the visual weight of the element by expanding the stroke
symmetrically outward and inward from the path’s center line. Changing
the stroke-width impacts element boundaries, potential
clipping within the SVG viewBox, interactions with scaling
transformations, and the visual prominence of corner joins and line
caps.
Center-Aligned Stroke Expansion
By default, SVG renders strokes centered directly on the coordinate
path. When you increase the stroke-width, half of the added
width expands outward, and the other half expands inward. For example,
applying a stroke-width="10" to a <rect>
places 5 pixels of the stroke outside the rectangle’s defined
x, y, width, and
height coordinates, and 5 pixels inside.
Visual Dimensions and ViewBox Clipping
Because strokes expand beyond the defined geometry of a shape,
increasing stroke-width enlarges the overall visual
footprint of the element without changing its underlying coordinate
data. If a shape is positioned close to the edge of the SVG canvas, a
large stroke-width can push the outer half of the stroke
outside the defined viewBox, causing the edges of the
stroke to be cut off unless the viewBox is expanded or
overflow: visible is set.
Scaling and Resolution Independence
SVG elements scale vector graphics proportionally. When an entire SVG
or a specific element is scaled via CSS or the transform
attribute, the stroke width scales proportionally with it by
default.
To keep the stroke thickness constant regardless of scaling, you can
apply the vector-effect="non-scaling-stroke" attribute to
the element. This ensures the stroke maintains its defined pixel width
even when the shape shrinks or enlarges.
Interaction with Caps, Joins, and Dashes
Changing the stroke-width amplifies the effects of other
stroke properties:
stroke-linecap: Properties likeroundorsquareadd extensions to the ends of open paths, and their visual size scales directly with thestroke-width.stroke-linejoinandstroke-miterlimit: Corner styles (miter,round,bevel) become significantly more pronounced as the stroke thickens. Thick miter joins can create long, sharp spikes unless constrained bystroke-miterlimit.stroke-dasharray: The gaps and dashes defined in a dash pattern remain the same length, but a thicker stroke alters the aspect ratio of each dash, turning thin lines into block-like shapes.
Units and Supported Values
The stroke-width property accepts:
- Unitless values: Interpreted in the current SVG
user coordinate system (e.g.,
stroke-width="4"). - Length units: Standard CSS units like
px,em, orrem(e.g.,stroke-width="2px"). - Percentages: Relative to the diagonal size of the
current SVG viewport (e.g.,
stroke-width="1%"). - Zero or None: Setting
stroke-width="0"effectively hides the outline, leaving only the fill visible. Negative values are invalid and treated as0.