Stroke

Master SVG stroke properties to create professional vector graphics with precise outline control, dashed lines, and smooth animations.

What Is Stroke in SVG

SVG stroke refers to the outline or border that can be applied to SVG shapes, paths, and text elements. Unlike HTML elements where borders are limited to rectangular boxes, SVG strokes can follow complex curved paths, irregular shapes, and any custom geometry you define. When you set a stroke on an element, the browser draws a line along the path of that element's perimeter or trajectory.

The stroke is distinct from the fill, which colors the interior of closed shapes. You can use stroke alone to create wireframe effects, or combine it with fill for outlined shapes with colored interiors. Strokes can be solid colors, gradients, or patterns, and they can range from hairline thin to arbitrarily thick. The versatility of SVG stroke makes it indispensable for creating everything from simple icons to sophisticated data visualizations and animated graphics.

The fundamental difference between SVG stroke and HTML borders lies in their flexibility. While CSS borders are constrained to rectangular boxes following the element's bounding box, SVG strokes conform precisely to the actual geometry of the element. A circle element gets a stroke that follows its curved perimeter exactly, a path with bezier curves receives a stroke that traces every curve, and complex polygons maintain consistent stroke width along every segment regardless of how irregular the shape becomes. This precision is essential when working with modern CSS layout techniques that combine traditional box model properties with SVG graphics.

Key stroke-related properties you can control include stroke for the color, stroke-width for thickness, stroke-linecap for endpoint appearance, stroke-linejoin for corner treatment, and stroke-dasharray for creating dashed patterns. Each property works together to give you complete control over how outlines appear on your vector graphics.

The CSS stroke Property

The CSS stroke property defines the color or paint server used to draw an element's stroke. As a presentation attribute, stroke only affects elements that can be given a stroke, such as <rect>, <circle>, <ellipse>, <line>, <path>, <polygon>, <polyline>, and <text> elements. When declared via CSS, the value overrides any stroke attribute set directly on the SVG element.

This property accepts various value types that give you flexibility in how you define stroke colors:

Named colors like "red" or "blue" provide quick, readable options for common colors. RGB and RGBA values let you specify exact color components with optional transparency. Hex codes offer a compact representation of RGB colors. HSL values allow you to define colors by hue, saturation, and lightness, making it easier to create color variations. For advanced color blending techniques, explore how color-mix works with SVG properties.

The currentColor keyword is particularly powerful as it inherits the stroke color from the element's CSS color property. This enables themeable SVG graphics that automatically adapt when you change text colors or implement dark mode. The none value removes any stroke entirely, which is useful for conditional styling or overriding inherited stroke settings.

For more advanced effects, you can use gradients defined in SVG <defs> by referencing them with stroke: url(#gradientId), or use patterns for textured strokes. The context-stroke value inherits stroke settings from a parent context element, useful in complex nested SVG structures.

Using CSS for stroke styling aligns with modern web development practices by separating styling from structure. This approach enables reusable classes, easier maintenance, and consistent styling across multiple SVG elements through your stylesheet rather than inline attributes. When combined with CSS selectors, you can create powerful conditional styling systems.

CSS stroke Property Values
1/* Various stroke color values */2.stroke-color {3  stroke: red;4  stroke: #ff0000;5  stroke: rgb(255, 0, 0);6  stroke: hsl(0, 100%, 50%);7  stroke: currentColor;8  stroke: none;9  stroke: url(#gradient-stroke);10  stroke: context-stroke;11}

Supported SVG Elements

Not all SVG elements can have strokes applied to them. The stroke property is designed for elements that define shapes, paths, or text where a visible outline makes sense. Understanding which elements support stroke helps prevent frustration when styling SVG graphics.

Shape elements that fully support stroke include <rect> for rectangles and squares, <circle> for circular shapes, <ellipse> for oval shapes, <line> for straight line segments, <path> for any custom path shape defined by drawing commands, <polygon> for closed multi-sided shapes with connected segments, and <polyline> for open connected line segments.

Text elements using <text> can have strokes applied to create outlined text effects, popular in headlines and logos. The text fill and stroke can be combined for layered typographic effects, with the stroke typically appearing outside the fill.

Group elements (<g>) can have stroke set as an inherited property that affects all child elements. This is powerful for applying consistent stroke styling across multiple shapes without repeating the property on each element. Stroke inheritance follows standard CSS inheritance rules within the SVG document structure. Understanding CSS positioning helps when combining stroke elements with positioned layouts.

Elements that do NOT support stroke include <defs> for definitions, <symbol> for reusable symbols, <use> for instance references (though the referenced element may have its own stroke), and various filter primitives and masking elements that aren't designed for visible rendering. Understanding these limitations helps you choose the right approach for each visual element.

When designing SVG graphics, plan your structure around elements that support the stroke styling you need. Group related elements for efficient stroke application and leverage inheritance to keep your styling maintainable. For complex icon systems, our web development team can help you implement scalable vector graphics efficiently.

Stroke Control Properties

stroke-width

Controls the thickness of the stroke. Accepts pixel values, percentages, and unitless values for SVG coordinate units.

stroke-linecap

Determines how strokes appear at endpoints: butt (default), round, or square endings.

stroke-linejoin

Controls corner appearance: miter (sharp), round (rounded), or bevel (chamfered).

stroke-dasharray

Creates dashed or dotted strokes by defining alternating dash and gap lengths.

stroke-dashoffset

Shifts where the dash pattern starts, enabling line-drawing animations.

stroke-opacity

Controls transparency independently from fills for layered visual effects.

Controlling Stroke Width and Endpoints

stroke-width

The stroke-width property controls the thickness of the stroke drawn around SVG elements. This property accepts length values including absolute units like pixels (px), points (pt), or relative units like percentages (%) relative to the SVG coordinate system. Unitless values use the SVG's native coordinate units, which is common when working with viewBox-defined graphics.

Thicker strokes are more prominent and can serve as visual anchors in your design, drawing attention to key elements. Thinner strokes create subtle outlines appropriate for technical diagrams, delicate wireframes, or backgrounds where the stroke should enhance without overwhelming. The default stroke width is typically 1 pixel in the SVG's coordinate system, but this scales proportionally with the SVG when sized.

Very thin strokes below 1 pixel may render inconsistently across displays with different pixel densities, potentially appearing uneven or disappearing at certain zoom levels. Conversely, extremely thick strokes may obscure the underlying shape, particularly for complex paths with many curves or corners.

stroke-linecap

The stroke-linecap property determines how strokes appear at their endpoints, which is especially important for open paths like lines and polylines. Three values are available:

butt cuts the stroke off squarely at the endpoint with no extension beyond the path end. This is the default value and works well when precise endpoint measurement matters, such as in technical drawings or when strokes meet at exact points.

round adds a semicircular cap centered at the endpoint with a diameter equal to the stroke width. This creates softer, more organic-looking lines ideal for hand-drawn styles, organic shapes, or designs where a friendly, approachable aesthetic is desired. This works beautifully with border-radius for creating cohesive rounded designs.

square extends the stroke beyond the endpoint by half the stroke width in each direction. This extends the visual length of lines, which can be useful when aligning multiple strokes or creating emphasis, but may cause unexpected overlaps if not accounted for in your design.

Choosing the right linecap significantly affects the visual appearance of your SVG graphics and should match the overall aesthetic and purpose of your design.

Comparison of stroke-linecap values: butt, round, and square

Visual comparison of stroke-linecap values

Creating Dashed Lines and Animations

stroke-dasharray

The stroke-dasharray property creates dashed or dotted strokes by specifying alternating patterns of visible stroke and gaps. This property accepts a comma or space-separated list of lengths that define the pattern. The values represent alternating dash and gap lengths, cycling through the sequence as needed to cover the entire stroke path.

A single value like stroke-dasharray: 10 creates a pattern of 10 units of dash followed by 10 units of gap. Two values like stroke-dasharray: 10, 5 create 10 units of dash followed by 5 units of gap, then repeat. Three or more values create more complex patterns that cycle through the entire sequence before repeating.

For dotted lines, use equal small values like stroke-dasharray: 2, 6 where the dash is much smaller than the gap. Complex dashed patterns with multiple segments are achieved with values like stroke-dasharray: 20, 10, 5, 10, which creates a repeating pattern of four segments.

stroke-dashoffset

The stroke-dashoffset property controls where the dash pattern starts along the stroke path, making it essential for creating line-drawing animations. By setting stroke-dasharray to the total path length and stroke-dashoffset to the same value, you create a pattern where the dash (if any) starts completely offset from the visible path.

When you animate stroke-dashoffset from the full path length down to zero, the dash pattern progressively moves into view, creating the illusion of a line drawing itself. This technique powers the popular animated line graphics seen in SVG logo animations, loading indicators, and data visualization reveals. Combine this with CSS transform for even more dynamic effects.

The animation works because as the offset decreases, more of the dash pattern becomes visible along the path. When offset reaches zero, the full pattern aligns with the path, showing the complete stroke. This technique requires no JavaScript and can be implemented entirely with CSS animations for smooth, performant effects.

Stroke Dasharray and Animation Examples
1/* Line drawing animation */2.animated-path {3  stroke-dasharray: 1000;4  stroke-dashoffset: 1000;5  animation: draw 2s forwards;6}7 8@keyframes draw {9  to {10    stroke-dashoffset: 0;11  }12}13 14/* Dashed line variations */15.dashed {16  stroke-dasharray: 10, 5;17}18 19.dotted {20  stroke-dasharray: 2, 6;21}22 23.complex-pattern {24  stroke-dasharray: 20, 10, 5, 10;25}

Practical Use Cases

Stroke properties are fundamental to numerous web graphics applications and mastering them opens up many possibilities for creating professional vector visuals.

Icon Systems rely heavily on strokes for consistent, scalable visual elements. Many modern icon sets use outline styles that depend entirely on stroke properties. Consistent stroke width across an icon set creates visual harmony and ensures icons look good at any size. The ability to control stroke-linecap and stroke-linejoin is essential for creating polished icon sets with consistent corner treatments and endpoint styles. Our web development services can help you design and implement professional icon systems for your applications.

Data Visualizations use strokes to create line charts, trend indicators, and connecting elements between data points. Dashed strokes often indicate projected or estimated data, while solid strokes show actual values. The stroke-dasharray property is invaluable for creating legend items that match the style of actual chart lines. Combining these with analytics services creates powerful data storytelling tools.

Technical Diagrams including flowcharts, architectural drawings, and system diagrams depend on precise stroke rendering with consistent widths and clear corner treatments. The stroke-linejoin property is particularly important here, as sharp miter joins look professional on technical drawings while round joins can appear too casual. Understanding how display property affects element layout helps when positioning diagram elements.

Animated Graphics frequently use stroke-dasharray and stroke-dashoffset to create drawing effects that reveal content progressively. This technique is popular for animated logos, loading animations, and storytelling visuals that guide users through information step by step. Our team specializes in creating engaging animated graphics for web applications.

Maps and Geographic Visualizations use strokes for borders, routes, and boundary delineations. Different stroke widths and dash patterns can indicate different types of boundaries or transportation routes, creating clear visual hierarchies in map-based interfaces.

Decorative Elements like dividers, frames, ornamental lines, and typographic treatments benefit from stroke styling. Outlined text, decorative dividers, and custom frame styles are all achieved through thoughtful application of stroke properties. When combined with border-radius, you can create sophisticated decorative elements that blend rounded and sharp aesthetics.

For projects requiring sophisticated SVG graphics, our web development experts can help you implement these techniques effectively.

Frequently Asked Questions

Ready to Master SVG Graphics?

Our team of SVG experts can help you create stunning vector graphics, animated illustrations, and performant web visuals.

Sources

  1. MDN Web Docs: stroke (SVG Attribute) - Primary reference for stroke definition and supported elements
  2. MDN Web Docs: stroke (CSS property) - CSS property specification and values
  3. MDN Web Docs: stroke-linecap - Line end styling documentation
  4. MDN Web Docs: Fills and strokes - Tutorial with code examples for SVG graphics