Last active
November 30, 2024 00:51
-
-
Save adamcbrewer/f3086d494ecc6c1de1da to your computer and use it in GitHub Desktop.
JS: Trim SVG viewbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Will trim the whitespace around the SVG's viewbox | |
* | |
*/ | |
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); |
@zogreptile you don't have to set the width or height unless you need a default size for them. I personally use CSS for that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If i get the idea properly, it's also necessary to set the
width
andheight
svg attributes with the calculatedbbox.width, bbox.height
values.