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

              
                <header>
  <h1>Characters Per Line experiment</h1>
</header>

<section data-cpl-section>

  <h2 id="customiser">CPL Customiser</h2>

  <fieldset>
    <legend>Text: </legend>
    <textarea placeholder="Enter custom text" data-type="text" data-input></textarea>
  </fieldset>
  <fieldset>
    <legend>Element width: </legend>
    <input type="range" min="300" max="940" value="940" data-type="width" data-input>
    <span data-width-value>940</span>
  </fieldset>
  <fieldset>
    <legend>Characters per line:</legend>
    <input type="range" min="10" max="100" value="50" data-type="cpl" data-input>
    <span data-cpl-value>50</span>
  </fieldset>
  <fieldset>
    <legend>Max characters (actual):</legend>
    <label data-max-value></label>
  </fieldset>

  <p class="c-cpl-text c-cpl-text--customiser" data-cpl-text>As we refine our methods of responsive web design, we’ve increasingly focused on measure (another word for “line length”) and its relationship to how people read.</p>

</section>
              
            
!

CSS

              
                html {
  font-size: 15px;
  font-family: monospace; }

body {
  margin: 10px;
  width: 940px;
  max-width: calc(100% - 10px); }

header,
section {
  margin-bottom: 50px; }

header {
  border-bottom: solid 1px black;
  padding-bottom: 20px; }

fieldset {
  border: solid 1px grey;
  margin-bottom: 10px; }

input {
  width: 88%; }

textarea {
  border: solid 3px lightgrey;
  height: 50px;
  resize: none;
  width: 98%; }

h1 {
  border-bottom: solid 1px black;
  padding-bottom: 20px; }

p {
  display: inline-block;
  word-wrap: break-word; }

.c-cpl-text {
  line-height: 1.5;
  width: 100%; }
  .c-cpl-text--customiser {
    font-size: 31.396px;
    width: 940px; }
  .c-cpl-text--responsive {
    font-size: 15.03px;
    width: 450px; }
    @media (min-width: 640px) {
      .c-cpl-text--responsive {
        font-size: 20.04px;
        width: 600px; } }
    @media (min-width: 940px) {
      .c-cpl-text--responsive {
        font-size: 30.06px;
        width: 900px; } }
  .c-cpl-text--rem {
    font-size: 1rem;
    width: 28rem; }
    @media (min-width: 640px) {
      .c-cpl-text--rem {
        font-size: 1.25rem;
        width: 35rem; } }
    @media (min-width: 940px) {
      .c-cpl-text--rem {
        font-size: 1.5rem;
        width: 44rem; } }
  .c-cpl-text--ch {
    font-size: 1rem;
    max-width: 50ch; }
    @media (min-width: 640px) {
      .c-cpl-text--ch {
        font-size: 1.25rem; } }
    @media (min-width: 940px) {
      .c-cpl-text--ch {
        font-size: 1.5rem; } }
  .c-cpl-text--ch-use-case {
    font-size: 1.2rem;
    max-width: 45ch;
    line-height: 1.8; }
    @media (min-width: 640px) {
      .c-cpl-text--ch-use-case {
        max-width: 55ch; } }
    @media (min-width: 940px) {
      .c-cpl-text--ch-use-case {
        max-width: 75ch; } }

.c-link--selected {
  color: green; }

              
            
!

JS

              
                var width = 940;
var cpl = 50;
var bodyMargin = 10;
var fontType = "monospace";
var fontTypeConstants = {
    serif: 2.3333,
    sansSerif: 2.2513,
    monospace: 1.67,
    Georgia: 2.27
};
var textElement = document.querySelector("[data-cpl-text]");

function updateText (event) {
    var inputElement = event.currentTarget;
    var valueElement = inputElement.nextElementSibling || null;
    
    var inputValue = inputElement.value;
    
    var dataType = inputElement.getAttribute("data-type");
    var fontType = "monospace";
    
    if (dataType === "width") {
        width = inputValue;
        textElement.style.width = width + "px";
    }

    if (dataType === "cpl") {
        cpl = inputElement.value;
    }

    if (dataType === "text") {
        textElement.innerHTML = inputElement.value;
    }

    if (valueElement) {
        valueElement.innerHTML = inputValue;
    }

    addSpanElements(textElement);
    updateCharacterCountUI(textElement);

    textElement.style.fontSize = getCPLFontSize(width, cpl, fontType);

}

function getCPLFontSize(width, cpl, fontType) {
    var constant = fontTypeConstants[fontType];

    return (width / cpl) * constant + "px";
}

function updateCharacterCountUI(textElement) {
    var spanElements = textElement.querySelectorAll("span");
    var maxElement = document.querySelector("[data-max-value]");
    var characterCountData = {
        lineLengths: [],
        maxLength: 0
    };
    var currentLineLength = 0;
    var currentYPosition;

    Array.prototype.forEach.call(spanElements, function (span, index) {

        var lengthWithoutSpace = span.innerHTML.length;
        var lengthWithSpace = lengthWithoutSpace + 1;

        if (span.offsetLeft === bodyMargin ) {
            // Span is left most

            if (span.offsetTop > currentYPosition && currentYPosition !== undefined) {
                // Span is first in row (not in element)

                characterCountData.lineLengths.push(currentLineLength);
            }

            currentLineLength = lengthWithSpace;
            currentYPosition = span.offsetTop;

        } else {
            // Span is mid sentence

            currentLineLength += lengthWithSpace;

        }

        if (index === (spanElements.length - 1)) {
            // Span is last word

            currentLineLength = currentLineLength - 1;    
            characterCountData.lineLengths.push(currentLineLength);

        }


    });

    characterCountData.maxLength = Math.max.apply(Math, characterCountData.lineLengths);

    maxElement.innerHTML = characterCountData.maxLength;

}

function addSpanElements(textElement) {
    var textValue = textElement.innerHTML;

    textValue = textValue.replace(/<span>/g, "");
    textValue = textValue.replace(/<\/span>/g, "");

    textValue = "<span>" + textValue + "</span>";
    textValue = textValue.replace(/\s/g, "</span> <span>");

    textElement.innerHTML = textValue;
}

// initialise

Array.prototype.forEach.call(document.querySelectorAll("[data-input]"), function (sliderElement) {
    sliderElement.addEventListener("input", updateText);
});

addSpanElements(textElement);
updateCharacterCountUI(textElement);
              
            
!
999px

Console