Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. SVG
  3. Reference
  4. Elements
  5. <use>

<use>

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

* Some parts of this feature may have varying levels of support.

The<use> element takes nodes from within an 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.

Usage context

CategoriesGraphics element, Graphics referencing element, Structural element
Permitted contentAny number of the following elements, in any order:
Animation elements
Descriptive elements

Attributes

href

The URL to an element/fragment that needs to be duplicated. SeeUsage notes for details on common pitfalls.
Value type:<URL>;Default value: none;Animatable:yes

xlink:hrefDeprecated

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

Warning:Since SVG 2, thexlink:href attribute is deprecated in favor ofhref. Seexlink:href page for more information.

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 on<use> elements, unless the element referenced has aviewBox - i.e., they only have an effect when<use> refers to a<svg> or<symbol> element.

Note:Starting with SVG2,x,y,width, andheight areGeometry Properties, meaning those attributes can also be used as CSS properties for that element.

DOM Interface

This element implements theSVGUseElement interface.

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 onmyCircle.

html,body,svg {  height: 100%;}
html
<svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">  <circle 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>

Usage notes

Most attributes on<use> are ignored if the corresponding attribute is already defined on the elementreferenced by<use>. (This differs from how CSS style attributes override those set 'earlier' in the cascade).Only the attributesx,y,width,height andhref 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 elementwill be applied to the<use> element.

Since the cloned nodes are not exposed, care must be taken when usingCSS 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 usingCSS inheritance.

For security reasons, browsers may apply thesame-origin policy on<use> 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 for<use> elements.

Loading resources from external files via<use>

You can load nodes from an external SVG file via the<use> element by specifying the path of the file followed by a URL fragment pointing to theid of the node to load:

html
<svg>  <use href="../assets/my-svg.svg#my-fragment"></use></svg>

Historically, the URL fragment was always required, even if you just wanted to load the entire SVG document. In such a case, theid would be included on the SVG root element:

html
<svg xmlns="http://www.w3.org/2000/svg">  <circle cx="150" cy="100" r="80" fill="green" /></svg>

However, modern implementations have been updated so that if you want to load the entire external document, you can refer to it without a URL fragment (and theid is no longer needed on the SVG document root element):

html
<svg>  <use href="../assets/my-svg.svg"></use></svg>

Check theBrowser compatibility table for browser support.

Loading resources from data URIs via<use>

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.

Again, check theBrowser compatibility table for browser support.

Specifications

Specification
Scalable Vector Graphics (SVG) 2
# UseElement

Browser compatibility

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp