HTML SVG stroke

Beginner
9 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

SVG stroke is a presentation attribute that paints the outline of a shape or path. Use it on <line>, <circle>, <rect>, <polyline>, <path>, and related shapes.

Pair stroke with stroke-width. Then refine ends (stroke-linecap), corners (stroke-linejoin), and dashes (stroke-dasharray).

Baseline · Widely available.

Stroke presentation attributes are stable across modern browsers (widely available since about July 2020 for the CSS stroke property pairing).

Presentation attribute + CSS.

stroke and related stroke-* names also exist as CSS properties. If both are set, the CSS property wins.

Default stroke is none.

Shapes have no outline until you set a visible stroke. Default stroke-width is 1.

Syntax

stroke.html
<svg width="240" height="80" viewBox="0 0 240 80"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Thick stroked line">
  <line x1="20" y1="40" x2="220" y2="40"
        fill="none" stroke="#2563eb" stroke-width="12"
        stroke-linecap="round" />
</svg>

Pattern: choose a shape → set stroke + stroke-width → optionally add caps, joins, or dashes.

Attributes

Core stroke presentation attributes:

NameDescriptionTry it
strokeOutline paint (color, none, or url(#id))Try it
stroke-widthOutline thickness (default 1)Try it
stroke-linecapbutt, round, or squareTry it
stroke-linejoinmiter, round, or bevelTry it
stroke-dasharrayDash and gap lengthsTry it
stroke-dashoffsetShifts where the dash pattern startsTry it
stroke-opacityOutline transparency (0–1)Try it

Global Attributes

SVG elements you stroke (for example <line>, <path>, <circle>) also support the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Change thickness and line cap on a horizontal line.

12

Stroke is centered on the path. Caps only matter on open ends.

SVG

stroke-width="12" stroke-linecap="round"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Color and Width

Set stroke and stroke-width on a line.

HTML
<svg width="240" height="80" viewBox="0 0 240 80"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Thick blue stroked line">
  <line x1="20" y1="40" x2="220" y2="40"
        fill="none" stroke="#2563eb" stroke-width="12"
        stroke-linecap="round" />
</svg>
Try It Yourself

How It Works

  1. Draw a <line> (or any shape) inside <svg>.
  2. Set stroke to the outline color.
  3. Set stroke-width for thickness.
  4. Use fill="none" when you only want the outline.

Example 2 — Line Caps

Compare butt, round, and square ends.

HTML
<svg width="240" height="160" viewBox="0 0 240 160"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Three lines with different linecaps">
  <line x1="40" y1="30" x2="200" y2="30"
        stroke="#0f172a" stroke-width="16" stroke-linecap="butt" />
  <line x1="40" y1="80" x2="200" y2="80"
        stroke="#2563eb" stroke-width="16" stroke-linecap="round" />
  <line x1="40" y1="130" x2="200" y2="130"
        stroke="#16a34a" stroke-width="16" stroke-linecap="square" />
</svg>
Try It Yourself

How It Works

  1. butt ends flush with the path tip (default).
  2. round adds a half-circle cap.
  3. square extends the stroke by half the width past each end.
  4. Caps matter on open paths like lines and polylines — not closed shapes alone.

Example 3 — Line Joins

Control corners with stroke-linejoin.

HTML
<svg width="240" height="200" viewBox="0 0 240 200"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Polylines with different linejoins">
  <polyline points="30,50 90,20 150,50"
            fill="none" stroke="#0f172a" stroke-width="14"
            stroke-linejoin="miter" />
  <polyline points="30,110 90,80 150,110"
            fill="none" stroke="#2563eb" stroke-width="14"
            stroke-linejoin="round" />
  <polyline points="30,170 90,140 150,170"
            fill="none" stroke="#ea580c" stroke-width="14"
            stroke-linejoin="bevel" />
</svg>
Try It Yourself

How It Works

  1. miter keeps a sharp pointed corner (default).
  2. round softens the corner with an arc.
  3. bevel cuts the corner flat.
  4. Use a thick width so join differences are easy to see.

Example 4 — Dashed Stroke

Create a pattern with stroke-dasharray.

HTML
<svg width="240" height="100" viewBox="0 0 240 100"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Dashed stroked circle">
  <circle cx="120" cy="50" r="35"
          fill="none" stroke="#7c3aed" stroke-width="6"
          stroke-dasharray="14 8" stroke-linecap="round" />
</svg>
Try It Yourself

How It Works

  1. stroke-dasharray="14 8" means 14 on, 8 off.
  2. More numbers create richer patterns (e.g. 10 4 2 4).
  3. Odd-length lists are repeated to make an even pattern.
  4. Round caps soften each dash end.

Example 5 — Dash Offset

Shift the dash pattern with stroke-dashoffset.

HTML
<svg width="240" height="120" viewBox="0 0 240 120"
     xmlns="http://www.w3.org/2000/svg"
     role="img" aria-label="Two dashed lines with different dashoffset">
  <line x1="20" y1="40" x2="220" y2="40"
        stroke="#0f172a" stroke-width="8"
        stroke-dasharray="20 10" stroke-dashoffset="0" />
  <line x1="20" y1="80" x2="220" y2="80"
        stroke="#2563eb" stroke-width="8"
        stroke-dasharray="20 10" stroke-dashoffset="15" />
</svg>
Try It Yourself

How It Works

  1. Same stroke-dasharray on both lines.
  2. stroke-dashoffset="15" slides the pattern along the path.
  3. Useful for alignment and for draw-on style animations.
  4. Pair with pathLength when you need predictable dash math.

Edge Cases & Pitfalls

Most missing-outline bugs are default paint, thin width, or clipping.

default

stroke defaults to none

No outline appears until you set a visible stroke. Remember default fill is black, which can hide the issue on filled shapes.

centered

Strokes are centered on the path

Half the width sits inside, half outside. Thick strokes near the viewBox edge can look clipped.

caps

linecap needs open ends

stroke-linecap affects open subpaths. Closed shapes mainly show stroke-linejoin at corners.

css

CSS overrides attributes

If both a presentation attribute and a CSS stroke-* property are set, the CSS value wins.

Browser Support

SVG stroke presentation attributes are supported in every modern browser.

✓ Baseline · Widely available

SVG stroke

Widely available across Chrome, Firefox, Safari, Edge, and Opera. Internet Explorer 9+ supports basic stroke, stroke-width, linecap, linejoin, and dasharray. CSS stroke property pairing is Baseline widely available since about July 2020.

98% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Supported
stroke Widely supported

Bottom line: Safe for production icons, charts, and UI outlines. Prefer presentation attributes in these lessons so stroke stays visible in the SVG markup.

Key Takeaways

  • Paint: stroke + positive stroke-width.
  • Ends: stroke-linecap (butt / round / square).
  • Corners: stroke-linejoin (miter / round / bevel).
  • Dashes: stroke-dasharray + optional stroke-dashoffset.

One line: outline paint + width, then caps, joins, or dashes as needed.

Default CSS Settings

Conceptual stroke defaults for SVG shapes:

defaults.css
/* Conceptual SVG stroke defaults */
.shape {
  stroke: none;
  stroke-width: 1;
  stroke-linecap: butt;
  stroke-linejoin: miter;
  stroke-dasharray: none;
  stroke-opacity: 1;
}

/* CSS overrides presentation attributes when both are set */
.outline {
  stroke: #2563eb;
  stroke-width: 4;
}

Prefer presentation attributes in these lessons so the outline stays visible in the SVG markup.

Frequently Asked Questions

stroke paints the outline of a shape or path. Pair it with stroke-width, and optionally linecap, linejoin, and dasharray.
It sets outline thickness. The stroke is centered on the path, so half the width sits inside and half outside.
It shapes the ends of open strokes. Values are butt (default), round, and square.
It shapes corners where segments meet. Values are miter (default), round, and bevel.
Set stroke-dasharray to lengths for dash and gap, such as "12 8". Use stroke-dashoffset to shift the pattern.
Default stroke is none. Set a visible stroke color and a positive stroke-width. Also check that fill="none" is not the only paint when you expected an outline.

Next: SVG Filters

Learn blur, drop shadows, and reusable filter effects.

SVG Filters →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful