css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              $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%);
  }
}

            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console