Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riccardoscalco/413e661e66d102fa6f76 to your computer and use it in GitHub Desktop.
Save riccardoscalco/413e661e66d102fa6f76 to your computer and use it in GitHub Desktop.
Scale SVG graphics with only css
<div id="box">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

Scale SVG graphics with only css

Make SVG graphics responsive with CSS. Useful for scale down d3 charts on mobile devices.

A Pen by Riccardo Scalco on CodePen.

License.

var w = 100,
h = 100;
// https://css-tricks.com/scale-svg/
// padding-bottom Hack on an Inline <svg> Element
var svg = d3.select("#box")
.append("svg")
.attr("viewBox", "0 0 " + w + " " + h)
.attr("preserveAspectRatio", "xMidYMin slice")
.attr("width", "100%")
.style({
"padding-bottom": (100 * h / w) + "%",
"height": "1px",
"overflow": "visible"
});
svg.append("circle")
.attr("cx", w / 2)
.attr("cy", h / 2)
.attr("r", w / 2)
.style("fill", "limegreen");
#box {
max-width: 600px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment