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:

  1. Unclipped Rendering: The portion of the stroke that extends past the coordinates of the viewBox remains fully rendered in the DOM.
  2. Preserved Geometry: You do not need to expand the viewBox dimensions or manually offset path coordinates inside the SVG file to accommodate thick strokes.
  3. 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: