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="container">
  <div>
  <p class="myText">Alëksej Fedorovič Karamazov era il terzo figlio di un proprietario terriero del nostro distretto, Fëdor Pavlovič Karamazov, assai noto ai suoi tempi  (e del resto ancor oggi ricordato fra noi) per la sua tragica e oscura fine, avvenuta esattamente tredici anni fa e della quale parlerò a tempo debito.</p>
  <button onclick="countLines()">Count lines</button>
  <button onclick="clearResults()">Clear</button>
  <p class="results"></p>
  </div>
</div>


              
            
!

CSS

              
                @import 'https://fonts.googleapis.com/css?family=Roboto+Mono:300,700';
html,
body {
  font-family: 'Roboto Mono', monospace;
    background: #2c322e;
  height: calc(100% -40px);
}

.container {
  height: auto;
  width: 600px;
  padding:40px;
  justify-content: left;
  align-items: top;
  display: flex;
  box-sizing: border-box;
}



.results {
  font-size: 0.85em;
}

button {
  cursor: pointer;
  background: transparent;
  outline:none;
  border:1px solid #fff;
  color:#fff;
  border-radius:4px;
  padding:10px;
}

p {
  line-height: 1.5;
  font-weight: 300;
  font-size: 16px;
  color: #fafafa;
}

b {font-weight: 700;}

              
            
!

JS

              
                var results = document.querySelector(".results");

function countLines() {
  //Recupero dei dati e generazione delle variabili
  var item = document.querySelector(".myText");
  var myWidth = item.offsetWidth;
  var myString = item.innerHTML;

  //Popolamento dell'array:
  var myChars = myString .split(" ");
  for (let i=0; i<myChars.length; i++) {
    myChars[i] += " ";
    myChars[i] = myChars[i].split("");   
  }  
 
  let w = getComputedStyle(item).fontWeight;
  let s = getComputedStyle(item).fontSize;
  let f = getComputedStyle(item).fontFamily;
  var font = w.concat(" ",s," ",f);
  
  let letterWidth = getTextWidth("a", font);
  
  let measure = 0;
  
  for(let i=0; i<myChars.length; i++) {
        for(let j=0; j<myChars[i].length; j++) {
            
            measure += letterWidth;
            
            if (measure > myWidth){
               measure = 0;
               myChars.splice(i, 0, '\n');
               //i++;
            }   
        }  
    }
  
  
  let arr = [];
  for(let i=0; i<myChars.length; i++) {
    for(let j=0; j<myChars[i].length; j++) {
      arr.push(myChars[i][j]);
    }
  }
  let t = arr.join('');  
  let splitLines = t.split('\n');
   
  results.innerHTML = 'Il paragrafo ha: <b>' + splitLines.length + '</b> righe.' + '<br/>' + 'Il contenuto della <b>seconda</b> riga è:<br/><em>' + splitLines[1]; + '</em>'
}

function clearResults() {
  results.innerHTML = '';  
}

function getTextWidth(text, font) {
  var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
  var context = canvas.getContext("2d");
  context.font = font;
  var metrics = context.measureText(text);
  return metrics.width;
}
              
            
!
999px

Console