Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Strange <code>vw</code>/<code>viewBox</code> interaction</h1>
<h2>&lt;rect> positioned at left edge of SVG, &lt;svg> translated 60vw, behaving as expected</h2>
<svg class="translate-vw" viewBox="0 0 1000 100">
    <rect fill="#0000FF" width="100" height="100"/>
</svg>

<h2>&lt;rect> positioned at left edge of SVG then translated 60vw, not behaving as expected</h2>
<svg viewBox="0 0 1000 100">
    <rect class="translate-vw" fill="#0000FF" width="100" height="100"/>
</svg>
<p>Matches the previous example only when the window is 1000px wide (matching the viewBox width). If the window is smaller or larger, the size of a <code>vw</code> is <em>first</em> scaled by the ratio of <nobr><code>(window width)/(viewBox width)</code></nobr>, <em>then</em> the normal viewBox scaling occurs, resulting in a double-correction.
<p>If you set the second SVG to a fixed <code>1000px</code> width, the reason for the correction becomes obvious - the two rects maintain alignment regardless of the window width, meaning that the 60vw length is always literally 60% of the window's width:
<h2>Identical to previous, but with a fixed size of <code>1000px</code></h2>
<svg viewBox="0 0 1000 100" width=1000>
    <rect class="translate-vw" fill="#0000FF" width="100" height="100"/>
</svg>
<p>But if you set the width to anything else, such that the viewBox causes some scaling, then it's still wrong, just not in quite as confusing a manner:
<h2>Identical to previous, but with a fixed size of <code>500px</code></h2>
<svg viewBox="0 0 1000 100" width=500>
    <rect class="translate-vw" fill="#0000FF" width="100" height="100"/>
</svg>
<p>Correct behavior is to scale <code>vw</code> by the ratio <nobr><code>(window width)/(&lt;svg> width)</code></nobr>. This would make the squares align in all four examples.</p>
<p>If you nest further, you just need to stack the scaling factors, with "viewBox width of outer SVG" taking the place of "window width":
<h2>Final example, with two nested &lt;svg>s, for testing purposes</h2>
<svg viewBox="0 0 1200 100" width=1000>
  <svg viewBox="0 0 100 200" width=200>
    <rect class="translate-vw" fill="#0000FF" width="100" height="100"/>
  </svg>
</svg>
              
            
!

CSS

              
                body {margin: 0}
div {background: pink;}

.one {width: 60vw;}

.translate-vw {
	transform: translateX(60vw)
}

svg {display: block; overflow: visible; outline: thin dashed gray}

              
            
!

JS

              
                
              
            
!
999px

Console