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

              
                <h1>Preventing Widows in Post Titles</h1>
<h2>This is a very long heading that <em>DOES NOT</em> have a widow&hellip;</h2>
<h2 class="has-orphans">This is a very long heading that <em>DOES</em> have a widow, look&hellip;</h2>
<hr>
<p>Original post in CSS-Tricks.com: <a href="https://css-tricks.com/preventing-widows-in-post-titles/" target="_blank">Preventing Widows in Post Titles</a></p>
<h3>Important technical note:</h3>
<p><strong>You can have ANY markup inside the heading and it will not get stripped out.</strong></p>
<p>At the moment of creating this demo, Chris' script in his post did not account for markup being present inside the heading. In other words, Chirs' script strips out any markup inside the <code>&lt;h1&gt;</code>, so if your heading has a link then the link goes bye-bye when it runs.</p>
<p>This improved script however doesn't strip out any markup, that's why you see the underlined words in both examples via <code>&lt;em&gt;</code>.</p>
<p>Obviously, a huge Thanks! to Chris Coyer for the original script.</p>
<hr>
<p>Some clarification about widows and orphans taken from <a href="https://css-tricks.com/preventing-widows-in-post-titles/#comment-60227" target="_blank">this comment in the above post</a>:</p>
<blockquote>
  <p>"A widow is a paragraph-ending line or word that falls at the top beginning of the following page or column – and is separated by the rest of the paragraph.</p>
  <p>An orphan is a line/word that appears by itself at the bottom of a page/column."</p>
</blockquote>
<h3>Difference between Widows and Orphans</h3>
<img src="https://assets.codepen.io/9988/widows-orphans-difference.png" alt="Difference between Widows and Orphans in typography">
<p>Source: <a href="https://thegoodpage.net/2015/11/21/rags-widows-orphans/" target="_blank">Good Rags No Widows No Orphans</a></p>
<hr>
<p>Demo created by: <a href="http://ricardozea.me/" target="_blank">Ricardo Zea</a></p>

              
            
!

CSS

              
                @import "compass/css3";

//These properties are merely for visual enhacenment of the headings, they're not needed for the script to work.
h1 { text-align: center; }

h2 {
   max-width: 580px;
   margin: 0 auto 20px;
   padding: 20px;
   font: 40px Montserrat;
   background: rgba(white, 0.04);
   box-shadow: inset 0 0 0 5px rgba(black, 0.2);   
}






//Styling stuff, not needed for the demo
//Google Fonts
@import url(https://fonts.googleapis.com/css?family=Montserrat);
@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300italic);

//Sass/Compass stuff
@import "compass/css3/images";
$MyBlack: #272822;
$r: #c03;
$g: #429032;
$b: #2963bd;
$y: #c90;

* { @include box-sizing(border-box); }
html { height: 100%; }
body {
   @include background(radial-gradient(lighten($MyBlack, 5%), $MyBlack 80%));
   color: white;
   font: 16px "Source Sans Pro";
   padding: 30px;
}
h3 { font-size: 25px; }
hr {
   border: none;
   border-top: #666 1px dotted;
}
blockquote { font-style: italic; }
em {
   text-decoration: underline;
   font-style: normal;
}
code {
   padding: 5px;
   font-style: italic;
   background: rgba(black, 0.15);
   font: 14px Courier;
   box-shadow: inset 0 0 1px rgba(black, 0.5);
}
a,
a:visited { color: $b; }
a:hover {
   color: $y;
   text-decoration: none;
}

.new-window {
   position: relative;
   padding-right: 16px;
   &:after {
      content: "";
      display: block;
      width: 14px;
      height: 12px;
      position: absolute;
      top: 10%;
      right: 0;
      background: url(https://i.stack.imgur.com/3H2PQ.png) no-repeat;
   }
}

              
            
!

JS

              
                //***ATTENTION!!!***:
//When using this script in production you need to remove the ".not('.has-orphans')" method below */

$("h1, h2").not(".has-orphans").each(function() {
    var wordArray = $(this).html().split(" ");
    if (wordArray.length > 1) {
        wordArray[wordArray.length-2] += "&nbsp;" + wordArray[wordArray.length-1];

        var lastWord = wordArray.pop();
        lastWord = lastWord.replace(/.*((?:<\/\w+>)*)$/, "$1");
        $(this).html(wordArray.join(" ") + lastWord);
    }
});

/* 
   I left the original (but adapted) script from Chris' post for reference only.
   If you want to see it work (or 'not work' for that matter), just uncomment the script below and comment the top one.
  What you'll see is that the words "DOESN'T" and "DOES" are not underlined anymore and both headings have no orphans.
*/

/*
$("h1").each(function() {
  var wordArray = $(this).text().split(" ");
  if (wordArray.length > 1) {
    wordArray[wordArray.length-2] += "&nbsp;" + wordArray[wordArray.length-1];
    wordArray.pop();
    $(this).html(wordArray.join(" "));
  }
});*/

//Add a title to the links that open in a new tab/window
$("a[target='_blank']").attr({title:'Link opens in a new tab'}).addClass('new-window');


              
            
!
999px

Console