{{SVGRef}}
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 the use
element is, much like cloned template elements.
Example
The following example shows how to use the use
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 on myCircle
.
html,
body,
svg {
height: 100%;
}
<svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
<circle id="myCircle" cx="5" cy="5" r="4" stroke="blue" />
<use href="#myCircle" x="10" fill="blue" />
<use href="#myCircle" x="20" fill="white" stroke="red" />
</svg>
{{EmbedLiveSample('Example', 200, 200)}}
Attributes
{{SVGAttr("href")}}
- : The URL to an element/fragment that needs to be duplicated. See Usage notes for details on common pitfalls.
Value type:<URL>
; Default value: none; Animatable: yes
- : The URL to an element/fragment that needs to be duplicated. See Usage notes for details on common pitfalls.
{{SVGAttr("xlink:href")}}
{{Deprecated_Inline}}
{{SVGAttr("x")}}
- : The x coordinate of an additional final offset transformation applied to the
<use>
element.
Value type:<coordinate>
; Default value:0
; Animatable: yes
- : The x coordinate of an additional final offset transformation applied to the
{{SVGAttr("y")}}
- : The y coordinate of an additional final offset transformation applied to the
<use>
element.
Value type:<coordinate>
; Default value:0
; Animatable: yes
- : The y coordinate of an additional final offset transformation applied to the
{{SVGAttr("width")}}
- : The width of the use element.
Value type:<length>
; Default value:0
; Animatable: yes
- : The width of the use element.
{{SVGAttr("height")}}
- : The height of the use element.
Value type:<length>
; Default value:0
; Animatable: yes
- : The height of the use element.
Note:
width
, andheight
have no effect onuse
elements, unless the element referenced has a viewBox - i.e. they only have an effect whenuse
refers to asvg
orsymbol
element.
[!NOTE] Starting with SVG2,
x
,y
,width
, andheight
are Geometry Properties, meaning those attributes can also be used as CSS properties for that element.
Usage notes
Most attributes on use
are ignored if the corresponding attribute is already defined on the element referenced by use
. (This differs from how CSS style attributes override those set ‘earlier’ in the cascade).
Only the attributes {{SVGAttr("x")}}
, {{SVGAttr("y")}}
, {{SVGAttr("width")}}
, {{SVGAttr("height")}}
and {{SVGAttr("href")}}
on the use
element will or may have some effect, described later, if the referenced element has already defined the corresponding attribute. However, any other attributes not set on the referenced element will be applied to the use
element.
Since the cloned nodes are not exposed, care must be taken when using CSS to style a use
element and its cloned descendants. CSS properties are not guaranteed to be inherited by the cloned DOM unless you explicitly request them using CSS inheritance.
For security reasons, browsers may apply the same-origin policy on use
elements and may refuse to load a cross-origin URL in the {{SVGAttr("href")}}
attribute. There is currently no defined way to set a cross-origin policy for use
elements.
[!WARNING] Loading resources with data URIs in the
href
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 the Browser compatibility table to check support in different browser versions.
[!WARNING] Since SVG 2, the
{{SVGAttr("xlink:href")}}
attribute is deprecated in favor of{{SVGAttr("href")}}
. See{{SVGAttr("xlink:href")}}
page for more information.
Usage context
{{SVGInfo}}
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}