Link External CSS to SVG Using xml-stylesheet

Incorporating external cascading style sheets into an SVG document allows developers to maintain consistent styling, reuse existing CSS rules, and separate presentation from graphic structure. Because SVG is an XML-based language, standalone SVG files use the standard <?xml-stylesheet ?> processing instruction rather than the HTML <link> element to reference external stylesheets. This guide explains the syntax, placement, attributes, and usage considerations for linking external CSS files to SVG graphics.

The <?xml-stylesheet ?> Syntax

To link an external stylesheet to an SVG document, place the <?xml-stylesheet ?> processing instruction at the very top of the SVG file. It must appear before the opening <svg> root tag. If your file includes an XML declaration (<?xml version="1.0" ... ?>), the stylesheet instruction should immediately follow it.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="style.css" type="text/css"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
  <circle class="accent-circle" cx="50" cy="50" r="40" />
</svg>

In the corresponding style.css file, you can target SVG elements and classes using standard CSS properties:

.accent-circle {
  fill: #007acc;
  stroke: #003366;
  stroke-width: 4px;
  transition: fill 0.3s ease;
}

.accent-circle:hover {
  fill: #ff6600;
}

Supported Pseudo-Attributes

The <?xml-stylesheet ?> processing instruction accepts several pseudo-attributes defined by the W3C XML Stylesheet specification:

Example using media queries:

<?xml-stylesheet href="dark-theme.css" type="text/css" media="(prefers-color-scheme: dark)"?>

Embedding and Rendering Contexts

The <?xml-stylesheet ?> instruction behaves differently depending on how the SVG is embedded into a web page:

  1. Standalone SVG & Direct Navigation: When viewing an SVG directly in a browser or opening it as a standalone file, the processing instruction successfully loads and applies the external stylesheet.
  2. Embedded via <iframe> or <object>: When embedded into an HTML document using <object data="image.svg" type="image/svg+xml"> or <iframe>, the SVG acts as an independent document context, and the <?xml-stylesheet ?> directive will execute normally.
  3. Embedded via <img> or CSS background-image: For security reasons, web browsers disable external resource loading (including external stylesheets, external fonts, and scripts) when an SVG is loaded inside an <img> tag or via CSS. Styles for these SVGs must be embedded directly inside an internal <style> element within the SVG markup itself.
  4. Inline SVG: Inlined <svg> elements inside an HTML document ignore the <?xml-stylesheet ?> directive entirely because they inherit the HTML document’s existing CSS stylesheets directly.

Cross-Origin Restrictions

When hosting external CSS files on a different domain or subdomain than the SVG file, ensure that the stylesheet server sends appropriate Cross-Origin Resource Sharing (CORS) headers, specifically Access-Control-Allow-Origin: *. Browsers enforce strict cross-origin security rules and will block external stylesheets that do not pass CORS validation.