Skip to content

Svg ​

Embed raw SVG content as a nested element. Use to embed third-party SVGs or complex vector graphics that don't need boundsvg's text measurement.

No children allowed.

Props ​

PropTypeRequiredDefaultDescription
contentstringYes—Raw SVG string content
widthnumberYes—Display width in px
heightnumberYes—Display height in px
preserveAspectRatio"none" | "meet" | "slice""meet"How to fit the SVG content into the display box
contentIdPrefixstring—Prefix exact embedded IDs and supported same-document references; see ID rewriting
opacitynumber—Opacity (0–1)
zIndexnumber—Sibling-local paint order (integer; higher paints later)
metaRecord<string, string>—Metadata emitted as data-boundsvg-meta-* attributes and into the layered manifest (max 16 keys, [a-z][a-z0-9-]{0,31} keys, 256-char values)
transformTransform2D—Static post-layout paint transform
animateAnimationSpec—Declarative opacity/transform track; see Animation

Flex Item ​

PropTypeDefaultDescription
flexGrownumber0Flex grow factor
flexShrinknumber1Flex shrink factor
flexBasisnumber | "auto""auto"Flex basis
alignSelf"auto" | "start" | "center" | "end" | "stretch""auto"Self alignment

Grid Item ​

PropTypeDescription
gridColumnstringGrid column placement (e.g. "1 / 3")
gridRowstringGrid row placement (e.g. "1 / 2")

Positioning ​

PropTypeDefaultDescription
position"relative" | "absolute""relative"Positioning mode
topnumber—Top offset in px
rightnumber—Right offset in px
bottomnumber—Bottom offset in px
leftnumber—Left offset in px

Box Model ​

PropTypeDescription
marginnumber | [number, number, number, number]Margin (uniform or [top, right, bottom, left])

Event / Identity ​

PropTypeDescription
onClickstringClick handler reference
onDoubleClickstringDouble-click handler reference
onContextMenustringContext menu handler reference
onPointerMovestringPointer move handler reference
onPointerEnterstringPointer enter handler reference
onPointerLeavestringPointer leave handler reference
idstringStable NodeId for hit-testing
layerstringLayer id for Layered Export. Always rendered as an atomic island

All 21 event handler props are supported. See Event Handlers for the full list.

Example ​

tsx
const mapSvg = fs.readFileSync("world-map.svg", "utf-8");

<Canvas width={800} height={400}>
  <Svg
    content={mapSvg}
    width={800}
    height={400}
    preserveAspectRatio="meet"
    contentIdPrefix="map-"
  />
</Canvas>;

ID rewriting ​

Use a non-empty contentIdPrefix when embedding multiple SVG sources whose internal IDs could collide. The rewriter preserves the original attribute order, quotes, whitespace, comments, CDATA, text, and entity spelling. It changes only exact id attribute values and supported references to IDs defined in the same embedded SVG.

Supported references are:

  • fragment-only href and xlink:href values;
  • url(#id) in clip-path, color-profile, cursor, fill, filter, marker, marker-start, marker-mid, marker-end, mask, stroke, and style attributes;
  • url(#id) and flat ID selectors in a <style> block;
  • WAI-ARIA single-ID attributes (aria-activedescendant, aria-details, aria-errormessage) and ID-list attributes (aria-controls, aria-describedby, aria-flowto, aria-labelledby, aria-owns);
  • SMIL begin and end timing lists using syncbase, eventbase, repeat(), marker(), deprecated id(...), offsets, or escaped ID values.

External references and references to unknown IDs remain unchanged. Attribute names are matched exactly, so values in data-id, data-href, comments, CDATA, metadata text, and similarly named attributes are not rewritten. Class names, custom properties, and authored keyframe names are outside this namespace.

With a non-empty prefix, malformed start-tag/declaration syntax and ambiguous supported syntax that points to a known local ID fail before output with CONTENT_ID_PREFIX_MALFORMED_XML or CONTENT_ID_PREFIX_UNSUPPORTED_REFERENCE. CSS block at-rules and nested CSS blocks are unsupported. Omitting the prefix, or passing an empty string, keeps the existing pass-through behavior.

Use analyzeEmbeddedSvgIds() from @boundsvg/core/svg to inspect exact definitions, duplicates, supported reference kinds, their source attributes and syntax, and unresolved IDs before rendering. The analyzer applies the same structural safety boundary and reports failures at the analyzer stage.