SVG Stroke Attribute
In SVG, the stroke The attribute is used to define the stroke (border) color of a graphic element; it can be applied to any graphic element with an outline, such as , , , etc.
Basic Syntax
Attribute Analysis:
-
strokeThe attribute defines the stroke color of a graphic element, which can be expressed with color names, hexadecimal color values, RGB values, RGBA values, etc. -
If you do not want to display the stroke, you can set the
strokeThe attribute to "none".
SVG provides a wide range of stroke attributes.
- stroke
- stroke-width
- stroke-linecap
- stroke-dasharray
All stroke attributes can be applied to any kind of line, text, and element, like the outline of a circle.
The following code draws a red-filled rectangle with a blue stroke specified.
SVG stroke attribute
The Stroke attribute defines the outline color of a line, text, or element:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
g
fill
=
"
none
"
<
path
stroke
=
"
red
"
d
=
"M5 20 l215 0
"
/>
<
path
stroke
=
"
blue
"
d
=
"M5 40 l215 0
"
/>
<
path
stroke
=
"
black
"
d
=
"M5 60 l215 0
"
/>
g
svg
Try it »
Click to view:View SVG File.
SVG stroke-width attribute
The stroke-width attribute defines the thickness of a line, text, or element outline:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
g
fill
=
"
none
"
stroke
=
"
black
"
<
path
stroke-width
=
"
2
"
d
=
"M5 20 l215 0
"
/>
<
path
stroke-width
=
"
4
"
d
=
"M5 40 l215 0
"
/>
<
path
stroke-width
=
"
6
"
d
=
"M5 60 l215 0
"
/>
g
svg
Try it »
Click to view:View SVG File.
SVG stroke-linecap attribute
The strokelinecap attribute defines the endings of different types of open paths:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
g
fill
=
"
none
"
stroke
=
"
black
"
stroke-width
=
"
6
"
<
path
stroke-linecap
=
"
butt
"
d
=
"M5 20 l215 0
"
/>
<
path
stroke-linecap
=
"
round
"
d
=
"M5 40 l215 0
"
/>
<
path
stroke-linecap
=
"
square
"
d
=
"M5 60 l215 0
"
/>
g
svg
Try it »
Click to view:View SVG File.
SVG stroke-dasharray attribute
The strokedasharray attribute is used to create dashed lines:
Below is the SVG code:
Example
<
svg
xmlns
=
"
http://www.w3.org/2000/svg
"
version
=
"
1.1
"
<
g
fill
=
"
none
"
stroke
=
"
black
"
stroke-width
=
"
4
"
<
path
stroke-dasharray
=
"
5,5
"
d
=
"M5 20 l215 0
"
/>
<
path
stroke-dasharray
=
"
10,10
"
d
=
"M5 40 l215 0
"
/>
<
path
stroke-dasharray
=
"
20,10,5,5,5,10
"
d
=
"M5 60 l215 0
"
/>
g
svg
Try it »
Click to view:View SVG File.
Notes
-
If you want to set the stroke color and width at the same time, you can use the
stroke-widthThe attribute to define the width of the stroke. -
can be used via
stroke-opacityThe attribute to define the opacity of the stroke. -
The stroke color and width can be set via a CSS stylesheet, or directly on the element using the
styleattribute.
By setting the stroke attribute, you can add a stroke to SVG graphic elements, making them more visually effective and recognizable.