HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<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);
};
};
Also see: Tab Triggers