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

              
                <div class="clamp-container">
  <h1 data-dynamic-font-size>Pithy Phrase</h1>
</div>
<div class="clamp-container">
  <h1 data-dynamic-font-size>Short but not too short</h1>
</div>
<div class="clamp-container">
  <h1 data-dynamic-font-size>Something with more substance</h1>
</div>
<div class="clamp-container">
  <h1 data-dynamic-font-size>Something with more substance and pizazz</h1>
</div>
<div class="clamp-container">
  <h1 data-dynamic-font-size>
    A longer but equally compelling headline that fills up a couple lines
  </h1>
</div>
              
            
!

CSS

              
                .clamp-container {
  background: aliceblue;
  border-radius: 4px;
  color: navy;
  font-family: sans-serif;
  margin-bottom: 1rem;
  max-width: 60rem;
  padding: 0.5rem 1rem;
  
  & h1 {
    display: block;
    font-size: 2rem;
    max-width: 40ch;
    text-wrap: balance;
  }
}
              
            
!

JS

              
                const dynamicFontSize = (textLength) => {
    /**
     * Edit these values to suit your needs
     */
    // Them minimum and maximum font sizes in pixels
    const minFontSize = 32;
    const maxFontSize = 64;

    // The minimum and maximum line lengths in characters that should affect size
    const minLineLength = 12;
    const maxLineLength = 50;

    // The minimum and maximum viewport widths in pixels where resizing should occur
    const minViewportWidth = 480;
    const maxViewportWidth = 1400;

    /**
     * Danger, calculations below, tread carefully
     */
    // Relative max font size, gets bigger the fewer characters there are, down to minLineLength
    const relativeMaxFontSize =
      textLength < minLineLength
        ? maxFontSize
        : textLength > maxLineLength
        ? minFontSize
        : maxFontSize -
          ((maxFontSize - minFontSize) * (textLength - minLineLength)) /
            (maxLineLength - minLineLength);

    // Relative max viewport width, gets smaller the bigger the font size so it doesn't resize too soon
    const relativeMaxViewportWidth =
      maxViewportWidth * (minFontSize / relativeMaxFontSize);

    // Relative min viewport width, gets bigger the smaller the font size so it doesn't resize too late
    const relativeMinViewportWidth =
      minViewportWidth * (maxFontSize / relativeMaxFontSize);

    // Viewport width calculations
    const viewportWidth =
      (100 * (maxFontSize - minFontSize)) /
      (relativeMaxViewportWidth - relativeMinViewportWidth);

    // Relative font size calculation
    const relativeFontSize =
      (relativeMinViewportWidth * maxFontSize -
        relativeMaxViewportWidth * minFontSize) /
      (relativeMinViewportWidth - relativeMaxViewportWidth);

    // The magic clamp
    return `clamp(${minFontSize / 16}rem, ${viewportWidth}vw + ${
      relativeFontSize / 16
    }rem, ${relativeMaxFontSize / 16}rem)`;
  };

  const dynamicFontEls = document.querySelectorAll("[data-dynamic-font-size]");

  dynamicFontEls.forEach((dynamicFontEl) => {
    dynamicFontEl.style.fontSize = dynamicFontSize(
      dynamicFontEl.textContent.replace(/(<([^>]+)>)/gi, "").trim().length
    );
  });
              
            
!
999px

Console