<foreignObject>

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

<foreignObject> SVG 元素可以包含来自不同 XML 命名空间的内容。在浏览器环境中,这通常是 (X)HTML。

使用语境

分类图形元素,可渲染元素
允许内容任何元素或字符数据

属性

height

foreignObject 的高度。值类型<length> | <percentage>默认值auto可动画

width

foreignObject 的宽度。值类型<length> | <percentage>默认值auto可动画

x

foreignObject 的 x 坐标。值类型<length> | <percentage>默认值0可动画

y

foreignObject 的 y 坐标。值类型<length> | <percentage>默认值0可动画

注意:从 SVG2 开始,xywidthheight几何属性,这意味着这些属性也可以作为该元素的 CSS 属性来使用。

DOM 接口

此元素实现了 SVGForeignObjectElement 接口。

示例

html
<svg viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
  <style>
    div {
      color: white;
      font: 18px serif;
      height: 100%;
      overflow: auto;
    }
  </style>

  <polygon points="5,5 195,10 185,185 10,195" />

  <!-- Common use case: embed HTML text into SVG -->
  <foreignObject x="20" y="20" width="160" height="160">
    <!--
      In the context of SVG embedded in an HTML document, the XHTML
      namespace could be omitted, but it is mandatory in the
      context of an SVG document
    -->
    <div xmlns="http://www.w3.org/1999/xhtml">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed mollis mollis
      mi ut ultricies. Nullam magna ipsum, porta vel dui convallis, rutrum
      imperdiet eros. Aliquam erat volutpat.
    </div>
  </foreignObject>
</svg>

规范

规范
Scalable Vector Graphics (SVG) 2
# ForeignObjectElement

浏览器兼容性