CSS stroke-width Property

The CSS stroke-width property specifies the thickness of the stroke used to draw an SVG element's outline.

It controls how wide a line or outline appears without changing the underlying SVG path geometry. The property works with stroke, stroke-linecap, stroke-linejoin, and other stroke properties to style SVG icons, diagrams, charts, and illustrations.

stroke-width Syntax

The stroke-width property accepts a non-negative length, percentage, or number.

Basic Syntax

svg path {
  stroke-width: 5;
}

Number Values

svg path {
  stroke-width: 0;
}
svg path {
  stroke-width: 1;
}
svg path {
  stroke-width: 2.5;
}
svg path {
  stroke-width: 10;
}

Unitless numbers represent distances in the SVG user coordinate system.

Length Values

svg path {
  stroke-width: 2px;
}
svg path {
  stroke-width: 0.25rem;
}
svg path {
  stroke-width: 3pt;
}
svg path {
  stroke-width: 0.2em;
}

Length values use CSS units and are resolved according to the applicable CSS and SVG coordinate systems.

Percentage Values

svg path {
  stroke-width: 1%;
}
svg path {
  stroke-width: 2%;
}
svg path {
  stroke-width: 5%;
}

Percentages are calculated relative to the normalized diagonal of the current SVG viewport.

CSS-Wide Keywords

svg path {
  stroke-width: initial;
}
svg path {
  stroke-width: inherit;
}
svg path {
  stroke-width: unset;
}
svg path {
  stroke-width: revert;
}
svg path {
  stroke-width: revert-layer;
}

The initial value is 1px, which corresponds to one CSS pixel before applicable transformations.

Property Values

Value Description
<number> A non-negative unitless value representing a width in SVG user units.
<length> A non-negative CSS length, such as 2px, 0.2em, or 0.25rem.
<percentage> A non-negative percentage relative to the normalized diagonal of the current SVG viewport.
initial Resets the property to 1px.
inherit Uses the computed stroke width from the parent element.
unset Uses the inherited value because the property is inherited.
revert Reverts the property according to CSS cascade origin rules.
revert-layer Reverts the property according to CSS cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 1px
Inherited Yes
Applies to Applicable SVG shapes, paths, and text elements
Computed value Absolute length or percentage, with numbers converted to absolute lengths
Animation type By computed value type
Percentages Relative to the normalized diagonal of the current SVG viewport
Related properties stroke, stroke-linecap, stroke-linejoin, vector-effect
Specification SVG 2

Valid Examples

svg path {
  stroke-width: 0;
}
svg line {
  stroke-width: 3;
}
svg circle {
  stroke-width: 5px;
}
svg rect {
  stroke-width: 2%;
}
svg polygon {
  stroke-width: calc(2px + 3px);
}

Invalid Examples

svg path {
  stroke-width: -5;
}
svg path {
  stroke-width: -2px;
}
svg path {
  stroke-width: auto;
}
svg path {
  stroke-width: thick;
}
svg path {
  stroke-width: 5px 10px;
}

Negative widths, unsupported keywords, and multiple width values are invalid.

How It Works

An SVG stroke is painted along the outline of a shape or path. The stroke-width property determines the thickness of that painted outline.

Step 1: Create an SVG Path

<svg viewBox="0 0 240 120">
  <path d="M20 90 L120 20 L220 90"/>
</svg>

Step 2: Define the Stroke

path {
  fill: none;
  stroke: #2563eb;
}

The path has a blue stroke using the initial stroke width.

Step 3: Increase the Stroke Width

path {
  fill: none;
  stroke: #2563eb;
  stroke-width: 12;
}

The stroke becomes thicker while the path coordinates remain unchanged.

Complete SVG Example

<svg viewBox="0 0 240 120" role="img" aria-label="Blue angled line with a thick stroke">
  <path d="M20 90 L120 20 L220 90" fill="none" stroke="#2563eb" stroke-width="12"/>
</svg>

Important Consideration

Increasing the stroke width expands the painted area around the path. It does not change the original path coordinates or the size of the filled geometry.

Comparing Stroke Width Values

Different stroke widths can change the visual weight and emphasis of an SVG element.

Value Typical Appearance
0 No visible stroke thickness
1 Thin outline
3 Moderate outline
6 Thick outline
12 Very thick outline

Thin Stroke

.thin-outline {
  stroke: #2563eb;
  stroke-width: 1;
}

A thin stroke is useful for subtle details and fine outlines.

Medium Stroke

.medium-outline {
  stroke: #2563eb;
  stroke-width: 4;
}

A medium stroke provides greater visibility while preserving detail.

Thick Stroke

.thick-outline {
  stroke: #2563eb;
  stroke-width: 12;
}

A thick stroke creates a stronger visual emphasis.

Zero Stroke Width

.zero-width {
  stroke: #2563eb;
  stroke-width: 0;
}

A zero-width stroke does not produce a visible painted outline.

Important Consideration

The apparent thickness also depends on the SVG coordinate system, transformations, and rendered size.

Percentage Stroke Widths

Percentage values for stroke-width are calculated relative to the normalized diagonal of the current SVG viewport.

Normalized Diagonal

The normalized diagonal is calculated using the viewport width and height:

Normalized diagonal = sqrt((width² + height²) / 2)

This produces a reference length based on both viewport dimensions rather than using only the width or height.

Example Calculation

For a square SVG viewport measuring 200 by 200 user units:

Normalized diagonal = sqrt((200² + 200²) / 2)
Normalized diagonal = 200

A stroke width of 5% therefore corresponds to 10 user units in that coordinate system.

Percentage Example

svg path {
  stroke: #2563eb;
  stroke-width: 5%;
}

Percentage vs. Number

.percentage-stroke {
  stroke-width: 5%;
}
.number-stroke {
  stroke-width: 10;
}

These values produce the same width in a 200 by 200 SVG user coordinate system when the percentage reference is 200 units.

Important Consideration

A percentage is not calculated from the path's length, the element's bounding box, or the width of its stroke. It uses the SVG viewport's normalized diagonal.

Stroke Position and Geometry

By default, an SVG stroke is centered on the path or shape boundary.

Centered Stroke

.centered-outline {
  fill: none;
  stroke: #2563eb;
  stroke-width: 10;
}

A stroke width of 10 generally extends approximately 5 units to either side of the path centerline.

Rectangle Example

<rect x="20" y="20" width="160" height="100" fill="none" stroke="#2563eb" stroke-width="20"/>

The rectangle's stroke extends both inside and outside its geometric boundary.

Stroke Width and the Viewport

Because the stroke extends beyond the path boundary, a thick stroke placed near an SVG viewport edge may be clipped.

Stroke Width and Fill

.filled-shape {
  fill: #dbeafe;
  stroke: #2563eb;
  stroke-width: 12;
}

The stroke is painted around the shape boundary while the fill covers the shape's interior. By default, the stroke is painted over the fill where they overlap.

Important Consideration

Changing stroke-width does not alter the underlying shape geometry. It changes the area occupied by the painted outline.

The stroke-width property works with several other CSS stroke properties to control SVG outline appearance.

Property Purpose
stroke Defines the paint used for the outline.
stroke-width Defines the thickness of the outline.
stroke-linecap Controls the shape of open stroke endpoints.
stroke-linejoin Controls the shape of corners where segments meet.
stroke-miterlimit Limits the length of pointed miter joins.
stroke-dasharray Defines the pattern of dashes and gaps.
stroke-dashoffset Offsets the starting position of a dash pattern.
stroke-opacity Controls stroke transparency.

Rounded Endpoints

.rounded-line {
  stroke: #2563eb;
  stroke-width: 10;
  stroke-linecap: round;
}

Creates a thick line with rounded endpoints.

Rounded Corners

.rounded-path {
  fill: none;
  stroke: #0d9488;
  stroke-width: 12;
  stroke-linejoin: round;
}

Creates a thick outline with rounded corners.

Dashed Stroke

.dashed-outline {
  fill: none;
  stroke: #a855f7;
  stroke-width: 6;
  stroke-dasharray: 12 8;
}

Creates a dashed outline with a consistent stroke width.

Transparent Stroke

.transparent-outline {
  stroke: #2563eb;
  stroke-width: 10;
  stroke-opacity: 0.5;
}

Creates a thick, partially transparent outline.

Scaling SVG Strokes

SVG transformations can affect the apparent thickness of a stroke.

Scaling a Shape

<svg viewBox="0 0 240 140">
  <g transform="scale(1.5)">
    <circle cx="70" cy="45" r="30" fill="none" stroke="#2563eb" stroke-width="6"/>
  </g>
</svg>

The circle and its stroke are scaled together.

Using vector-effect

.non-scaling-outline {
  fill: none;
  stroke: #2563eb;
  stroke-width: 6;
  vector-effect: non-scaling-stroke;
}

The non-scaling-stroke value prevents the stroke thickness from scaling with transformations in the usual way.

Complete Non-Scaling Example

<svg viewBox="0 0 240 140">
  <g transform="scale(1.5)">
    <circle cx="70" cy="45" r="30" fill="none" stroke="#2563eb" stroke-width="6" vector-effect="non-scaling-stroke"/>
  </g>
</svg>

Responsive SVG Graphics

When an SVG is resized through its viewBox and CSS dimensions, strokes normally scale with the SVG content. A non-scaling stroke can help preserve consistent visual thickness.

Important Consideration

Use vector-effect: non-scaling-stroke when stroke thickness should remain stable during SVG scaling. Test the result with the transformations and viewport sizes used by your design.

Animating Stroke Width

The stroke-width property supports CSS transitions and animations, allowing SVG outlines to become thicker or thinner over time.

CSS Transition

.interactive-outline {
  stroke: #2563eb;
  stroke-width: 3;
  transition: stroke-width 0.3s ease;
}
.interactive-outline:hover {
  stroke-width: 10;
}

The outline becomes thicker when the element is hovered.

CSS Keyframe Animation

@keyframes stroke-pulse {
  0% {
    stroke-width: 2;
  }
  50% {
    stroke-width: 10;
  }
  100% {
    stroke-width: 2;
  }
}
.animated-stroke {
  stroke: #0d9488;
  animation: stroke-pulse 2s ease-in-out infinite;
}

The stroke repeatedly changes between thin and thick outlines.

Respect Reduced Motion Preferences

@media (prefers-reduced-motion: reduce) {
  .animated-stroke {
    animation: none;
    stroke-width: 4;
  }
  .interactive-outline {
    transition: none;
  }
}

Disabling unnecessary animations for users who prefer reduced motion improves accessibility.

Important Consideration

Animating stroke width changes the painted outline thickness without modifying the underlying path coordinates.

Practical Examples

Thin SVG Icon

.thin-icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

Creates a lightweight SVG icon with rounded endpoints and corners.

Bold SVG Icon

.bold-icon {
  fill: none;
  stroke: currentColor;
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

Creates a heavier outline for stronger visual emphasis.

Chart Line

.chart-line {
  fill: none;
  stroke: #2563eb;
  stroke-width: 3;
}

Creates a clear line for an SVG chart.

Highlighted Chart Line

.highlighted-chart-line {
  fill: none;
  stroke: #0d9488;
  stroke-width: 6;
}

Uses a thicker stroke to emphasize one data series.

Thick Rectangle Outline

.outlined-rectangle {
  fill: #dbeafe;
  stroke: #2563eb;
  stroke-width: 12;
}

Creates a filled rectangle with a prominent outline.

Dashed Outline

.dashed-shape {
  fill: none;
  stroke: #a855f7;
  stroke-width: 5;
  stroke-dasharray: 10 6;
}

Creates a dashed stroke with consistent thickness.

Inherited Stroke Width

.diagram-group {
  stroke: #475569;
  stroke-width: 4;
}

Applies the same stroke width to descendant SVG elements unless they override it.

Reset Stroke Width

.default-stroke {
  stroke-width: initial;
}

Restores the initial stroke width of 1px.

stroke-width Example

This example compares four identical SVG lines with different stroke widths. Each line uses the same color and coordinates, making the differences in thickness easy to see.

HTML Example

<div class="stroke-width-demo">
  <svg viewBox="0 0 600 240" role="img" aria-label="Four horizontal blue lines with stroke widths of 2, 5, 10, and 20 units">
    <line class="width-2" x1="50" y1="40" x2="550" y2="40"/>
    <line class="width-5" x1="50" y1="90" x2="550" y2="90"/>
    <line class="width-10" x1="50" y1="140" x2="550" y2="140"/>
    <line class="width-20" x1="50" y1="195" x2="550" y2="195"/>
  </svg>
</div>

CSS Example

.stroke-width-demo svg {
  display: block;
  width: 100%;
  height: auto;
}
.stroke-width-demo line {
  stroke: #2563eb;
  stroke-linecap: round;
}
.stroke-width-demo .width-2 {
  stroke-width: 2;
}
.stroke-width-demo .width-5 {
  stroke-width: 5;
}
.stroke-width-demo .width-10 {
  stroke-width: 10;
}
.stroke-width-demo .width-20 {
  stroke-width: 20;
}
Browser Result

The lines become progressively thicker from top to bottom. Their centerline coordinates remain unchanged.

Comparing the Results

Line stroke-width Appearance
First 2 Thin
Second 5 Medium
Third 10 Thick
Fourth 20 Very thick

Using Pixel Values

.stroke-width-demo .width-10 {
  stroke-width: 10px;
}

In this SVG coordinate system, the pixel value produces the same apparent width as the unitless value 10.

Using Percentage Values

.stroke-width-demo .width-10 {
  stroke-width: 2%;
}

The width is calculated from the SVG viewport's normalized diagonal rather than the line's length.

Changing the Line Caps

.stroke-width-demo line {
  stroke-linecap: butt;
}

The lines now have flat endpoints instead of rounded endpoints.

Making All Lines the Same Width

.stroke-width-demo .width-2,
.stroke-width-demo .width-5,
.stroke-width-demo .width-10,
.stroke-width-demo .width-20 {
  stroke-width: 8;
}

All four lines now have the same stroke thickness.

Accessibility Considerations

Stroke thickness can affect the visibility and clarity of SVG icons, diagrams, and other graphics.

Use Sufficient Stroke Width

Very thin strokes may be difficult to distinguish, especially on small displays or when graphics are scaled down.

Maintain Adequate Contrast

Ensure that stroked lines and outlines remain visible against their backgrounds.

Avoid Excessively Thick Strokes

Very thick strokes can obscure details, overlap nearby elements, or make small icons difficult to interpret.

Provide Accessible SVG Names

Informative SVG graphics should include accessible names or equivalent descriptions.

<svg viewBox="0 0 200 120" role="img" aria-label="Blue angled line with a thick rounded stroke">
  <path d="M20 100 L100 20 L180 100" fill="none" stroke="#2563eb" stroke-width="10" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

Avoid Stroke Width as the Only Indicator

Do not rely solely on line thickness to communicate important information. Include labels or other visual indicators when necessary.

Test Responsive Scaling

Verify that strokes remain visible and proportional when SVG graphics are resized or zoomed.

Respect Reduced Motion Preferences

When animating stroke width, provide a reduced-motion alternative for users who request it.

Common Mistakes

Using Negative Values

svg path {
  stroke-width: -5;
}

Stroke widths must be non-negative.

Using Unsupported Keywords

svg path {
  stroke-width: thick;
}

The property does not accept keywords such as thin, medium, or thick.

Forgetting to Define a Stroke

The initial stroke value is none. Increasing stroke-width has no visible effect without a painted stroke.

Confusing Stroke Width With Shape Size

Changing stroke width does not modify the underlying path coordinates or shape dimensions.

Expecting Percentages to Use Path Length

Percentage stroke widths are based on the SVG viewport's normalized diagonal, not the length of the path.

Ignoring Viewport Clipping

Thick strokes can extend outside the SVG viewport and become clipped.

Overlooking Transformations

SVG transformations may scale stroke thickness. Use vector-effect: non-scaling-stroke when appropriate.

Confusing Stroke Width With Border Width

The stroke-width property controls SVG strokes. The CSS border-width property controls CSS box borders.

Using Zero When a Visible Outline Is Needed

A value of 0 removes visible stroke thickness.

Ignoring Line Caps and Joins

Thick strokes make endpoint and corner styles more noticeable. Adjust stroke-linecap and stroke-linejoin when necessary.

Important Considerations

Keep the following points in mind when using stroke-width:

  • It controls the thickness of SVG strokes.
  • The initial value is 1px.
  • The property is inherited.
  • It accepts non-negative numbers, lengths, and percentages.
  • Unitless numbers represent distances in SVG user units.
  • Percentage values use the SVG viewport's normalized diagonal.
  • Negative stroke widths are invalid.
  • A value of 0 produces no visible stroke thickness.
  • A painted stroke is required for a visible result.
  • Increasing stroke width expands the painted outline.
  • It does not modify the underlying path geometry.
  • SVG strokes are normally centered on the path.
  • Thick strokes may extend beyond the SVG viewport.
  • Stroke width interacts with line caps and joins.
  • Sharp miter joins can become more prominent with thicker strokes.
  • Stroke width can be combined with dash patterns.
  • Stroke opacity can be adjusted independently.
  • SVG transformations may affect apparent stroke thickness.
  • The non-scaling-stroke vector effect can preserve stroke thickness during scaling.
  • The property supports CSS transitions and animations.
  • Very thin strokes may be difficult to see.
  • Very thick strokes may obscure details.
  • Test stroke thickness across responsive sizes and supported browsers.

Browser Support

The CSS stroke-width property is widely supported for SVG graphics in modern browsers, including Chrome, Edge, Firefox, and Safari.

Unitless numbers and common length values have broad support. Percentage values and advanced combinations with transformations should be tested in the browsers you support.

For current compatibility information, visit Can I Use? and search for stroke-width.

Feature Detection

@supports (stroke-width: 5px) {
  .svg-outline {
    stroke-width: 5px;
  }
}

Feature queries can test whether a browser recognizes the property and value syntax.

Fallback Behavior

If a declaration is invalid or unsupported, the browser ignores it. The element uses another applicable value from the cascade or its inherited value, falling back to 1px when no other value applies.

Testing Recommendations

Test unitless numbers, length values, percentages, thick strokes, transformations, viewport clipping, and animations in the browsers you support.

Specifications

The stroke-width property is defined in the SVG 2 specification.

SVG 2 - stroke-width Property

The specification defines accepted stroke width values, percentage calculations, inheritance, and how stroke thickness is applied to SVG graphics.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: stroke-width