SVG Image File Shows XML Code Instead Of Image
Categories:
SVG Image Files Displaying as XML Code? Here's How to Fix It

Discover why your SVG files might be rendering as raw XML code instead of images and learn practical solutions, including server configuration and proper embedding techniques.
Scalable Vector Graphics (SVG) are powerful, resolution-independent image formats perfect for web use. However, a common frustration for developers is when an SVG file, instead of displaying as an image, renders as a block of raw XML code directly in the browser. This issue typically stems from incorrect server configuration or improper embedding methods, preventing the browser from interpreting the file as an image. This article will guide you through the common causes and provide actionable solutions to ensure your SVGs display correctly.
Understanding the Root Cause: MIME Types
The primary reason browsers display SVG files as XML code is often due to an incorrect or missing MIME type being sent by the web server. When a browser requests a file, the server sends a Content-Type
header along with the file's data. This header tells the browser what kind of file it's receiving. For SVG files, the correct MIME type is image/svg+xml
. If the server sends text/xml
, application/xml
, or no Content-Type
header at all, the browser defaults to displaying the file's raw content, which for an SVG, is its underlying XML structure.
flowchart TD A[Browser Requests SVG] --> B{Web Server Responds} B -->|Content-Type: image/svg+xml| C[Browser Renders SVG Image] B -->|Content-Type: text/xml OR Missing| D[Browser Displays Raw XML Code] C -- Optimal Flow --> E[User Sees Image] D -- Problematic Flow --> F[User Sees Code]
Flowchart illustrating how MIME types affect SVG rendering in a browser.
Server Configuration: The .htaccess File
For Apache web servers, the most common solution involves configuring the .htaccess
file to explicitly tell the server to send the correct MIME type for SVG files. This ensures that any SVG file served from that directory (and its subdirectories) will be accompanied by the image/svg+xml
header. This is a crucial step for many hosting environments.
<IfModule mod_mime.c>
AddType image/svg+xml .svg .svgz
AddEncoding gzip .svgz
</IfModule>
Adding SVG MIME type to an Apache .htaccess file.
.htaccess
file, it's a good practice to clear your browser's cache and try reloading the page. Sometimes, browsers cache incorrect Content-Type
headers, leading to persistent issues even after the server is correctly configured.Alternative Embedding Methods and Considerations
While server configuration is key, how you embed the SVG into your HTML can also play a role. Directly linking to an SVG file via an <img>
tag relies heavily on the server's MIME type. Using <object>
, <iframe>
, or embedding the SVG inline (<svg>...</svg>
) offers more control or different rendering behaviors.
Using
tag
Using
Inline SVG
<svg>...</svg>
) bypasses server MIME type issues entirely, as the SVG code is part of the HTML document itself. This is often the most robust solution for smaller, critical SVGs, but can make caching and content management more complex for larger assets.Troubleshooting Steps
If you've applied the .htaccess
fix and your SVGs are still showing as XML, here are some additional troubleshooting steps:
1. Verify MIME Type with Developer Tools
Open your browser's developer tools (usually F12), go to the 'Network' tab, and reload the page. Find the request for your SVG file and check its 'Headers' tab. Look for the Content-Type
header. It should be image/svg+xml
.
2. Check Server Configuration Location
Ensure your .htaccess
file is in the correct directory (e.g., the root of your website or the directory containing the SVGs). Some servers might have global configurations that override local .htaccess
settings.
3. Restart Web Server (if applicable)
If you have direct access to your server, a restart of Apache or Nginx might be necessary for changes to take effect, especially if you modified global configuration files instead of .htaccess
.
4. Test with Different Browsers
Occasionally, browser-specific caching or extensions can interfere. Test your SVG display in multiple browsers to rule out client-side issues.