ποΈ CSS stroke β Complete Tutorial
The CSS stroke property is used to color and style theoutline of SVG shapes, such as icons, paths, circles, rectangles, and text. While fill paints the inside, stroke paints the border or edges of the shape.
>>βIf fill paints the inside of an SVG, stroke draws the lines that define its shape.β
Note
β Works only on SVG elements (paths, shapes, text)
β Perfect for outline icons, borders, loaders, charts
β Often used together with stroke-width and stroke-linejoin
β Perfect for outline icons, borders, loaders, charts
β Often used together with stroke-width and stroke-linejoin
π What Elements Can Use stroke?
- <path>
- <circle>
- <rect>
- <line>
- <polyline>
- <polygon>
- <text> (SVG text outline)
π¦ Syntax
Basic Syntax
stroke: <color> | none | currentColor;π― Common Values
| Value | Meaning |
|---|---|
| stroke: red; | Sets a red outline |
| stroke: none; | Removes outline |
| stroke: currentColor; | Inherits parent color β great for icons |
π¨ 1. Basic Example β Outline a Shape
Basic stroke example
svg path {
stroke: #3498db;
stroke-width: 2;
}π¨ 2. Outline Icon Using currentColor
This makes the SVG automatically match its parentβs text color.
Themed outline icon
.icon {
color: purple;
}
.icon svg {
stroke: currentColor;
stroke-width: 2;
}π¨ 3. Remove Stroke
Remove outline
svg circle {
stroke: none;
}π¨ 4. Gradient Stroke
Gradient stroke
svg path {
stroke: url(#myGradient);
stroke-width: 4;
}π 5. Style Stroke Appearance
β€ stroke-width
stroke-width
svg path {
stroke-width: 4;
}β€ stroke-linecap
Controls how stroke ends look.
stroke-linecap
svg line {
stroke-linecap: round; /* butt | round | square */
}β€ stroke-linejoin
Controls corner shape of strokes.
stroke-linejoin
svg polyline {
stroke-linejoin: bevel; /* miter | round | bevel */
}β€ stroke-dasharray
Creates dashed lines.
stroke-dasharray
svg path {
stroke-dasharray: 5 3; /* dash length | gap length */
}β€ stroke-dashoffset
Offsets the start of dashes β used in animations.
stroke-dashoffset
svg path {
stroke-dashoffset: 20;
}π§ͺ Real-World Use Cases
1οΈβ£ Outline-Only Icons
Outline icon
.outline-icon svg {
stroke: currentColor;
fill: none;
stroke-width: 2;
}2οΈβ£ Animated SVG Loader
SVG loader
circle {
stroke: #ff4757;
stroke-width: 6;
stroke-dasharray: 90;
animation: spin 2s linear infinite;
}
@keyframes spin {
to {
stroke-dashoffset: -180;
}
}3οΈβ£ Chart Visualization
Line chart
polyline {
fill: none;
stroke: #2ecc71;
stroke-width: 3;
}β οΈ Common Mistakes
- β Trying to apply stroke on HTML elements (div, p, span)
- β Using stroke on external SVG images (must be inline)
- β Forgetting to pair with stroke-width for visibility
- β Using fill instead of stroke for outline icons
Note
To style stroke, the SVG must be inline or use an exposed <use> element.
π₯ Summary
The stroke property is essential for drawing outlines in SVG graphics. It powers icons, loaders, charts, and any vector-based UI component.
>>βStroke draws the shape. Fill completes it. Master both to master SVG.β