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 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<h1 id="h1">a jQuery plugin for producing big, bold & responsive headlines</h1>
<hr />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="./js/jquery.slabtext.min.js"></script>
<script>
// Function to slabtext the H1 headings
function slabTextHeadlines() {
$("h1").slabText({
// Don't slabtext the headers if the viewport is under 380px
"viewportBreakpoint":380
});
};
// Called one second after the onload event for the demo (as I'm hacking the
// fontface load event a bit here)
// Please do not do this in a production environment - you should really use
// google WebFont loader events (or something similar) for better control
$(window).load(function() {
// So, to recap... don't actually do this, it's nasty!
setTimeout(slabTextHeadlines, 1000);
});
</script>
/* Note: I've not added the fontface files to the git repo */
@font-face
{
font-family: 'LeagueGothicRegular';
src: url('League_Gothic-webfont.eot');
src: url('League_Gothic-webfont.eot?#iefix') format('embedded-opentype'),
url('League_Gothic-webfont.woff') format('woff'),
url('League_Gothic-webfont.ttf') format('truetype'),
url('League_Gothic-webfont.svg#LeagueGothicRegular') format('svg');
font-weight: normal;
font-style: normal;
}
html,
body
{
background:#D35400;
color:#444;
}
body
{
font:16px/1.8 "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
width:80%;
padding:20px 0;
max-width:960px;
margin:0 auto;
}
/* https://css-tricks.com/simple-styles-for-horizontal-rules/ */
hr
{
border:0;
height:1px;
background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -moz-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -ms-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -o-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
}
.col-1
{
width:47.5%;
margin:0 2.5% 0 0;
float:left;
}
.col-2
{
width:47.5%;
margin:0 0 0 2.5%;
float:left;
}
.col-1 p,
.col-2 p
{
color:#888;
font-size:80%;
text-align:center;
}
a
{
color:#111;
}
h1 a
{
text-decoration:none;
}
h1 a:hover
{
color:#a82222;
}
p
{
margin:0 0 1.5em 0;
line-height:1.5em;
}
dt
{
font-family:monospace;
}
pre
{
line-height:1.2;
}
footer
{
border-top:3px double #aaa;
padding-top:1em;
}
footer section
{
border-bottom:3px double #aaa;
padding-bottom:1em;
margin-bottom:1em;
}
sup a
{
text-decoration:none;
}
#h4
{
clear:both;
}
.amp
{
font-family:Baskerville,'Goudy Old Style',Palatino,'Book Antiqua',serif;
font-style:italic;
font-weight:lighter;
}
/* Set font-sizes for the headings to be given the slabText treatment */
h1
{
text-align:left;
font-family:'LeagueGothicRegular', "Impact", Charcoal, Arial Black, Gadget, Sans serif;
text-transform: uppercase;
line-height:1;
color:#222;
font-size:300%;
/* Remember to set the correct font weight if using fontface */
font-weight:normal;
}
/* Smaller font-size for the side-by-side demo */
.col-1 h1,
.col-2 h1
{
font-size: 32px;
}
h2
{
font-size: 25px;
}
/* Adjust the line-height for all headlines that have been given the
slabtext treatment. Use a unitless line-height to stop sillyness */
.slabtexted h1
{
line-height:.9;
}
/* Target specific lines in the preset Studio One demo */
.slabtexted #studio-one span:nth-child(2)
{
line-height:.8;
}
.slabtexted #studio-one span:nth-child(3)
{
line-height:1.1;
}
/* Fun with media queries - resize your browser to view changes. */
@media screen and (max-width: 960px)
{
body
{
padding:10px 0;
min-width:20em;
}
.col-1,
.col-2
{
float:none;
margin:0;
width:100%;
}
h1
{
font-size:36px;
}
h2
{
font-size:22px;
}
}
@media screen and (max-width: 460px)
{
h1
{
font-size:26px;
}
h2
{
font-size:18px;
}
}
.slabtexted .slabtext{display:-moz-inline-box;display:inline-block;white-space:nowrap}.slabtextinactive .slabtext{display:inline;white-space:normal;font-size:1em !important;letter-spacing:inherit !important;word-spacing:inherit !important;*letter-spacing:0 !important;*word-spacing:0 !important;}.slabtextdone .slabtext{display:block;line-height:0.9;}
/*! jQuery slabtext plugin v2.3 MIT/GPL2 @freqdec */
(function( $ ){
$.fn.slabText = function(options) {
var settings = {
// The ratio used when calculating the characters per line
// (parent width / (font-size * fontRatio)).
"fontRatio" : 0.78,
// Always recalculate the characters per line, not just when the
// font-size changes? Defaults to true (CPU intensive)
"forceNewCharCount" : true,
// Do we wrap ampersands in <span class="amp">
"wrapAmpersand" : true,
// Under what pixel width do we remove the slabtext styling?
"headerBreakpoint" : null,
"viewportBreakpoint" : null,
// Don't attach a resize event
"noResizeEvent" : false,
// By many milliseconds do we throttle the resize event
"resizeThrottleTime" : 300,
// The maximum pixel font size the script can set
"maxFontSize" : 999,
// Do we try to tweak the letter-spacing or word-spacing?
"postTweak" : true,
// Decimal precision to use when setting CSS values
"precision" : 3,
// The min num of chars a line has to contain
"minCharsPerLine" : 0
};
// Add the slabtexted classname to the body to initiate the styling of
// the injected spans
$("body").addClass("slabtexted");
return this.each(function(){
if(options) {
$.extend(settings, options);
};
var $this = $(this),
keepSpans = $("span.slabtext", $this).length,
words = keepSpans ? [] : String($.trim($this.text())).replace(/\s{2,}/g, " ").split(" "),
origFontSize = null,
idealCharPerLine = null,
fontRatio = settings.fontRatio,
forceNewCharCount = settings.forceNewCharCount,
headerBreakpoint = settings.headerBreakpoint,
viewportBreakpoint = settings.viewportBreakpoint,
postTweak = settings.postTweak,
precision = settings.precision,
resizeThrottleTime = settings.resizeThrottleTime,
minCharsPerLine = settings.minCharsPerLine,
resizeThrottle = null,
viewportWidth = $(window).width(),
headLink = $this.find("a:first").attr("href") || $this.attr("href"),
linkTitle = headLink ? $this.find("a:first").attr("title") : "";
if(!keepSpans && minCharsPerLine && words.join(" ").length < minCharsPerLine) {
return;
};
// Calculates the pixel equivalent of 1em within the current header
var grabPixelFontSize = function() {
var dummy = jQuery('<div style="display:none;font-size:1em;margin:0;padding:0;height:auto;line-height:1;border:0;"> </div>').appendTo($this),
emH = dummy.height();
dummy.remove();
return emH;
};
// Most of this function is a (very) stripped down AS3 to JS port of
// the slabtype algorithm by Eric Loyer with the original comments
// left intact
// http://erikloyer.com/index.php/blog/the_slabtype_algorithm_part_1_background/
var resizeSlabs = function resizeSlabs() {
// Cache the parent containers width
var parentWidth = $this.width(),
fs;
//Sanity check to prevent infinite loop
if(parentWidth === 0) {
return;
};
// Remove the slabtextdone and slabtextinactive classnames to enable the inline-block shrink-wrap effect
$this.removeClass("slabtextdone slabtextinactive");
if(viewportBreakpoint && viewportBreakpoint > viewportWidth
||
headerBreakpoint && headerBreakpoint > parentWidth) {
// Add the slabtextinactive classname to set the spans as inline
// and to reset the font-size to 1em (inherit won't work in IE6/7)
$this.addClass("slabtextinactive");
return;
};
fs = grabPixelFontSize();
// If the parent containers font-size has changed or the "forceNewCharCount" option is true (the default),
// then recalculate the "characters per line" count and re-render the inner spans
// Setting "forceNewCharCount" to false will save CPU cycles...
if(!keepSpans && (forceNewCharCount || fs != origFontSize)) {
origFontSize = fs;
var newCharPerLine = Math.min(60, Math.floor(parentWidth / (origFontSize * fontRatio))),
wordIndex = 0,
lineText = [],
counter = 0,
preText = "",
postText = "",
finalText = "",
slice,
preDiff,
postDiff;
if(newCharPerLine != idealCharPerLine) {
idealCharPerLine = newCharPerLine;
while (wordIndex < words.length) {
postText = "";
// build two strings (preText and postText) word by word, with one
// string always one word behind the other, until
// the length of one string is less than the ideal number of characters
// per line, while the length of the other is greater than that ideal
while (postText.length < idealCharPerLine) {
preText = postText;
postText += words[wordIndex] + " ";
if(++wordIndex >= words.length) {
break;
};
};
// This bit hacks in a minimum characters per line test
// on the last line
if(minCharsPerLine) {
slice = words.slice(wordIndex).join(" ");
if(slice.length < minCharsPerLine) {
postText += slice;
preText = postText;
wordIndex = words.length + 2;
};
};
// calculate the character difference between the two strings and the
// ideal number of characters per line
preDiff = idealCharPerLine - preText.length;
postDiff = postText.length - idealCharPerLine;
// if the smaller string is closer to the length of the ideal than
// the longer string, and doesn’t contain less than minCharsPerLine
// characters, then use that one for the line
if((preDiff < postDiff) && (preText.length >= (minCharsPerLine || 2))) {
finalText = preText;
wordIndex--;
// otherwise, use the longer string for the line
} else {
finalText = postText;
};
// HTML-escape the text
finalText = $('<div/>').text(finalText).html()
// Wrap ampersands in spans with class `amp` for specific styling
if(settings.wrapAmpersand) {
finalText = finalText.replace(/&/g, '<span class="amp">&</span>');
};
finalText = $.trim(finalText);
lineText.push('<span class="slabtext">' + finalText + "</span>");
};
$this.html(lineText.join(" "));
// If we have a headLink, add it back just inside our target, around all the slabText spans
if(headLink) {
$this.wrapInner('<a href="' + headLink + '" ' + (linkTitle ? 'title="' + linkTitle + '" ' : '') + '/>');
};
};
} else {
// We only need the font-size for the resize-to-fit functionality
// if not injecting the spans
origFontSize = fs;
};
$("span.slabtext", $this).each(function() {
var $span = $(this),
// the .text method appears as fast as using custom -data attributes in this case
innerText = $span.text(),
wordSpacing = innerText.split(" ").length > 1,
diff,
ratio,
fontSize;
if(postTweak) {
$span.css({
"word-spacing":0,
"letter-spacing":0
});
};
ratio = parentWidth / $span.width();
fontSize = parseFloat(this.style.fontSize) || origFontSize;
$span.css("font-size", Math.min((fontSize * ratio).toFixed(precision), settings.maxFontSize) + "px");
// Do we still have space to try to fill or crop
diff = !!postTweak ? parentWidth - $span.width() : false;
// A "dumb" tweak in the blind hope that the browser will
// resize the text to better fit the available space.
// Better "dumb" and fast...
if(diff) {
$span.css((wordSpacing ? 'word' : 'letter') + '-spacing', (diff / (wordSpacing ? innerText.split(" ").length - 1 : innerText.length)).toFixed(precision) + "px");
};
});
// Add the class slabtextdone to set a display:block on the child spans
// and avoid styling & layout issues associated with inline-block
$this.addClass("slabtextdone");
};
// Immediate resize
resizeSlabs();
if(!settings.noResizeEvent) {
$(window).resize(function() {
// Only run the resize code if the viewport width has changed.
// we ignore the viewport height as it will be constantly changing.
if($(window).width() == viewportWidth) {
return;
};
viewportWidth = $(window).width();
clearTimeout(resizeThrottle);
resizeThrottle = setTimeout(resizeSlabs, resizeThrottleTime);
});
};
});
};
})(jQuery);
Also see: Tab Triggers