<use>
The<use>
element takes nodes from within the SVG document, and duplicates them somewhere else.
The effect is the same as if the nodes were deeply cloned into a non-exposed DOM, then pasted where theuse
element is, much like clonedtemplate elements.
Example
The following example shows how to use theuse
element to draw a circle with a different fill and stroke color.
In the last circle,stroke= "red"
will be ignored because stroke was already set onmyCircle
.
<svgviewBox="0 0 30 10"xmlns="http://www.w3.org/2000/svg">
<circleid="myCircle"cx="5"cy="5"r="4"stroke="blue"/>
<usehref="#myCircle"x="10"fill="blue"/>
<usehref="#myCircle"x="20"fill="white"stroke="red"/>
</svg>
Attributes
href
-
The URL to an element/fragment that needs to be duplicated. SeeUsage notesfor details on common pitfalls.
Value type:<URL>
;Default value:none;Animatable:yes xlink:href
Deprecated-
An
<IRI>
reference to an element/fragment that needs to be duplicated. If bothhref
andxlink:href
are present, the value given byhref
is used.
Value type:<IRI>
;Default value:none;Animatable:yes x
-
The x coordinate of an additional final offset transformation applied to the
<use>
element.
Value type:<coordinate>
;Default value:0
;Animatable:yes y
-
The y coordinate of an additional final offset transformation applied to the
<use>
element.
Value type:<coordinate>
;Default value:0
;Animatable:yes width
-
The width of the use element.
Value type:<length>
;Default value:0
;Animatable:yes height
-
The height of the use element.
Value type:<length>
;Default value:0
;Animatable:yes
Note:width
,andheight
have no effect onuse
elements, unless the element referenced has aviewBox- i.e. they only have an effect whenuse
refers to asvg
orsymbol
element.
Note:Starting with SVG2,x
,y
,width
,andheight
areGeometry Properties,meaning those attributes can also be used as CSS properties for that element.
Usage notes
Most attributes onuse
are ignored if the corresponding attribute is already defined on the elementreferencedbyuse
.(This differs from how CSS style attributes override those set 'earlier' in the cascade).
Onlythe attributesx
,y
,width
,height
andhref
on theuse
element will or may have some effect, described later, if the referenced element has already defined the corresponding attribute. However,any other attributesnot set on the referenced elementwillbe applied to theuse
element.
Since the cloned nodes are not exposed, care must be taken when usingCSSto style ause
element and its cloned descendants. CSS properties are not guaranteed to be inherited by the cloned DOM unless you explicitly request them usingCSS inheritance.
For security reasons, browsers may apply thesame-origin policyonuse
elements and may refuse to load a cross-origin URL in thehref
attribute. There is currently no defined way to set a cross-origin policy foruse
elements.
Warning:Loading resources with data URIs in thehref
attribute is deprecated for security reasons.
This applies to<use href= "data:...
and also when settinghref
by using theset
orsetAttribute
method.
See "Load from data: URI" in theBrowser compatibilitytable to check support in different browser versions.
Warning:Since SVG 2, thexlink:href
attribute is deprecated in favor ofhref
.Seexlink:href
page for more information.
Usage context
Categories | Graphics element, Graphics referencing element, Structural element |
---|---|
Permitted content | Any number of the following elements, in any order: Animation elements Descriptive elements |
Specifications
Specification |
---|
Scalable Vector Graphics (SVG) 2 #UseElement |
Browser compatibility
BCD tables only load in the browser