Remove SVG Whitespace

Removes the whitespace around an svg

by RenaissanceDesign

HTML

<svg width="42" height="42" xmlns="http://www.w3.org/2000/svg">
 <style type="text/css">.st0{display:none;}
	.st1{fill:none;stroke:#FFFFFF;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}
	.st2{display:inline;}
	.st3{display:inline;fill:none;stroke:#FFFFFF;stroke-width:2.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;}</style>

 <switch>
  <g id="svg_1">
   <rect id="svg_2" height="55.4" width="68.9" class="st0"/>
   <g id="search">
    <g id="svg_3">
     <circle id="svg_4" r="12" cy="23.8" cx="34" class="st1"/>
     <path id="svg_5" d="m26.7,23.8c0,-4 3.3,-7.3 7.3,-7.3" class="st1"/>
    </g>
    <line id="svg_6" y2="32.6" x2="42.7" y1="43.7" x1="53.9" class="st1"/>
   </g>
  </g>
 </switch>
 <g>
  <title>background</title>
  <rect fill="none" id="canvas_background" height="7.24999" width="7.24999" y="-1" x="-1"/>
 </g>
 <g>
  <title>Layer 1</title>
 </g>
</svg>

JavaScript

var svg = document.getElementsByTagName("svg")[0];
var bbox = svg.getBBox();
var viewBox = [bbox.x, bbox.y, bbox.width, bbox.height].join(" ");
svg.setAttribute("viewBox", viewBox);
prompt("Copy to clipboard: Ctrl+C, Enter", svg.outerHTML);