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>
<h1>Emoji Shop</h1>
</header>
<main>
<!-- The list of emoji products -->
<ul id="products"></ul>
<aside>
<div class="sticky-wrapper">
<h2>Shopping Cart</h2>
<!-- The list of products in our cart -->
<ul id="cart"></ul>
<!-- Our cart's total -->
<div id="total">
Total:
<span id="cart-total" class="price">0</span>
</div>
</div>
</aside>
</main>
@import url('https://fonts.googleapis.com/css?family=Open+Sans')
// Variables
$headingGutter: 0.6em
$gutter: 20px
$primaryColor: #ff7a59
$secondaryColor: #cbd6e2
$tertiaryColor: #7fd0dd
$lightestColor: #fff
$textColorOnMid: #2d3e50
$baseFontSize: 15px
$headingScale: 1.5
$boxShadow: inset 0 0 0 0px #00a4bd
$contextFont: (header: 6, main: 0.6, aside: 0.1)
// Mixins
@mixin card
transition: 0.3s box-shadow ease, 0.3s transform ease
padding: $gutter
border: 1px solid $secondaryColor
padding: 2em
border-radius: 5px
display: flex
align-items: center
justify-content: space-around
flex-direction: column
&:hover
box-shadow: 0 10px 20px -5px $secondaryColor
transform: translateY(-0.2em)
@mixin cta
display: inline-block
border: 1px solid $primaryColor
background-color: $primaryColor
border-radius: 3px
padding: 1em 2.5em
text-decoration: none
font-size: 0.9em
font-weight: 600
color: $lightestColor
transition: color .35s ease, background-color .35s ease
@mixin ctaAlternate
background-color: $lightestColor
color: $primaryColor
body
font-size: $baseFontSize
padding-top: 8em
text-align: center
font-family: 'Open Sans', sans-serif
// Typography
@each $tagName, $value in $contextFont
#{$tagName}
@for $i from 1 through 6
h#{$i}
font-size: $baseFontSize + ($i * $value)
a
+cta()
&:hover
+ctaAlternate()
p
margin: 0.5em 0
aside
a
+ctaAlternate()
&:hover
+cta()
// Price currencies
.price
&::before
content: '$'
// Layout
main
display: flex
flex-direction: row
position: relative
header
position: fixed
top: 0
left: 0
width: 100%
z-index: 1
padding: 1em
border-bottom: 2px solid $secondaryColor
background-color: $lightestColor
aside
position: sticky
top: 0
left: auto
right: 0
flex: 1
.sticky-wrapper
position: sticky
top: #{$gutter * 5}
// Products List
#products
list-style: none
display: grid
grid-template-columns: 30% 30% 30%
grid-template-columns: repeat(3, 1fr)
grid-gap: $gutter
padding: 0 $gutter
width: 70%
li
+card()
.emoji
font-size: 4em
margin: 0
// Shopping Cart
#cart
padding: 0 $gutter
list-style: none
li
+card()
font-size: 80%
padding: #{$gutter / 2}
margin: #{$gutter / 2} 0
/**
* Represents a product.
* @constructor
*/
class Product {
constructor(options) {
// ANSWER TO 5a:
const { id, emoji, description, price, inStock } = options;
this.id = id;
this.emoji = emoji;
this.description = description;
this.price = price;
this.inStock = inStock;
/* ANSWER TO 5b:
* constructor({ id, emoji, description, price, inStock }) {
* this.id = id;
* this.emoji = emoji;
* this.description = description;
* this.price = price;
* this.inStock = inStock;
* }
*/
}
// ANSWER TO 6a:
getHTML(inCart = false) {
if (this.inStock) {
// ANSWER TO 7a AND 7b:
const { id, emoji, description, price, inStock } = this;
// ANSWER TO 8a AND 8b:
const html =
`<li>
<h3>${description}</h3>
${!inCart ? `<p class="emoji">${emoji}</p>` : ''}
${this.quantity > 1
? `<p>${this.quantity} x
<span class="price">
${this.cartPrice}
</span>
</p>`
: ''}
<p>
<span class="price">${price}</span>
</p>
${inCart
? `<a href="#" onClick="cart.removeProduct(${id});">
Remove
</a>`
: `<a href="#" data-id="${id}">
Add to Cart
</a>`
}
</li>`;
return html;
}
return '';
}
}
/**
* Represents a shopping cart.
* @constructor
*/
class ShoppingCart {
// ANSWER TO 9a:
constructor(items = []) {
this.items = items;
}
addProduct(item) {
// ANSWER TO 10a, 10b, AND 10c:
const matchingItem = this.items.find(currentElement => item.id === currentElement.id);
if (typeof matchingItem !== 'undefined') {
const { price } = matchingItem;
matchingItem.quantity++;
matchingItem.cartPrice = matchingItem.quantity * price;
this.updateCart();
} else {
const cartItem = new ProductInCart(item);
this.items.push(cartItem);
this.updateCart();
}
}
removeProduct(id) {
const matchingItem = this.items.find(item => id === item.id);
if (typeof matchingItem !== 'undefined') {
const matchingItemIndex = this.items.indexOf(matchingItem);
this.items.splice(matchingItemIndex, 1);
this.updateCart();
}
}
updateCart() {
// called whenever an item is added or removed
const cart = document.getElementById('cart');
cart.innerHTML = '';
// ANSWER TO 11a AND 11b:
this.items.forEach(item => cart.innerHTML += item.getHTML(true));
this.updateTotal();
}
updateTotal() {
// ANSWER TO 12a AND 12b:
const total = this.items.reduce((currentSum, item) => currentSum + item.cartPrice, 0);
document.getElementById('cart-total').innerText = total;
}
}
/**
* Represents a product in a shopping cart.
* @constructor
* @extends Product
* @param {integer} cartPrice - The total price of this product in the cart.
* @param {integer} quantity - The quantity of this product in the cart.
*/
class ProductInCart extends Product {
constructor(options) {
super(options);
this.cartPrice = options.price;
this.quantity = 1;
}
}
/**
* Initializes our application.
*/
const url = 'https://raw.githubusercontent.com/anythingcodes/emoji-shop/master/data/emoji.json';
const cart = new ShoppingCart();
// ANSWER TO 13a:
fetch(url)
.then(serverResponse => serverResponse.json())
.then(response => {
const productContainer = document.getElementById('products');
// ANSWER TO 14a:
const products = response.map(responseItem => {
const newProduct = new Product(responseItem);
productContainer.innerHTML += newProduct.getHTML();
return newProduct;
});
const links = productContainer.querySelectorAll('a');
// ANSWER TO 15a AND 15c:
links.forEach(link => {
// ANSWER TO 13b:
link.addEventListener('click', evt => {
evt.preventDefault();
const id = parseInt(evt.target.getAttribute('data-id'), 10);
// ANSWER TO 15b AND 15bi:
const matchingProduct = products.find(product => product.id === id);
cart.addProduct(matchingProduct);
});
})
})
.catch(err => console.log(err));
Also see: Tab Triggers