Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

HTML

              
                <h1>CSS Styles with Reusable SVG Icons</h1>
<svg class="defs">
  <symbol id="multiColoredIcon" viewBox="-2 -2 14 14">
    <desc>A stop sign</desc>
    <path fill="red" stroke-width="1"
          d="M0,3 L3,0 7,0 10,3 10,7 7,10 3,10 0,7Z"/>
    <text text-anchor="middle" line-height="1em"
          x="5" y="7" textLength="8"
          fill="currentColor" stroke="white" stroke-width="0.2" font-size="4" font-weight="bold">
      STOP
    </text>
  </symbol>
</svg>

<p>Here's some text containing an embedded SVG icon <svg class="embed"><title>Stop it!</title><use xlink:href="#multiColoredIcon"/></svg>. Isn't that nifty? </p>


<p class="blue">And this is some colored text containing the same icon <svg class="embed"><use xlink:href="#multiColoredIcon"><title>I'm warning you!</title></use></svg>.  Note that the SVG graphic properties that are set to use "currentColor" inherit the color. </p>

<p>Any styles set on the embedded <code>&lt;svg></code> or <code>&lt;use></code> element will also be inherited by the referenced graphics, replacing default values but <em>not</em> explicitly set values. <strong>Hover the above paragraphs</strong> to see the effect of setting the <code>stroke</code> property on the icons.  <strong>Hover directly over an icon</strong> to see the effect of setting <code>fill-opacity</code>.  <strong>Click on it</strong> to change the <code>color</code> property, which should change the <code>currentColor</code> fill of the text (but this doesn't seem to work in IE). </p>  

<p>In contrast, if you set styles on the elements within the original icon definition, it will affect all uses of the icon.  <strong>Hover the page title</strong> to see the effect of changing the graphics styles directly.  Note that I'm setting the styles on the <code>&lt;path></code> and  <code>&lt;text></code> elements directly (not on the parent <code>&lt;symbol></code> element) by using <code>symbol *</code> as the CSS selector, so that the styles will over-ride the graphical attributes defined in the SVG markup.</p>  
<p><strong>Be warned that browsers are <a href="https://codepen.io/AmeliaBR/pen/lshrp/">very inconsistent and buggy</a> about style changes on elements that are duplicated with <code>&lt;use></code> elements!</strong>  Any sort of a transition causes problems in webkit; specifying a transition on the original  graphics prevents the icons from being redrawn even when the redraw is triggered by a style change on the icon SVG (i.e., without touching the original graphic's styles).  In Firefox, setting hover effects directly on the source graphical elements, causes them to be triggered when hovering over the duplicate elements, but not in other browsers.  Therefore, stick with applying hover effects on the referencing SVG or <code>&lt;use></code> element, and leave the source graphic alone.</p>

<p>You can successfully transition <em>some</em> style effects by applying the CSS transition to the SVG/use element.  With a generic transition on the embedded SVG's styles, the change in opacity and in <code>color</code> / <code>currentColor</code> transition as expected in Webkit browsers (Chrome 33 / Opera 20) and Firefox (28), but not the change in stroke colour (it gets applied instantaneously).  In IE (which doesn't support transitions for SVG), the transition on <code>color</code> prevents  <code>currentColor</code> from updating.</p>
              
            
!

CSS

              
                h1{
  text-align:center;
  font-variant:small-caps;
  text-decoration:underline;
}
h1 code {
  font-variant: normal;
  font-size: larger;
}
p {
  max-width:80%;
  min-width:8em;
  margin:0.5em auto;
  font-size: large;
  line-height:1.5em;
}
p:first-of-type, p:nth-of-type(2){
  font-size:x-large;
}
p.blue {
  color:royalblue;
}
p strong {
  font-weight:bold;
  color:darkslateblue;
}
p code {
  background-color:lavender;
}
svg.defs {
  height:0; width:0; margin:0; padding:0;
}
svg.defs text {
  font-family:sans-serif;
}
svg.embed {
  height:3em;
  width:3em;
  vertical-align:-50%;
}
p:hover svg.embed{
  stroke: #fd0;
}
svg.embed:hover {
  fill-opacity:0.5;
}
/* This doesn't have the intended effect in IE */
svg.embed:active {
  color:orangered;
}
h1:hover ~ svg.defs symbol#multiColoredIcon * {
  fill:seagreen;
  stroke:darkgreen;
}
/* This doesn't transition the stroke property *
use, svg.embed {
  transition: 1s;
}/* */
/* This causes all sorts of bugs: *
symbol * {
  transition: 1s;
}/* */
              
            
!

JS

              
                
              
            
!
999px

Console