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 id="shop-wrap">
<h1>MY SHOPPING LIST</h1>
<!-- (A) ADD NEW ITEM -->
<form id="shop-form">
<input type="text" id="shop-item" placeholder="Item Name" required disabled/>
<input type="submit" id="shop-add" value="Add" disabled/>
</form>
<!-- (B) SHOPPING LIST -->
<div id="shop-list"></div>
</div>
<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
Visit
<a href="https://code-boxx.com/shopping-list-vanilla-javascript/"
target="_blank">
Code Boxx
</a> for more details.
</div>
/* (A) SHARED */
#shop-wrap input {
padding: 10px;
border: 0;
}
#shop-wrap input[type=button], #shop-wrap input[type=submit] {
cursor: pointer;
color: #fff;
background: #5a75d6;
}
#shop-form, .item-row {
display: flex;
align-items: stretch;
}
#shop-item, .item-name { flex-grow: 1; }
/* (B) WRAPPER */
#shop-wrap {
max-width: 500px;
padding: 15px;
margin: 0 auto;
}
/* (C) SHOPPING LIST */
.item-row { margin-top: 10px; }
.item-name {
padding: 10px;
background: #fff;
}
.item-name.item-got { background: #f5fffa; }
.item-name.item-got:before {
content: "\02713";
margin-right: 5px;
font-weight: bold;
color: #00d036;
}
.item-del { background: #de1919 !important; }
/* [DOES NOT MATTER] */
/* PAGE & BODY */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
body {
min-height: 100vh;
background-image: url(https://images.unsplash.com/photo-1603400521630-9f2de124b33b?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTY5ODQyNA&ixlib=rb-1.2.1&q=85);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
/* FOOTER */
#code-boxx {
text-align: center;
font-weight: 600;
margin-top: 30px;
}
#code-boxx a {
display: inline-block;
padding: 5px;
text-decoration: none;
background: #b90a0a;
color: #fff;
}
var slist = {
// (A) INITIALIZE SHOPPING LIST
items : [], // current shopping list
hform : null, // html add item <form>
hitem : null, // html add item <input> field
hadd : null, // html add item submit button
hlist : null, // html <div> shopping list
init : () => {
// (A1) GET HTML ELEMENTS
slist.hform = document.getElementById("shop-form");
slist.hitem = document.getElementById("shop-item");
slist.hadd = document.getElementById("shop-add");
slist.hlist = document.getElementById("shop-list");
// (A2) "ACTIVATE" HTML ADD ITEM FORM
slist.hitem.setAttribute("autocomplete", "off");
slist.hform.onsubmit = slist.add;
slist.hitem.disabled = false;
slist.hadd.disabled = false;
// (A3) RESTORE PREVIOUS SHOPPING LIST
if (localStorage.items == undefined) { localStorage.items = "[]"; }
slist.items = JSON.parse(localStorage.items);
// (A4) DRAW HTML SHOPPING LIST
slist.draw();
},
// (B) SAVE SHOPPING LIST INTO LOCAL STORAGE
save : () => {
if (localStorage.items == undefined) { localStorage.items = "[]"; }
localStorage.items = JSON.stringify(slist.items);
},
// (C) ADD NEW ITEM TO THE LIST
add : evt => {
// (C1) PREVENT FORM SUBMIT
evt.preventDefault();
// (C2) ADD NEW ITEM TO LIST
slist.items.push({
name : slist.hitem.value, // item name
done : false // true for "got it", false for "not yet"
});
slist.hitem.value = "";
slist.save();
// (C3) REDRAW HTML SHOPPING LIST
slist.draw();
},
// (D) DELETE SELECTED ITEM
delete : id => { if (confirm("Remove this item?")) {
slist.items.splice(id, 1);
slist.save();
slist.draw();
}},
// (E) TOGGLE ITEM BETWEEN "GOT IT" OR "NOT YET"
toggle : id => {
slist.items[id].done = !slist.items[id].done;
slist.save();
slist.draw();
},
// (F) DRAW THE HTML SHOPPING LIST
draw : () => {
// (F1) RESET HTML LIST
slist.hlist.innerHTML = "";
// (F2) NO ITEMS
if (slist.items.length == 0) {
slist.hlist.innerHTML = "<div class='item-row item-name'>No items found.</div>";
}
// (F3) DRAW ITEMS
else {
for (let i in slist.items) {
// ITEM ROW
let row = document.createElement("div");
row.className = "item-row";
slist.hlist.appendChild(row);
// ITEM NAME
let name = document.createElement("div");
name.innerHTML = slist.items[i].name;
name.className = "item-name";
if (slist.items[i].done) { name.classList.add("item-got"); }
row.appendChild(name);
// DELETE BUTTON
let del = document.createElement("input");
del.className = "item-del";
del.type = "button";
del.value = "Delete";;
del.onclick = () => { slist.delete(i); };
row.appendChild(del);
// COMPLETED/NOT YET BUTTON
let ok = document.createElement("input");
ok.className = "item-ok";
ok.type = "button";
ok.value = slist.items[i].done ? "Not Yet" : "Got It";
ok.onclick = () => { slist.toggle(i); };
row.appendChild(ok);
}
}
}
};
window.addEventListener("load", slist.init);
Also see: Tab Triggers