Choose the embedding method based on what the SVG needs to do. Use inline <svg> when page CSS or JavaScript must reach its individual shapes; use <img> for a normal, cacheable image with straightforward alternative text; and use <object> or <iframe> when the SVG should be a separate document. Use a CSS background for decoration that does not need its own text alternative.
Choose the right way to embed SVG
SVG can be included as markup inside the page, loaded as an image, or opened as a separate document. The choice affects styling, scripting, accessibility, caching, fallback behavior, and isolation.
| Method | Use it when | Important trade-off |
|---|---|---|
Inline <svg> |
Page CSS or JavaScript needs access to SVG elements, such as paths or groups. | The markup becomes part of the HTML, and the SVG is not cached as a standalone image resource. |
<img> |
You need a conventional, non-interactive image such as a logo or illustration. | Provide useful alt text; page JavaScript and SVG links are unavailable in image mode. |
<object> |
You need a separate SVG document and want to provide fallback content. | It has a separate document context; scripting and interaction follow document and security rules. |
<iframe> |
You want a separate embedded document, particularly when sandboxing matters. | Cross-origin page scripts cannot directly access the embedded document’s DOM. |
<embed> |
You need a general or legacy external-content embedding mechanism. | It is a separate-document method; give it an accessible label. |
| CSS background | The SVG is decorative and does not need to be announced as content. | It has no normal image alternative text, and image-mode restrictions apply. |
Use an external SVG with an image element
For a graphic that is content but does not need direct interaction with its internal shapes, <img> is usually the simplest choice. The browser loads the SVG as an image resource, and you can provide alternative text and dimensions in the HTML.
<img
src="logo.svg"
alt="Acme home page"
width="160"
height="40"
>
Write the alt text to convey the image’s purpose in context. If the logo links to a homepage, for example, “Acme home page” describes the destination rather than merely saying “logo.” For a purely decorative image, use an empty alt="" so assistive technology can skip it.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstall#1 Best Overall
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Set width and height when you need predictable layout sizing, especially if the file lacks an intrinsic aspect ratio or if reserving space before the image loads matters. Values in the markup express the intended rendered size; CSS can still control presentation.
This mode is well suited to reusable assets: browsers can cache the SVG as a separate resource, and the page HTML stays compact. The trade-off is that page CSS and JavaScript cannot reach the SVG’s internal elements in the way they can with inline markup. Scripts and links inside an SVG loaded as an image do not run or activate.
Inline SVG when CSS or JavaScript needs the shapes
Put the SVG markup directly in the HTML when you want to style individual parts from the page, change attributes with JavaScript, or build animation and stateful graphics. A viewBox defines the coordinate system the graphic uses as it scales.
<svg
viewBox="0 0 100 100"
role="img"
aria-labelledby="chart-title chart-desc"
>
<title id="chart-title">Quarterly sales trend</title>
<desc id="chart-desc">A line rises from Q1 through Q4.</desc>
<path d="M10 80 L35 60 L60 65 L90 20" fill="none" stroke="currentColor" />
</svg>
When the SVG needs an accessible name, put <title> immediately after the opening <svg> and associate the title with aria-labelledby. Add a <desc> when a longer explanation is useful, and include its ID in aria-labelledby as shown. If visible text already explains the graphic, avoid making assistive technology announce the same information twice.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Rank #2
Inline SVG’s access to the host page is also a security consideration: it shares the page’s HTML context rather than being processed in image mode. Include only markup you trust. It also increases the HTML payload when the same graphic is repeated, and it is not independently cached as an image file. For reusable assets that do not need DOM access, an external image is often a better fit.
Load SVG as a separate document
Use <object> for a document with fallback content
An object loads the SVG as an external document. Its content can include fallback markup, which is used if the embedded resource cannot be displayed.
<object
type="image/svg+xml"
data="diagram.svg"
width="500"
height="300"
>
<img src="diagram-fallback.png" alt="Process diagram">
</object>
The fallback here is a raster image with its own alternative text. The SVG itself remains a separate document, so do not assume it behaves like inline markup; interaction and scripting depend on document and browser security rules.
Use <iframe> for a framed document
An iframe embeds an SVG document in a browsing context with frame attributes such as a title and sandboxing.
Free tools Windows power users keep installed
One-click scans. No signup required.
<iframe
src="diagram.svg"
width="500"
height="300"
title="Process diagram"
sandbox
></iframe>
The title gives the frame an accessible label. The empty sandbox attribute applies restrictions to the embedded context; do not remove or loosen those restrictions without deciding which capabilities the content actually needs. Cross-origin restrictions prevent page JavaScript from directly manipulating an iframe’s DOM.
Use <embed> only when its behavior fits
An embed can load external SVG content, but its document-context behavior differs from <img>. Supply an accessible label, and choose it only when this form of external-content embedding suits the surrounding application.
<embed
src="diagram.svg"
type="image/svg+xml"
width="500"
height="300"
title="Process diagram"
>
Use SVG as a CSS background for decoration
A background is appropriate for a decorative illustration, texture, or ornament that does not need a standalone accessible name or interactive SVG elements.
.hero {
background-image: url("graphic.svg");
}
Because a CSS background has no normal image alternative text, do not make it the only way to communicate meaningful information. Put equivalent information in page text or use an image or inline SVG with an appropriate accessible name. Scripts and links in the background SVG do not run or activate.
Rank #4
How styling, scripting, security, and caching differ
- Page CSS and JavaScript: Inline SVG elements belong to the host document and can be targeted directly. An SVG in image or background mode does not expose its internal elements for normal page scripting or styling. Separate-document methods have their own document context and security rules.
- Cross-origin access: A page cannot directly manipulate the DOM of a cross-origin iframe. Treat external embedded documents as isolated unless the applicable browser security rules permit access.
- Scripts and links: In
<img>and CSS background contexts, SVG is processed as an image: scripts do not run and links are not activated. Separate-document embeddings are not image mode; browser restrictions, same-origin policy, and sandboxing affect what can execute or be reached. - Accessibility: Give an informative image meaningful
alttext. For an informative inline SVG, provide a title and, when needed, a description associated througharia-labelledby. Label embedded frames and treat backgrounds as decorative unless equivalent text is available. - Payload and reuse: Inline markup adds to the page HTML and is not cached as a standalone image resource. External image files can be cached by the browser as resources.
- Fallbacks and sizing:
<object>can contain fallback markup. Image dimensions, object dimensions, and iframe dimensions help reserve predictable space; use an accessible text alternative in the form that matches the method.
Practical decision checklist
- Ask whether the SVG’s internal shapes need page CSS or JavaScript. If yes, inline it.
- If it is a normal image, use
<img>and decide whether itsalttext should describe content, communicate purpose, or be empty because it is decorative. - If it needs a separate document context, choose
<object>when fallback content is useful, or an iframe when frame isolation and sandbox controls are relevant. - Use a CSS background only when the SVG is decorative and its absence would not remove information from the page.
- Set useful dimensions and check how the result is announced and behaves at the actual embedding location.
Or skip the browser setup
If you need a screenshot of a page that contains an embedded SVG, ScreenshotNeo can capture the page through one API request. For example, this cURL request saves a WebP screenshot of a page you specify:
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. ScreenshotNeo accepts cookie and consent banners as a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000.
Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month without a card.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshooting SVG embeds
The SVG looks blank or has the wrong size
Check the file path or URL, and confirm that the SVG loads. For inline SVG, make sure the opening element has a usable viewBox and that the SVG has a rendered size from its attributes or CSS. For external embeds, set dimensions on the embedding element and confirm that the resource is available at the referenced address.
CSS or JavaScript cannot change a path inside the SVG
If the SVG is loaded through <img> or a CSS background, its internal nodes are not available to page scripts or ordinary page styling. Inline the SVG when direct access is required. If it is in an iframe or another separate-document method, remember that cross-origin rules may block access even when the embedded SVG itself loads.
Best Value
The SVG script or link does not run from an image
That is expected for SVG used in <img> or CSS background mode: scripts do not run and links are not activated. Choose inline SVG or an appropriate separate-document method only if interactive behavior is needed, and account for its security implications.
The image is missing from assistive technology
For <img>, check that the alt attribute reflects its role. For inline SVG, verify that its title and optional description IDs match the values in aria-labelledby. For an iframe or embed, provide a useful label. A CSS background is not a substitute for an accessible content image.
The graphic has no useful fallback
Use <object> when you want to put fallback content between its opening and closing tags. Choose fallback content that communicates the same essential information, such as a raster version with suitable alternative text.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Frequently Asked Questions
Can I use an SVG file directly as an HTML image?
Yes. Use an <img src="graphic.svg" alt="..."> element for a standalone graphic that does not need page-level access to its internal SVG elements.
Which method is best if an SVG must respond to page state?
Use inline SVG when page CSS or JavaScript must target individual SVG elements. External-document methods have separate-context and security constraints.
Is an SVG background accessible by itself?
A CSS background does not provide normal image alternative text. Use it for decoration, or provide an equivalent elsewhere in the page.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

