Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darkworks/0dd50b45b05a20eda8b4d249be3be0b8 to your computer and use it in GitHub Desktop.
Save darkworks/0dd50b45b05a20eda8b4d249be3be0b8 to your computer and use it in GitHub Desktop.
Trim whitespace from SVG elements
function trimSvgWhitespace() {
// get all SVG objects in the DOM
var svgs = document.getElementsByTagName("svg");
// go through each one and add a viewbox that ensures all children are visible
for (var i=0, l=svgs.length; i<l; i++) {
var svg = svgs[i],
box = svg.getBBox(), // <- get the visual boundary required to view all children
viewBox = [box.x, box.y, box.width, box.height].join(" ");
// set viewable area based on value above
svg.setAttribute("viewBox", viewBox);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment