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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.

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.


                <h1>Using SVG for (PNG) Image Sprites</h1>
<p><strong>See the <a href="" target="_blank">updated version here</a>, which uses <code>&lt;symbol></code> elements to define the <code>viewBox</code> as part of the icons, cutting down on the mark-up required when using them later.</strong></p>
<p>A combination of transformations and clipping paths position the sprite image appropriately with SVG elements, which can then be reused (and resized) as necessary.</p>
<svg id="graphic">
      <!-- The icons are defined in an SVG <defs> element;
            it could be in a different file,
            since the icons will be referenced by url. -->
        <!-- One clipping path defines icon size -->
        <clipPath id="icon-cp" >
            <rect x="0" y="0" width="150" height="100" />
        <!-- One image element imports the file -->
        <image id="icon-sprite" width="969" height="293" xlink:href="" />
        <!-- For each icon, define the translation 
            on the image/use element, and assign 
            the clip path to a wrapping <g>;
            this nested structure allows the use
            of one clip path for all icons. -->
        <g id="icon1" clip-path="url(#icon-cp)">
            <use xlink:href="#icon-sprite" 
                transform="translate(0,0)" />
        <g id="icon2" clip-path="url(#icon-cp)">
            <use xlink:href="#icon-sprite" 
                transform="translate(-240,0)" />
        <g id="icon3" clip-path="url(#icon-cp)">
            <use xlink:href="#icon-sprite" 
                transform="translate(-240,-193)" />
        <!-- Of course, you can skip the "use"
           and define the image directly in each group -->
        <g id="icon4" clip-path="url(#icon-cp)">
          <image id="icon-sprite" width="969" height="293" xlink:href=""
                transform="translate(-603,0)" />
        <!-- (But then you have to repeat the width
            and height every time, and have many
            places to update if the url changes...) -->

    <text x="15" y="30">The icons can be used in an SVG graphic...</text>
    <use xlink:href="#icon1" x="0" y="50" />
    <use xlink:href="#icon2" x="200" y="50" />
    <use xlink:href="#icon3" x="0" y="200"/>
    <use xlink:href="#icon4" x="200" y="200"/>      
<p>But usually you will create small SVG elements to use as icons wherever you need them.  The SVG icons can be set to any size, so long as the "viewBox" attribute matches the original dimensions.  Each one contain a &lt;use> element referencing the icon fragment url:<p>
<svg class="icon" viewBox="0 0 150 100" preserveAspectRatio="xMidYMid meet" height="20px" width="30px">
        <use xlink:href="#icon1"/>
<svg class="icon" viewBox="0 0 150 100" preserveAspectRatio="xMidYMid meet" height="150px" width="250px">
        <use xlink:href="#icon2"/>
<svg class="icon" viewBox="0 0 150 100" height="4em" width="6em">
        <use xlink:href="#icon3"/>
<p>You can even squash them, if you want...</p>
<svg class="icon" viewBox="0 0 150 100"  preserveAspectRatio="none" height="50px" width="200px">
        <use xlink:href="#icon4"/>

<p>Since SVG height and width are overwritten by CSS, you can just set the viewBox attribute on the element and let the CSS scale them to fit context.  So a single class on your inline icon is enough to ensure it always fits the surrounding text:</p>
<div class="inset">
  <h1>Lunch Time 
<svg class="inline icon" viewBox="0 0 150 100">
        <use xlink:href="#icon2"/>
  <p>I love lunch time 
<svg class="inline icon" viewBox="0 0 150 100">
        <use xlink:href="#icon2"/>
</svg>, don't you?</p>
  <small>This message brought to you by 
  <svg class="inline icon" viewBox="0 0 150 100">
        <use xlink:href="#icon2"/>

<p>But what about everybody's favourite use of CSS image sprites, the hover effect?  Just declare multiple elements within the svg, and change their opacity on hover.  You can even add a transition effect.</p>
<svg class="icon" viewBox="0 0 150 100" height="60px" width="90px">
        <use class="no-hover" xlink:href="#icon1"/>
        <use class="hover" xlink:href="#icon4"/>
<p>(Of course, you'd want to put a little more effort into getting your icon images to line up correctly...)</p>

<p>Conventional CSS background image sprites look the same, but you'll need a separate CSS rule for each size -- and lots of calc() functions to get the sizing right:<p>
<div class="png icon i1"></div>
<div class="png icon i2"></div>
<div class="png icon small i3"></div>
<div class="png icon small i4"></div>

<p>And if you try to transition your hover icons, you get quite a different effect:</p>
<div class="png icon i1 hover"></div>

<p>Of course, which you use also depends on design consideration -- do you want the images tucked away in the style sheet, or do you want them available as content in the web page?</p>

  <p>Inspired by <a href="">this StackOverflow question</a>.  Image sprite file created for <a href="">this question</a>.</p>


                svg, div.icon {
    border:solid gray;
svg#graphic {
    height: 350px;
svg.inline.icon {
  border:none; background:none;
  width: 1.5em;
svg .hover {
  opacity: 0;
  transition: 1s;
svg .no-hover {
  transition: 1s;
svg:hover .hover {
  opacity: 1;
svg:hover .no-hover {
  opacity: 0;
div.inset {
  margin: 0 2em;
  padding: 1em 2em;
  border: solid gray;
  background: rgb(200,255,225);
div.icon.png {
    background: url("") no-repeat, aliceblue;
  background-size: calc(969px /2) calc(293px/2);
div.icon.png.i1 {   
    background-position: 0px 0px;
div.icon.png.i2 {   
    background-position: -240px 0px;
div.icon.png.i3 {   
    background-position: -240px -193px;
div.icon.png.i4 {   
    background-position: -603px 0px;
div.icon.png.i1.small {   
    background-position: calc(0px/2) calc(0px/2);
div.icon.png.i2.small {   
    background-position: calc(-240px/2) calc(0px/2);
div.icon.png.i3.small {   
    background-position: calc(-240px/2) calc(-193px/2);
div.icon.png.i4.small {   
    background-position: calc(-603px/2) calc(0px/2);
div.icon.hover {
  transition: 1s;
div.icon.hover.i1:hover {
    background-position: -603px 0px;  
p {
footer {
  border-top: dotted gray 1px;