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 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

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

              
                <h2>Preventing Orphans in Post Titles</h2>
<h1>This is a very long heading that <em>DOES NOT</em> have orphans&hellip;</h1>
<h1 class="has-orphans">This is a very long heading that <em>DOES</em> have orphans, look&hellip;</h1>
<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><u>Important technical note:</u></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>
<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 { width: 580px; margin:0 auto 20px; padding: 20px; font: 40px Montserrat; background: rgba(white,.04);  box-shadow:inset 0 0 0 5px rgba(black,.2); }

h2 { text-align: center; }

//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; }
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,.15); font: 14px Courier; box-shadow:inset 0 0 1px rgba(black,.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").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:'Opens in a new tab/window'}).addClass('new-window');


              
            
!
999px

Console