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.
<nav id="main-nav">
<ul>
<li><a href="#">About Us</a></li>
<li><a href="#">Product List</a></li>
<li class="last"><a href="#">Contact</a></li>
<li class="last"><a href="#">FAQ</a></li>
<li id="indicator"></li>
</ul>
</nav>
<button id="reset" type="button">reset</button>
$blue: #1b3d4e;
$copper: #c07053;
$snow: #f3efeb;
$stone: #cbc0ba;
body {
box-sizing: border-box;
background-color: $snow;
font-family: "Unica One", cursive;
font-size: 16px;
}
nav {
max-width: 960px;
padding: 2.666em 1em 1.333em 1em;
position: relative;
background-color: $blue;
ul {
list-style: none;
margin: 0;
padding-top: 1em;
position: relative;
display: flex;
justify-content: flex-start;
li {
// padding: 0 1.5em;
// border: 1px solid orange;
a {
font-size: 1.25em;
line-height: 1;
letter-spacing: 0.05em;
transition: position 1s ease;
text-decoration: none;
text-transform: uppercase;
// background-color: red;
// display: inline-block;
margin: 0 1em 0 0;
color: $stone;
&.active {
color: $copper;
}
}
}
li.last {
margin-right: 0;
}
} // ul
} // nav
#indicator {
display: block;
width: 100px;
height: 3px;
background-color: lighten($copper, 10%);
position: absolute;
bottom: -3px;
left: -100px;
margin: 0;
padding: 0;
transition: left 700ms ease, width 700ms ease, opacity 500ms ease;
opacity: 0;
&.active {
opacity: 1;
}
} // #indicator
#reset {
position: fixed;
bottom: 1em;
right: 1em;
background: none;
border: none;
font-family: inherit;
text-transform: uppercase;
background-color: $stone;
padding: 0.5em 1em;
display: none;
&.show {
display: block;
}
}
// NOTES:
// Not really terribly flexible yet, as it requires a lot of hardcoding of values and it tricky to get working with right-aligned navs
//https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/
// forEach method, could be shipped as part of an Object Literal/Module
function forEach(array, callback, scope) {
for (var i = 0; i < array.length; i++) {
callback.call(scope, i, array[i]); // passes back stuff we need
}
}
// Cacheing DOM items
var indicator = document.getElementById("indicator"),
nav = document.getElementById("main-nav"),
navItems = document.querySelectorAll("nav > ul li a"),
paddingCompensation = "1em",
resetButton = document.getElementById("reset");
resetButton.addEventListener("click", function(event) {
resetState(event);
});
// State
var activeState = false,
activeLink;
// Default position of indicator, captured from styles
var defaultSettings = {
left: window.getComputedStyle(indicator, null).getPropertyValue("left"),
width: window.getComputedStyle(indicator, null).getPropertyValue("width")
};
// If a link is "active" set the indicator's position to it instead of the default
function activeLinkSet() {
let thisPosition = activeLink.getBoundingClientRect();
let thisWidth = activeLink.offsetWidth;
let thisHeight = activeLink.offsetHeight;
indicator.style.left = "calc(" + thisPosition.x + "px - 1em)";
indicator.style.width = thisWidth + "px";
indicator.classList.add("active");
}
// Returns to default on hover out
function returnToDefault() {
indicator.style.left = defaultSettings.left;
indicator.style.width = defaultSettings.width;
indicator.classList.remove("active");
resetButton.classList.remove("show");
}
// Logic for everything, should probably rename this since it's used on more than hover. Takes event, and a string for logic as parameters
function handleHover(e, direction) {
if (direction === "set active") {
// if we're setting active, run that function
activeLinkSet();
} else {
// otherwise, check hover in or out
if (direction === "in") {
// if hover in, get the event target's settings and apply them.
let thisPosition = e.currentTarget.getBoundingClientRect();
let thisWidth = e.currentTarget.offsetWidth;
let thisHeight = e.currentTarget.offsetHeight;
// calc is here for difficulties with getBoundingClientRect and padding issues
indicator.style.left =
"calc(" + thisPosition.x + "px - " + paddingCompensation + ")";
indicator.style.width = thisWidth + "px";
indicator.classList.add("active");
} else if (direction === "out") {
// if hover out, set back to a default, depending on state of activeState
if (activeState === true) {
activeLinkSet();
} else {
returnToDefault();
}
}
}
}
// for each lopps to add event listeners to links
forEach(navItems, function(index, value) {
navItems[index].addEventListener("mouseenter", function(event) {
handleHover(event, "in");
});
navItems[index].addEventListener("click", function(event) {
handleClick(event);
});
});
// hover out is actually on the nav element itself, not the links!!!
nav.addEventListener("mouseleave", function(event) {
handleHover(event, "out");
});
// fake link clicking
// could be replaced by something simpler on a real multi-page site
function handleClick(e) {
e.preventDefault();
resetState();
// add active to current target
e.currentTarget.classList.add("active");
// set state
activeState = true;
// set active link
activeLink = e.currentTarget;
// run logic function to set placement of indicator
handleHover(e, "set active");
// turn reset button on
resetButton.classList.add("show");
}
function resetState() {
activeState = false;
// get all links
let allLinks = document.querySelectorAll("nav a");
// link is clicked, remove all active classes, just in case
for (var i = 0; i < allLinks.length; i++) {
allLinks[i].classList.remove("active");
}
returnToDefault();
}
Also see: Tab Triggers