css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="myDiv">
  <form class="svg-test-form">
  <div class="fields">
      <legend>SVG size</legend>
      <div><label><input type="radio" name="img-type" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="img-type" value="100"> 100x100</label></div>
      <div><label><input type="checkbox" name="use-viewbox"> Use viewBox</label></div>
      <legend><code>&lt;img&gt;</code> size</legend>
      <div><label><input type="radio" name="img-size" value="0" checked=""> Unset</label></div>
      <div><label><input type="radio" name="img-size" value="50"> 50x50</label></div>
      <div><label><input type="radio" name="img-size" value="100"> 100x100</label></div>
      <legend><code>&lt;img&gt;</code> CSS size</legend>
      <div><label><input type="radio" name="img-css-size" value="0" checked=""> Unset</label></div>
      <div><label><input type="radio" name="img-css-size" value="50"> 50x50</label></div>
      <div><label><input type="radio" name="img-css-size" value="100"> 100x100</label></div>
      <div><label><input type="checkbox" name="add-to-dom"> Add to &lt;body&gt;</label></div>
      <legend><code>&lt;canvas&gt;</code> size</legend>
      <div><label><input type="radio" name="canvas-size" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="canvas-size" value="100"> 100x100</label></div>
      <legend><code>drawImage</code> size</legend>
      <div><label><input type="radio" name="drawimage-size" value="50" checked=""> 50x50</label></div>
      <div><label><input type="radio" name="drawimage-size" value="100"> 100x100</label></div>
      <legend>Context transform</legend>
      <div><label><input type="radio" name="context-transform" value="0.5"> 0.5x</label></div>
      <div><label><input type="radio" name="context-transform" value="1" checked=""> 1x</label></div>
      <div><label><input type="radio" name="context-transform" value="2"> 2x</label></div>
      <div><label><input type="radio" name="context-transform" value="5"> 5x</label></div>
  <div class="svg-test-output trans-tile"><canvas width="50" height="50"></canvas></div>

              body {
  margin: 0;

.myDiv {
  width: 768px;
  margin: 0 auto;

  .svg-test-form {
    font-size: 0.9em;
    margin: 0 -20px;

  @media (min-width: 530px) {
    .svg-test-form {
      margin: 0 -64px 0 -32px;

  .svg-test-output {
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0;
    margin-bottom: 10px;
    overflow: hidden;

  .svg-test-output canvas {
    display: block;
    outline: 1px solid rgba(0,0,0,0.2);

  .svg-test-form .fields {
    display: flex;
    flex-flow: row wrap;
    background: #e0eaf5;
    padding: 10px 0 0 20px;

  .svg-test-form fieldset {
    border: none;
    padding: 0;
    margin: 10px 0;
    min-width: 50%;

  @media (min-width: 500px) {
    .svg-test-form fieldset {
      min-width: 33%;

  .svg-test-form legend {
    font-size: 1em;
    font-weight: bold;

              function loadImg(url, width, height) {
  return new Promise(function(resolve, reject) {
    var img = new Image();

    if (width) {
      img.width = width;

    if (height) {
      img.height = height;

    img.src = url;
    img.onload = function() {
    img.onerror = function() {
      reject(Error('Image load failed'))

(function() {
  var svgImgs = {
    fixed50: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/fixed50.72ff2442c6d1.svg',
    fixed100: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/fixed100.4b1cb7cb9384.svg',
    viewbox50: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/viewbox50.bc248d9428e2.svg',
    viewbox100: 'https://jakearchibald.com/static/posts/svg-canvas-media-queries/viewbox100.297cc24d9e8c.svg',

  function createCanvas(width, height) {
    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;
    return canvas;

  function drawImgOnCanvas(canvas, img, width, height, scale) {
    var context = canvas.getContext('2d');
    context.scale(scale, scale);
    context.drawImage(img, 0, 0, width, height);

  var svgTestForm = document.querySelector('.svg-test-form');
  var svgTestOutput = document.querySelector('.svg-test-output');

  function processForm() {
    var svgSize = svgTestForm.querySelector('[name=img-type]:checked').value;
    var imgSize = Number(svgTestForm.querySelector('[name=img-size]:checked').value);
    var imgCssSize = Number(svgTestForm.querySelector('[name=img-css-size]:checked').value);
    var addImgToDom = !!svgTestForm.querySelector('[name=add-to-dom]:checked');
    var useViewbox = !!svgTestForm.querySelector('[name=use-viewbox]:checked');
    var canvasSize = Number(svgTestForm.querySelector('[name=canvas-size]:checked').value);
    var drawImageSize = Number(svgTestForm.querySelector('[name=drawimage-size]:checked').value);
    var contextTransform = Number(svgTestForm.querySelector('[name=context-transform]:checked').value);
    var imgUrl;

    if (useViewbox) {
      imgUrl = svgImgs['viewbox' + svgSize];
    else {
      imgUrl = svgImgs['fixed' + svgSize];

    svgTestOutput.innerHTML = '';

    loadImg(imgUrl, imgSize, imgSize).then(img => {
      var el = img;

      if (imgCssSize) {
        img.style.width = img.style.height = imgCssSize + 'px';

      if (addImgToDom) {

      el = createCanvas(canvasSize, canvasSize);
      drawImgOnCanvas(el, img, drawImageSize, drawImageSize, contextTransform);

      if (addImgToDom) {



  svgTestForm.addEventListener('change', function() {

🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................