Tutorial: Extracting SVG Graphics from Websites: The Console Method and Alternatives
Extracting scalable vector graphics (SVGs) from websites can be challenging, especially when the graphics are styled using external CSS stylesheets. Standard copying methods often result in the loss of colors and formatting. The following guide details a JavaScript-based method to extract SVGs with fully baked-in styles, along with alternative approaches and instructions for using an SVG as a website favicon.
Method 1: The Developer Console Script
This method utilizes the browser’s Developer Console. The script targets an SVG, clones it, explicitly applies the computed CSS styles (such as fill, stroke, and opacity) to the cloned elements, and automatically triggers a download of the complete file.
- Open the Developer Tools (F12 or Ctrl+Shift+I).
- Navigate to the Console tab.
- Paste the following code and press Enter:
// 1. Target your SVG element (modifying the selector might be required if multiple SVGs exist)
const svgElement = document.querySelector("svg");
// 2. Clone the element to avoid altering the live webpage
const clonedSvg = svgElement.cloneNode(true);
// 3. Find all elements that hold color styles (paths, circles, shapes)
const sourceElements = svgElement.querySelectorAll("*");
const clonedElements = clonedSvg.querySelectorAll("*");
// 4. Copy computed styles (colors, fills, strokes) to the cloned SVG
sourceElements.forEach((sourceEl, index) => {
const clonedEl = clonedElements[index];
const computedStyle = window.getComputedStyle(sourceEl);
// Explicitly copy color and layout definitions
clonedEl.style.fill = computedStyle.fill;
clonedEl.style.stroke = computedStyle.stroke;
clonedEl.style.color = computedStyle.color;
clonedEl.style.opacity = computedStyle.opacity;
});
// 5. Serialize the cloned SVG with baked-in styles
const svgString = new XMLSerializer().serializeToString(clonedSvg);
const svgBlob = new Blob([svgString], { type: "image/svg+xml;charset=utf-8" });
// 6. Download the file
const downloadLink = document.createElement("a");
downloadLink.href = URL.createObjectURL(svgBlob);
downloadLink.download = "colored-image.svg";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
Alternative Methods for SVG Extraction
If extracting computed styles is not strictly necessary, other methods can be utilized:
- Inspect Element (Copy outerHTML): Right-click the SVG on the webpage, select Inspect, right-click the highlighted
<svg>tag in the DOM tree, and select Copy > Copy outerHTML. This code can then be pasted into a local .svg file. - Network Tab: Open Developer Tools, navigate to the Network tab, filter by Img, and reload the page. SVGs loaded as external files will appear here and can be downloaded directly.
- Browser Extensions: Specialized extensions like “SVG Grabber” can automatically scrape and download all SVG assets present on a webpage.
How to Use an SVG as a Favicon
Implementing an SVG file as a website favicon requires adding a specific link tag to the HTML document. This approach provides infinite scalability and crisp rendering on high-DPI displays.
Insert the following code into the <head> section of the HTML document:
<link rel="icon" href="/path/to/favicon.svg" type="image/svg+xml">
For broader compatibility, it is often recommended to include a fallback standard .ico file alongside the SVG declaration.