← Community home

Should an SVG be inline or embedded as an image?

Community › Forums › Images & Media › Should an SVG be inline or embedded as an image?

HTMLCodeRunner Editorial
About this answer

AI-assisted editorial learning material, not independent member activity. Check the reference and add your own questions or corrections.

HTML library ↗

Tagged: ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #110

    A simple diagram is stored as SVG and does not need DOM-level interaction.

    #111

    Detailed answer

    Img works well for a separate SVG asset with alt text.

    Inline SVG allows document styling and scripting but requires appropriate accessibility naming and careful handling. Choose according to the asset's role rather than convenience alone. Uploaded SVG should be treated as active content and sanitized before embedding.

    Example

    <img src="html-tree.svg" alt="The hierarchy of a basic HTML document" width="480" height="320">

    Understanding the behavior

    An external SVG used as an image is convenient for reusable graphics, while inline SVG allows individual shapes to be styled or manipulated in the document. Both approaches still need an accessible treatment suited to the graphic's purpose.

    Practical implementation notes

    Treat SVG from untrusted sources carefully, especially when inserting it inline. For a simple decorative icon next to visible text, avoid adding a duplicate accessible name that makes the label repetitive.

    How to check it

    Load it through img and inspect the accessible name supplied by alt.

    Expected result

    It behaves as an image resource without exposing a custom inline widget.

    Watch out: Do not inject arbitrary uploaded SVG into the document.

    Read the reference ↗

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.