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.
<header id="header" class="header header--fixed hide-from-print" role="banner">
<div class="container" >
<nav id="nav" class="nav-wrapper" role="navigation">
<ul class="nav nav--main">
<li class="nav__item ">
<a class="header__link subdued" href="https://www.github.com/WickyNilliams/headroom.js">
<span aria-hidden="true" class="icon icon--github"></span>
<span class="complimentary push--left">GitHub</span>
</a>
</li>
<li class="nav__item ">
<a class="header__link subdued" href="https://www.twitter.com/WickyNilliams">
<span aria-hidden="true" class="icon icon--twitter"></span>
<span class="complimentary push--left">@WickyNilliams</span>
</a>
</li>
</ul>
</nav>
<a href="http://wicky.nillia.ms/headroom.js/" class="brand header__link">
<b class="brand__forename">Headroom</b><b class="brand__surname">.js</b>
</a>
</div>
</header>
<article>
<header class="feature">
<div class="container container--wide feature-title">
<h1 class="feature-title__title headroom-title">Headroom.js</h1>
<p class="feature-title__subtitle">Give your pages some headroom. Hide your header until you need it.</p>
</div>
</header>
<div class="container block main" role="main">
<h2>Downloads</h2>
<div class="downloads" style="margin-bottom:1em">
<div class="grid">
<div class="grid__item one-half"><a class="btn btn--secondary btn--full" href="https://raw.github.com/WickyNilliams/headroom.js/master/dist/headroom.js">Development <span class="complimentary subdued">(3.7kB)</span></a></div>
<div class="grid__item one-half"><a class="btn btn--secondary btn--full" href="https://raw.github.com/WickyNilliams/headroom.js/master/dist/headroom.min.js">Production <span class="complimentary subdued">(1.7kB)</span></a></div>
</div>
</div>
<h2>What's it all about?</h2>
<p>Headroom.js is a lightweight, high-performance JS widget (with no dependencies!) that allows you to react to the user's scroll. The header on this site is a living example, it slides out of view when scrolling down and slides back in when scrolling up.</p>
<h3>Why use it?</h3>
<p>Fixed headers are a popular approach for keeping the primary navigation in close proximity to the user. This can reduce the effort required for a user to quickly navigate a site, but they are not without problems… </p>
<p>Large screens are usually landscape-oriented, meaning less vertical than horizontal space. A fixed header can therefore occupy a significant portion of the content area. Small screens are typically used in a portrait orientation. Whilst this results in more vertical space, because of the overall height of the screen a meaningfully-sized header can still be quite imposing.</p>
<p>Headroom.js allows you to bring elements into view when appropriate, and give focus to your content the rest of the time.</p>
<h3>How does it work?</h3>
<p>At it's most basic headroom.js simply adds and removes CSS classes from an element in response to a scroll event:</p>
<pre class="language-markup"><code><!-- initially -->
<header class="headroom">
<!-- scrolling down -->
<header class="headroom headroom--unpinned">
<!-- scrolling up -->
<header class="headroom headroom--pinned">
</code></pre>
<p>Relying on CSS classes affords headroom.js incredible flexibility. The choice of what to do when scrolling up or down is now entirely yours - anything you can do with CSS you can do in response to the user's scroll.</p>
<h2>Usage</h2>
<p>Using headroom.js is really simple. It has a pure JS API, and an optional jQuery/Zepto-compatible plugin.</p>
<h3>With pure JS</h3>
<pre class="language-javascript"><code>// grab an element
var myElement = document.querySelector("header");
// construct an instance of Headroom, passing the element
var headroom = new Headroom(myElement);
// initialise
headroom.init();
</code></pre>
<h3>With jQuery/Zepto</h3>
<pre class="language-javascript"><code>// simple as this!
// NOTE: init() is implicitly called with the plugin
$("# header").headroom();
</code></pre>
<p>The plugin also offers a data-* API if you prefer a declarative approach.</p>
<pre class="language-markup"><code><!-- selects $("[data-headroom]") -->
<header data-headroom>
</code></pre>
<p>Note: Zepto's additional <a href="https://github.com/madrobby/zepto#zepto-modules" title="https://github.com/madrobby/zepto#zepto-modules">data module</a> is required for compatibility.</p>
<h3>Options</h3>
<p>Headroom.js can also accept an options object to alter the way it behaves. You can see the default options by inspecting <code>Headroom.options</code>. The structure of an options object is as follows:</p>
<pre class="language-javascript"><code>{
// vertical offset in px before element is first unpinned
offset : 0,
// scroll tolerance in px before state changes
tolerance : 0,
// css classes to apply
classes : {
// when element is initialised
initial : "headroom",
// when scrolling up
pinned : "headroom--pinned",
// when scrolling down
unpinned : "headroom--unpinned"
}
}
</code></pre>
<h2>Examples</h2>
<p>
Head over to the <a href="http://wicky.nillia.ms/headroom.js/playroom/">headroom.js playroom</a> if you want see some example usages. There you can tweak all of headroom's options and apply different CSS effects in an interactive demo.
</p>
<h2>License</h2>
<p>
Licensed under the <a href="https://www.opensource.org/licenses/mit-license.php">MIT License</a>.
</p>
<div class="sharing">
<h2>Share this on:</h2>
<div class="grid">
<div class="grid__item one-third">
<a class="sharing__button btn btn--secondary btn--full" href="https://facebook.com/sharer.php?u=http://wicky.nillia.ms/headroom.js/">Facebook</a>
</div>
<div class="grid__item one-third">
<a class="sharing__button btn btn--secondary btn--full" href="https://twitter.com/intent/tweet?url=http://wicky.nillia.ms/headroom.js/&text=Headroom.js. Give your pages some headroom&via=WickyNilliams">Twitter</a>
</div>
<div class="grid__item one-third">
<a class ="sharing__button btn btn--secondary btn--full" href="https://plus.google.com/share?url=http://wicky.nillia.ms/headroom.js/">Google+</a>
</div>
</div>
</div>
</div>
<a class="btt btn btn--plain hide-from-print" href="#" id="btt">Top <i class="icon icon--up"></i></a>
</article>
<script src="https://rawgithub.com/WickyNilliams/headroom.js/gh-pages/assets/scripts/main.js"></script>
(function() {
var header = new Headroom(document.querySelector("#header"), {
tolerance: 5,
offset : 205,
classes: {
initial: "animated",
pinned: "slideDown",
unpinned: "slideUp"
}
});
header.init();
var bttHeadroom = new Headroom(document.getElementById("btt"), {
tolerance : 0,
offset : 500,
classes : {
initial : "slide",
pinned : "slide--reset",
unpinned : "slide--down"
}
});
bttHeadroom.init();
}());
Also see: Tab Triggers