Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SVG Stroke Attribute

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:

SVG provides a wide range of stroke attributes.

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

By setting the stroke attribute, you can add a stroke to SVG graphic elements, making them more visually effective and recognizable.