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

              
                <p class="fontClass"><code>fontSwitcher</code> dynamically loads any font from the <a href="https://www.google.com/fonts" target="_blank">Google Fonts</a> repository and applies it to existing text.</p>
<p class='fontClass goItalic'>(Current version v3.0 is <a href='https://github.com/freginold/fontSwitcher'>available on GitHub</a> and also supports <a href='https://edgewebfonts.adobe.com/index'>Adobe Edge Web Fonts</a>.)</p>
<button id="fontButton" onclick="var newFont=prompt('New Google Font?');fontSwitcher(newFont, 'fontClass');">Try it</button>
<p class="fontClass">To use <code>fontSwitcher</code>, include this line between your <code>&#060;head&#062;</code> and <code>&#060;/head&#062;</code> tags:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>&#060;script src="https://codepen.io/freginold/pen/vLJewY.js"&#062;&#060;/script&#062;</code></p>
<p class="fontClass">Call <code>fontSwitcher</code> in this format:</p>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>fontSwitcher('newFontName', 'elementClass'[, 'fallbackFont1', 'fallbackFont2' ...]);</code>
<p class="fontClass">where <code>newFontName</code> is the new font to load and <code>elementClass</code> is the class to attach the new font to.</p>
<p><code>fallbackFont1</code> (and additional fallback fonts, if any) is the font to fall back to if the primary font does not load for any reason. The fallback font can be a native or pre-loaded font, a generic font, or it can even be a Google web font (if the web font has already been loaded).  If using a Google web font as a fallback font and you loaded it with <code>fontSwitcher</code>, make sure it has been applied to an alternative class, so that the <code>&#060;link&#062;</code> element is not overwritten.</p>
<p class="fontClass">Capitalization and spacing matter for Google font names.</p>
<p class="fontClass">You can use <code>fontSwitcher</code> to apply multiple Google fonts on the same page; just call the function once for each class.</p>
<p class="fontClass">To attach multiple classes to an element, use the format:</p>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<code>class="<span class="goItalic">class1 class2</span>"</code>
<p class="fontClass">You can use Google's <a href="https://github.com/typekit/webfontloader#get-started" target="_blank">Web Font Loader</a> instead, but <code class="goBold">fontSwitcher</code> automatically applies the new font to any class you specify, so you can use it for multiple fonts at the same time.</p>
<p class="fontClass"><code>fontSwitcher</code> can also be used to apply local fonts (ie. Courier, Arial) but it may be slower than simply changing the <code>fontFamily</code> attribute since it will still call to Google first.</p>
<p class="fontClass">Licensed under the <a href="https://github.com/freginold/fontSwitcher/blob/gh-pages/LICENSE" target="_blank">MIT License</a>.</p>
<p class="fontClass">More improvements will be coming, including support for font styles and effects.</p>
              
            
!

CSS

              
                body {
  background-color: #efefef;
  width: 97%;
  padding: 3px;
  font-size: 1.2em;
}
a:hover {
  text-decoration: none;
}
.fontClass {
  font-family: serif;
}
#fontButton {
  font-family: monospace;
  font-size: 1.2em;
  display: inline;
  position: relative;
  font-weight: bold;
  color: darkblue;
  margin-top: 10px;
  margin-left: 5px;
  background: #d1d1d1;
}
ul {
  color: black;
  font-family: serif;
  margin-left: -7px;
}
.goItalic {
  font-style: italic;
}
              
            
!

JS

              
                function fontSwitcher(newFontName, elementClass) {  

  function checkLink() {
    //check to see if link element for this class has already been built
      if (!(document.getElementById(linkName))) {
        //if fontSwitcher link for this class doesn't exist, create it
        var linkElement=document.createElement("link");
        linkElement.id=linkName;
        linkElement.rel="stylesheet";
        linkElement.type="text/css";
        document.head.appendChild(linkElement);
    }
    else if (!((document.getElementById(linkName).rel=="stylesheet") &&
          (document.getElementById(linkName).type=="text/css"))) {
            document.getElementById(linkName).rel="stylesheet";
            document.getElementById(linkName).type="text/css";
        }
  }

  function makeURL(fontName) {
    //create URL to grab the Google font
    newFontURL="https://fonts.googleapis.com/css?family=";
    for (var i=0;i<(fontName.length);i++) {
      if (fontName.charAt(i)==" ") {
        newFontURL=newFontURL+"+";
      }
      else {
        newFontURL=newFontURL+fontName.charAt(i);
      }
    }
    return newFontURL;
  }

  function checkFallbacks() {
    //check if any fallback fonts specified
    if (fontArgs.length<=2) {
        return;
    }
    //loop through fontArgs and add each fallback font to the string
    for (incr=2; incr<fontArgs.length;incr++) {
        if (!!checkGeneric(fontArgs[incr])) {
            //if fallback font is generic
            fallbackString=fallbackString+","+fontArgs[incr];
        }
        else {
            fallbackString=fallbackString+",'"+fontArgs[incr]+"'";
        }
    }
  }
  
  function checkGeneric(thisFont) {
    //check to see if it's a generic font; if it is, return true
    generic=false;
    switch (thisFont.toLowerCase()) {
        case 'serif':
        case 'sans-serif':
        case 'cursive':
        case 'fantasy':
        case 'monospace':
            generic=true;
            break;
        default:
            generic=false;
            break;
    }
    return generic;
  }
  // ------- execution starts here ------------
  
  var newFontURL=makeURL(newFontName);
  var linkName="FS"+elementClass;
  var fontArgs=arguments;
  var fallbackString='';
  var classToChange;
  checkFallbacks();
  if (!checkGeneric(newFontName)) {
    //if non generic font
    checkLink();
    newFontName="'"+newFontName+"'"+fallbackString;
    document.getElementById(linkName).href=newFontURL;
  }
  else {
    newFontName=newFontName+fallbackString;
  }  
  classToChange=document.getElementsByClassName(elementClass);
  for (var i=0;i<classToChange.length;i++){
    classToChange[i].style.fontFamily=newFontName;
  }
}
              
            
!
999px

Console