Fix SVG Stroke Truncation with CSS Overflow Visible
SVG stroke truncation occurs when a stroke extends past the defined
viewport or viewBox boundaries of an SVG element, causing the browser to
clip the edges. Applying the CSS rule overflow: visible
directly to the SVG element disables this automatic clipping behavior,
allowing the full stroke width to render outside the element’s container
boundaries without requiring manual recalculations of the viewBox
coordinates.
Why SVG Strokes Get Truncated
SVG vector paths render strokes centered along the path coordinate
line by default. If a path sits directly against the edge of the SVG
coordinate system (such as x="0" or y="0"),
half of the stroke’s specified width falls inside the coordinate
boundary, while the other half falls outside.
By default, web browsers apply overflow: hidden to root
<svg> elements when a viewBox attribute
is present. This default clipping path discards any visual data that
exceeds the container’s width and height, resulting in cut-off borders,
flat edges on rounded corners, and uneven line widths along the
perimeter.
How
overflow: visible Resolves Truncation
The overflow: visible property instructs the rendering
engine to skip clipping along the SVG’s outer bounding box. When
applied:
- Unclipped Rendering: The portion of the stroke that
extends past the coordinates of the
viewBoxremains fully rendered in the DOM. - Preserved Geometry: You do not need to expand the
viewBoxdimensions or manually offset path coordinates inside the SVG file to accommodate thick strokes. - Responsive Scaling: Responsive SVGs that change size retain proper stroke rendering regardless of how aggressively their parent container scales.
Implementation Methods
You can apply overflow: visible to your SVGs using
standard CSS rules or inline styles.
Using an External or Internal CSS Rule
Target the SVG globally, by class, or by ID within your stylesheet:
svg.allow-overflow {
overflow: visible;
}Using an Inline Style
Apply the declaration directly to the <svg> root
tag in your HTML:
<svg viewBox="0 0 100 100" style="overflow: visible;">
<rect x="0" y="0" width="100" height="100" stroke="blue" stroke-width="6" fill="none" />
</svg>Important Considerations
While overflow: visible is an efficient fix, consider
the following layout behaviors when implementing it:
- Parent Container Margins: Because the stroke extends beyond the SVG’s computed box dimensions, it may overlap adjacent HTML elements. Add margin or padding to the SVG or its parent container to maintain proper spacing.
<img>Tag Limitations: Theoverflow: visibleproperty only works for inline SVGs embedded directly in HTML. SVGs loaded inside an<img>tag are treated as isolated images and will remain clipped by the image’s bounding frame.- Alternative Approaches: If modifying surrounding
layout margins is not feasible, the alternative approach is to expand
the
viewBoxvalues (e.g., changingviewBox="0 0 100 100"toviewBox="-5 -5 110 110") to build stroke clearance directly into the SVG coordinate space.