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 class="widget-wrap">
<div id="cart"></div>
<h1>GROCERY LIST</h1>
<!-- (A) ADD ITEM -->
<form id="gro-form">
<input type="number" id="gro-qty" min="1" value="1" placeholder="Quantity" required disabled>
<input type="text" id="gro-item" placeholder="Item Name" required disabled>
<button id="gro-add" class="material-icons" disabled>add</button>
</form>
<!-- (B) ITEM LIST -->
<div id="gro-list"></div>
<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
Visit
<a href="https://code-boxx.com/javascript-grocery-list/"
target="_blank">
Code Boxx
</a> for more details.
</div>
</div>
/* (A) ADD ITEM */
#gro-form, input, button {
font-size: 16px;
padding: 10px;
}
#gro-form {
display: flex;
border: 1px solid #ccc;
background: #f2f2f2;
}
button, input { border: 0; }
input[type=number] { width: 80px; }
input[type=text] { flex-grow: 1; }
button, input[type=button], input[type=submit] {
cursor: pointer;
color: #fff;
background: #b10b1a;
}
/* (B) ITEMS LIST */
#gro-list { margin-top: 20px; }
.item-row {
margin: 10px 0;
display: flex;
align-items: center;
}
.item-row.yes { background: #c6ffc3; }
.item-row.no { background: #ffd7d7; }
.item-name {
flex-grow: 1;
padding: 0 10px;
}
.item-ok {
color: #000;
background: none;
}
/* (X) DOES NOT MATTER */
/* PAGE & BODY */
* {
font-family: arial, sans-serif;
box-sizing: border-box;
}
body {
display: flex;
align-items: center; justify-content: center;
min-height: 100vh;
background-image: url(https://images.unsplash.com/photo-1506976785307-8732e854ad03?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NTU4ODAyNzE&ixlib=rb-1.2.1&q=80);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
/* WIDGET */
.widget-wrap {
min-width: 600px;
padding: 30px;
border-radius: 20px;
background: rgba(255, 255, 255, 0.8);
}
/* SVG */
#cart {
width: 100px; height:100px;
background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 576 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M528.12 301.319l47.273-208C578.806 78.301 567.391 64 551.99 64H159.208l-9.166-44.81C147.758 8.021 137.93 0 126.529 0H24C10.745 0 0 10.745 0 24v16c0 13.255 10.745 24 24 24h69.883l70.248 343.435C147.325 417.1 136 435.222 136 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-15.674-6.447-29.835-16.824-40h209.647C430.447 426.165 424 440.326 424 456c0 30.928 25.072 56 56 56s56-25.072 56-56c0-22.172-12.888-41.332-31.579-50.405l5.517-24.276c3.413-15.018-8.002-29.319-23.403-29.319H218.117l-6.545-32h293.145c11.206 0 20.92-7.754 23.403-18.681z" /></svg>');
background-repeat: no-repeat;
background-position: center;
}
/* FOOTER */
#code-boxx {
font-weight: 600;
margin-top: 50px;
}
#code-boxx a {
display: inline-block;
padding: 5px;
text-decoration: none;
background: #b90a0a;
color: #fff;
}
var glist = {
// (A) INITIALIZE GROCERY LIST
items : [], // current grocery list
hqty : null, // html add quantity field
hitem : null, // html add item field
hlist : null, // html <div> grocery list
init : () => {
// (A1) GET HTML ELEMENTS
glist.hqty = document.getElementById("gro-qty");
glist.hitem = document.getElementById("gro-item");
glist.hlist = document.getElementById("gro-list");
// (A2) ENABLE FORM
glist.hqty.disabled = false;
glist.hitem.disabled = false;
document.getElementById("gro-add").disabled = false;
document.getElementById("gro-form").onsubmit = glist.add;
// (A3) RESTORE PREVIOUS GROCERY LIST
if (localStorage.items == undefined) { localStorage.items = "[]"; }
glist.items = JSON.parse(localStorage.items);
// (A4) DRAW HTML GROCERY LIST
glist.draw();
},
// (B) SAVE GROCERY LIST INTO LOCAL STORAGE
save : () => {
if (localStorage.items == undefined) { localStorage.items = "[]"; }
localStorage.items = JSON.stringify(glist.items);
},
// (C) ADD NEW ITEM TO THE LIST
add : evt => {
// (C1) ADD NEW ITEM TO LIST
glist.items.push({
qty : glist.hqty.value, // item quantity
name : glist.hitem.value, // item name
got : false // item accquired
});
glist.save();
// (C2) RESET HTML ADD FORM
glist.hqty.value = 1;
glist.hitem.value = "";
// (C3) REDRAW HTML GROCERY LIST
glist.draw();
return false;
},
// (D) DELETE SELECTED ITEM
delete : id => { if (confirm("Remove this item?")) {
glist.items.splice(id, 1);
glist.save();
glist.draw();
}},
// (E) TOGGLE ITEM BETWEEN "ACCQUIRED" & "NOT YET"
toggle : id => {
glist.items[id].got = !glist.items[id].got;
glist.save();
glist.draw();
},
// (F) DRAW THE HTML GROCERY LIST
draw : () => {
// (F1) RESET HTML LIST
glist.hlist.innerHTML = "";
// (F2) NO ITEMS
if (glist.items.length == 0) {
glist.hlist.innerHTML = "<div class='item-row item-name'>No items found.</div>";
}
// (F3) DRAW ITEMS
else { for (let i in glist.items) {
// (F3-1) ITEM ROW
let row = document.createElement("div");
row.className = glist.items[i].got ? "item-row yes" : "item-row no";
glist.hlist.appendChild(row);
// (F3-2) DELETE BUTTON
let del = document.createElement("button");
del.className = "item-del material-icons";
del.innerHTML = "delete";
del.onclick = () => glist.delete(i);
row.appendChild(del);
// (F3-3) ITEM QUANTITY & NAME
let name = document.createElement("div");
name.innerHTML = `${glist.items[i].qty} X ${glist.items[i].name}`;
name.className = "item-name";
row.appendChild(name);
// (F3-4) ITEM ACCQUIRED
let ok = document.createElement("button");
ok.className = "item-ok material-icons";
ok.innerHTML = glist.items[i].got ? "done" : "clear";
ok.onclick = () => glist.toggle(i);
row.appendChild(ok);
}}
}
};
window.addEventListener("load", glist.init);
Also see: Tab Triggers