Pen Settings



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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


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


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.


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.


                <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>
<p>Original post in <a href="" 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>
<p>Some clarification about widows and orphans taken from <a href="" target="_blank">this comment in the above post</a>:</p>
  <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>
<h3>Difference between Widows and Orphans</h3>
<img src="" alt="Difference between Widows and Orphans in typography">
<p>Source: <a href="" target="_blank">Good Rags No Widows No Orphans</a></p>
<p>Demo created by: <a href="" target="_blank">Ricardo Zea</a></p>



                @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(;
@import url(,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: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( no-repeat;



//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];
    $(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');