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.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="sticky-wrap">
<!-- This nav bar goes to the top of your hero section -->
<header role="banner" itemscope="itemscope" itemtype="http://schema.org/WPHeader">
<nav class="nav nav--primary">
<ul class="nav__items">
<li class="nav__item">
<a class="title" href="/">
Primary 1
</a>
</li>
<li class="nav__item">
<a class="title" href="/">
Primary 2
</a>
</li>
<li class="nav__item">
<a class="title" href="/">
Primary 3
</a>
</li>
<li class="nav__item">
<a class="title" href="/">
Primary 4
</a>
</li>
</ul>
</nav>
</header>
<!-- This is your hero content -->
<div class="hero hero__content">
Amazing Hero content
</div>
<!-- This nav bar goes to the bottom of your hero section -->
<nav class="nav nav--secondary">
<ul class="nav__items nav__items--secondary">
<li class="nav__sub-item">
<a href="/" class="link">Secondary 1</a>
</li>
<li class="nav__sub-item">
<a href="/" class="link">Secondary 2</a>
</li>
<li class="nav__sub-item">
<a href="/" class="link">Secondary 3</a>
</li>
<li class="nav__sub-item">
<a href="/" class="link">Secondary 4</a>
</li>
</ul>
</nav>
</div>
<div class="content-wrap">
Amazing body content
</div>
<div class="obi">
<div style="width:100%;height:0;padding-bottom:50%;position:relative;"><iframe src="https://giphy.com/embed/Nx0rz3jtxtEre" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div>
</div>
</body>
</html>
$color-white: white;
$color-green: #33d797;
$color-blue: #1c6cf0;
$color-navy: #1d1b2f;
@import url('https://fonts.googleapis.com/css?family=Lato');
*,
*:before,
*:after {
box-sizing: border-box;
margin: 0;
padding: 0;
border: 0;
}
.sticky-wrap {
z-index: 1;
position: relative;
}
//base styles for both primary and secondary navs
.nav {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
position: relative;
padding: 1rem;
transition: 0.2s ease all;
width: 100%;
a {
color: $color-white;
text-decoration: none;
}
//single item within items (in the nav)
&__item {
padding: 0 0.5rem;
}
&__sub-item {
padding: 0.5rem;
text-align: center;
}
//colors of text and links for home navigation
&--primary,
&--secondary {
color: $color-white;
}
//provides padding and margin for sticky header primary
&--primary {
background-color: $color-navy;
padding: 1.3rem 3rem;
position: fixed;
}
// secondary nav containing hot links to page content
&--secondary {
display: flex;
justify-content: center;
align-items: flex-start;
padding: 0;
margin: 0;
background-color: $color-green;
}
//container for items within nav
&__items {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
position: absolute;
width: initial;
left: 20%;
right: 20%;
top: initial;
list-style: none;
&--secondary {
margin: 0;
padding: 0.7rem 0;
bottom: 0;
width: 100%;
left: 0;
position: relative;
transition: padding 0.3s ease;
}
}
}
//class that fixes top (primary) navigation to top of window on desktop
.nav-fixed {
&--secondary {
position: fixed;
bottom: auto;
background-color: $color-blue;
//optional if you want to increase padding of the secondary nav once attached
.nav__items--secondary {
padding: 1rem 0;
}
}
}
.hero {
background-color: coral;
&__content {
display: flex;
justify-content: center;
align-content: center;
padding: 25rem;
font-size: 7em;
text-align: center;
height: 100%;
width: 100%;
@media only screen and (max-width: 400px) {
font-size: 3em;
padding: 70% 20%;
}
}
}
.content-wrap {
position: relative;
display: flex;
text-align: center;
justify-content: center;
align-items: center;
min-height: 1000px;
font-size: 7em;
@media only screen and (max-width: 400px) {
font-size: 3em;
}
}
.obi {
padding: 5% 25%;
@media only screen and (max-width: 400px) {
padding: 5%;
}
}
body {
font-family: lato;
}
// Create variables for our primary and secondary navigations
const navPrimary = document.querySelector(".nav--primary");
const navSecondary = document.querySelector(".nav--secondary");
const stickyWrap = document.querySelector(".sticky-wrap");
const contentWrap = document.querySelector('.content-wrap');
// Get the size and position relative to the viewport of the primary nav
let navPrimaryHeight = navPrimary.getBoundingClientRect().height;
// Get the height of the secondary nav items
let navSecondaryHeight = navSecondary.getBoundingClientRect().height;
// Get the top position of the secondary nav items
let navSecondaryTop = navSecondary.getBoundingClientRect().top;
// Get the whole hero section (.sticky-wrap) that wraps top nav, hero content and bottom nav, get it's bottom position and then take away the height of the secondary nav and add the number of pixels the doc has currently scrolled
let navSecondaryOrigPos = stickyWrap.getBoundingClientRect().bottom - navSecondaryHeight + window.pageYOffset;
// The function that ensures the nav shows if the user refreshes the page, scrolled past the hero section
function stickyNavInit() {
navSecondaryTop = navSecondary.getBoundingClientRect().top;
// If the top position of the secondary navigation items are less than or equal to the primary navigation's height
if (navSecondaryTop <= navPrimaryHeight) {
// add the fixed class to the secondary nav
navSecondary.classList.add("nav-fixed--secondary");
// add the height to the secondary nav as top
navSecondary.style.top = navPrimaryHeight + "px";
//add padding top to the web body to prevent jumping
contentWrap.style.paddingTop = (navSecondaryTop + navPrimaryHeight) + "px";
}
// If the window's scroll Y position and the height of the primary navigation are less than the whole hero section's bottom position
if (window.scrollY + navPrimaryHeight < navSecondaryOrigPos) {
// then remove the sticky class from secondary nav
navSecondary.classList.remove("nav-fixed--secondary");
// and remove the height of the navigation from the top
navSecondary.removeAttribute("style");
contentWrap.removeAttribute("style");
}
}
// function that throttles the event listener (or any function you add)
function throttled(delay, fn) {
let lastCall = 0;
return function (...args) {
const now = (new Date).getTime();
if (now - lastCall < delay) {
return;
}
lastCall = now;
return fn(...args);
}
}
// On scroll, fire the function
throttled(200, window.addEventListener("scroll", stickyNavInit));
Also see: Tab Triggers