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="site-wrapper">
  <div class="site-container">
    <h1>Smart Dropcaps Ahoy!</h1>
    <p>Well hello! This is a simple, but powerful, JavaScript/jQuery dropcaps solution. It uses a combination of js and css to render a drop cap at the start of the first child paragraph of a given div class (or id). The letter will span a specified number or rows precisely, and will resize according to font size and line height. If the font size increases or decreases with the viewport width, the drop cap letter will resize accordingly.</p>
    <p>JavaScript and jQuery are used sparingly - to calculate font sizes and line heights, to create the dropcap markup, and to size it accordingly. Everything that can be done with pure CSS remains in CSS, including the ability to fine-tune the size of the font within its box. Although I am running scripts on window resize, I've got a "debouncer" in place, which delays firing by .25 second, preventing browser overload.</p>
    <p>My ultimate goal is to write a Drupal 8 module (and a WordPress plugin) that incorporates this code into an interface that presents the site builder with a variety of choices, including the use of images from a specific directory for the initial letter.</p>
    <p><i><b>Notes:</b></i>
      <ul>
        <li>I personally always test with a "W" as the first letter, because that's the widest letter.  Usually.</li>
      </ul>
    </p>
    <p><i><b>Things to do ("notes to self"):</b></i> 
      <ul>
        <li>Ensure that the height of the drop cap does not exceed the height of the paragraph</li>
        <li>Provide an option for image-based letters kept in a specific directory</li>
      </ul>
    </p>
  </div>
</div>
              
            
!

CSS

              
                $basecolor: #4b3520;

html {
  font-size: 18px;
}

body {
  font-family: 'PT Sans', 'Helvetica Neue', helvetica, arial, sans-serif;
  font-size: calc(.75em + .625vw);
  line-height: 1.5;
  background-color: $basecolor;
  padding: 0;
  margin: 0;
}

.site-wrapper {
  background-color: #eee;
  margin: 1.5em;
  background-color: mix($basecolor, white, 30%);
}

.site-container {
  padding: 1.25em;
}

h1, h2, h3, h4 {
  margin: 0 0 .5em 0;
  padding: 0;
  line-height: 1.2;
}

p {
  margin: 0 0 1em 0;
  padding: 0;
}

.dropcap {
  &--wrapper {
    display: flex;
    float: left;
    line-height: 1;
    overflow: hidden;
  }
  
  &--inner {
    border: 1px solid black;
    border-radius: .025em;
    display: flex;
    flex: 1;
    align-items: center;
    background-color: mix($basecolor, white, 50%);
  }
  
  &--letter {
    display: block;
    line-height: 1;
    flex: 1;
    text-align: center;
    font-size: 1em;
    margin-top: 0.03em;
    font-family: 'UnifrakturCook', 'Playfair Display SC', Times, 'Times New Roman', serif;
    text-transform: uppercase;
    color: mix($basecolor, black, 40%);
  }
}

              
            
!

JS

              
                let rowSpan = 4

$( document ).ready(function() {
  // A console log for testing.
  // console.log('Document ready!')
  let firstPar = getFirstPar();
  let firstLetter = getFirstLetter(firstPar)
  chopFirstLetter(firstPar)
  let dropCapObject = createDropCapObject(firstPar, firstLetter)
  maintainDropCap(firstPar, dropCapObject, rowSpan)
})

var debouncedActivity = debounce(function() {
  // A console log for testing.
  // console.log('Window resized!')
  let firstPar = getFirstPar();
  let dropCapObject = getDropCapObject(firstPar);
  maintainDropCap(firstPar, dropCapObject, rowSpan)
}, 300);

window.addEventListener('resize', debouncedActivity);

function getFirstPar() {  
  // A console log for testing.
  // console.log('Getting first paragraph object')
  let firstPar = $( '.site-container p' ).first()
  return firstPar
}

function getFirstLetter(firstPar) {
  // A console log for testing.
  // console.log('Getting first letter')
  let firstLetter = firstPar.text().charAt(0)
  return firstLetter
}

function chopFirstLetter(firstPar) {
  // A console log for testing.
  // console.log('Chopping the first letter')
  stringToChop = firstPar.html();
  firstPar.html(stringToChop.substring(1));
}

function createDropCapObject(firstPar, firstLetter) {  
  // A console log for testing.
  // console.log('Creating drop cap object')
  firstPar.prepend( '<span class="dropcap--wrapper"><span class="dropcap--inner"><span class="dropcap--letter">' + firstLetter + '</span></span></span>' )
  let dropCapObject = $( '.dropcap--wrapper' )
  return dropCapObject
}

function getDropCapObject(firstPar) {  
  // A console log for testing.
  // console.log('Getting drop cap object')
  let dropCapObject = $( '.dropcap--wrapper' )
  return dropCapObject
}

function maintainDropCap(firstPar, dropCapObject, rowSpan) {
  
  let firstParFontSize = parseInt($(firstPar).css('font-size'), 10)
  let firstParLineHeight = parseInt($(firstPar).css('line-height'), 10)
  let rowSpanPadding = firstParLineHeight - firstParFontSize
  let rowSpanHeight = (firstParLineHeight * rowSpan) - rowSpanPadding
  
  // Some console logs for testing.
  // console.log('firstParFontSize: ' + firstParFontSize)
  // console.log('firstParLineHeight: ' + firstParLineHeight)
  // console.log('rowSpanHeight: ' + rowSpanHeight)
  
  dropCapObject.css( "height", rowSpanHeight )
  dropCapObject.css( "width", rowSpanHeight )
  dropCapObject.css( "margin-top", rowSpanPadding * .5 )
  dropCapObject.css( "margin-right", rowSpanPadding * .75 )
  dropCapObject.css( "font-size", rowSpanHeight )
}

// Debounce function from https://davidwalsh.name/javascript-debounce-function
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
	var timeout;
	return function() {
		var context = this, args = arguments;
		var later = function() {
			timeout = null;
			if (!immediate) func.apply(context, args);
		};
		var callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
		if (callNow) func.apply(context, args);
	};
};
              
            
!
999px

Console