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="text">       
  Information  is not knowledge.
  Knowledge is not wisdom.
  <div>
    W<a href="#">isdom</a> is not <a href="#">truth</a>.
  </div>
  Truth is not beauty.<br/>
  Beauty is not love.
  <b>Love is not music.</b>
  Music is THE BEST.
</div>

<div class="text">
  Information is not knowledge.
  Knowledge is not wisdom.
  <div>
    Wisdom is not <a href="#">truth</a>.
  </div>
  Truth is not beauty.<br/>
  Beauty is not love.
  <b>Love is not music.</b>
  Music is THE BEST.
</div>

<div class="info">
  <p>Try resizing element or window.</p>
</div>


              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 20px;
  line-height: 1.4em;
  padding: 30px;
  background: #111;
  color: #f9f9f9;
}

.text {
  width: 400px;
  max-width: 100%;
  margin: 20px auto;
  outline: 1px dotted #333;
  overflow: hidden;
  resize: horizontal;
}

.info {
  max-width: 400px;
  margin: 20px auto;
  color: #aaa;
  text-align: center;
  font-size: 15px;
  line-height: 17px;
}

b {
  font-weight: 900;
}
              
            
!

JS

              
                function parseChildNodes(element) {
  const fragment = document.createDocumentFragment();
  
  [...element.childNodes].forEach((node) => {
    if (node.nodeType === Node.TEXT_NODE) {
      const text = node.textContent.replace(/( +\r\n +| +\n +| +\r +)/gm, "").replace(/ {2,}/gm, ' ');
      const words = text.split(" ");

      words.forEach((word, index) => {
        const span = document.createElement("span");
        span.style.display = "inline-block";
        span.textContent = word;
        fragment.appendChild(span);

        const space = document.createTextNode(" ");

        if (index < words.length - 1) {
          fragment.appendChild(space);
        }
      });
    } else if (node.nodeType === Node.COMMENT_NODE) {
      // skip comments
    } else {
      fragment.appendChild(getTextLines(node));
    }
  });
  
  return fragment;
}

function getTextLines(element) {
  const fragment = parseChildNodes(element);
  
  element.innerHTML = "";
  element.appendChild(fragment);

  const lines = [];
  let line = [];
  let prevTop = null;

  element.querySelectorAll("span").forEach((wordSpan, index) => {
    const wordRect = wordSpan.getBoundingClientRect();

    if (prevTop !== wordRect.top && index > 0) {
      // Push the current line
      lines.push(line);
      // Start a new line
      line = [wordSpan.textContent];
    } else {
      // Add word to the current line
      line.push(wordSpan.textContent);
    }

    prevTop = wordRect.top;
  });

  // Push whatever words are left as the last line
  lines.push(line);

  return element;
}

function getColor(i) {
  const startH = 0;
  const h = (startH + i) % 360;
  const s = 90;
  const l = 70;
  return `hsl(${h}, ${s}%, ${l}%)`;
}

function colorLines(element) {
  const lines = getTextLines(element);
  //   const coloredLines = lines
  //     .map((line, i) => {
  //       return `<div style="color: ${getColor(i * 70)}">
  //         ${line.join(" ")}
  //       </div>`;
  //     })
  //     .join("\n");

  //   element.innerHTML = coloredLines;
}

const element = document.querySelector(".text");

// Get lines of text and color them on load
colorLines(element);

// Do it again on window and element resize
// function handleResize() {
//   colorLines(element);
// }

// window.addEventListener('resize', handleResize);
// new ResizeObserver(handleResize).observe(element);

              
            
!
999px

Console