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="layout">
<div class="warning">
<strong>Warning:</strong> Your browser does not support CSS container queries. Please open this demo in Chrome
Canary with the experimental <i>#enable-container-queries</i> flag turned on or in Safari Technology Preview.
</div>
<header class="header">
<div class="header__brand">
<div>Why LIT is 🔥 - shop</div>
</div>
<div class="header__cart">
<shopping-cart-toggle>
<span slot="toggle">Open shopping cart</span>
<shopping-cart slot="content"></shopping-cart>
</shopping-cart-toggle>
</div>
</header>
<main>
<h1>Shopping Cart</h1>
<p style="max-width: 1000px;">In this CodePen, the shopping cart product list is a Web Component that is both used in the large page view as in the small dropdown toggled by the <em>Open Shopping Cart</em> button at the top right. Thanks to Container Queries, we can reuse a single component and style it according to it's parents context and dimensions.</p><br>
<shopping-cart class="shopping-cart"></shopping-cart>
</main>
<footer class="footer">
<div class="footer__column footer__categories">
<h2>Categories</h2>
<ul>
<li><a href="/">Keyboards</a></li>
<li><a href="/">Mice</a></li>
<li><a href="/">Screens</a></li>
<li><a href="/">Laptops</a></li>
<li><a href="/">Microphones</a></li>
<li><a href="/">Headphones</a></li>
</ul>
</div>
<div class="footer__column footer__support">
<h2>Support</h2>
<ul>
<li><a href="/">Payments</a></li>
<li><a href="/">Shipment</a></li>
<li><a href="/">Returns</a></li>
</ul>
</div>
<div class="footer__column footer__contact">
<address>
<h2>Why LIT is 🔥 - shop</h2>
<span class="address__line">Wired street 42</span>
<span class="address__line">ABC123 Bitville</span>
</address>
</div>
<div class="footer__column footer__credits">Experiment by <a href="https://mrtnvh.com">mrtnvh</a></div>
</footer>
</div>
:root {
--dark: #444;
--gray: #ccc;
--light: #eee;
--spacer: 0.75rem;
--primary: #7950f2;
--link-color: var(--primary);
--heading-color: var(--dark);
--box-shadow: 0 5px 50px 0 rgba(0, 0, 0, 0.25);
}
@media screen and (min-width: 800px) {
:root {
--spacer: 1.5rem;
}
}
body {
font-family: sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: var(--dark);
font-size: clamp(14px, 2vw, 16px);
line-height: 1.5;
}
h1,
h2,
h3 {
margin-block-start: 0;
color: var(--heading-color);
font-weight: 800;
line-height: 1;
}
h1 {
font-size: clamp(2.25rem, 6vw, 6rem);
letter-spacing: -0.05em;
margin-bottom: var(--spacer);
}
p {
margin-bottom: var(--spacer);
}
a {
color: var(--link-color);
}
input {
font-size: 1rem;
}
.layout {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.header {
--link-color: var(--light);
background-color: var(--primary);
color: var(--light);
display: flex;
align-items: center;
justify-content: center;
padding-inline: var(--spacer);
}
.header__brand {
display: inline-flex;
flex-grow: 1;
font-weight: 800;
letter-spacing: -0.025em;
font-size: 1.5rem;
padding: var(--spacer);
}
.header__cart {
padding: var(--spacer);
}
.header__cart button {
-webkit-appearance: none;
padding: calc(var(--spacer) / 3) calc(var(--spacer) / 2);
border: 1px solid var(--link-color);
background: transparent;
color: var(--light);
font-weight: 800;
}
main {
flex-grow: 1;
padding: calc(var(--spacer) * 2) calc(var(--spacer) * 2);
}
.footer {
--link-color: var(--light);
--heading-color: var(--light);
color: var(--light);
background: linear-gradient(to bottom, #6741d9, #4d31a0);
padding: calc(var(--spacer) * 2) var(--spacer);
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template:
"categories support contact contact"
"copy copy credits credits";
}
.footer__column {
padding: calc(var(--spacer) / 2) calc(var(--spacer) / 1);
}
.footer__categories {
grid-area: categories;
}
.footer__support {
grid-area: support;
}
.footer__contact {
grid-area: contact;
}
.footer__copy {
grid-area: copy;
}
.footer__credits {
grid-area: credits;
text-align: right;
}
.footer ul {
list-style: none;
padding: 0;
}
.footer li {
margin-block-end: calc(var(--spacer) / 3);
}
address {
font-style: normal;
}
.address__line {
display: block;
margin-block-end: calc(var(--spacer) / 3);
}
.shopping-cart {
display: block;
padding-inline: var(--spacer);
background: var(--light);
}
.warning {
background-color: #fff9db;
color: #e67700;
padding: calc(var(--spacer) * 1.5) calc(var(--spacer) * 2);
}
@supports (container: inline-size) {
.warning {
display: none;
}
}
import {
html,
css,
LitElement
} from "https://cdn.skypack.dev/lit@2.0.0-rc.2?dts";
import {
customElement,
property,
state
} from "https://cdn.skypack.dev/lit@2.0.0-rc.2/decorators.js";
interface Product {
image: string;
name: string;
description?: string;
price: number;
quantity: number;
id: number;
}
const products: Product[] = [
{
id: 98456,
image:
"https://images.unsplash.com/photo-1541807084-5c52b6b3adef?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&h=500&q=80",
name: "Laptop",
description:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id varius nisl. Duis feugiat placerat est, a dignissim sapien dictum nec. Suspendisse eu ante eget nulla tempor tempor. Suspendisse ac nunc sollicitudin, eleifend nulla quis, pharetra enim. Pellentesque viverra tristique bibendum. Praesent vulputate at risus nec tempor. Praesent lorem neque, accumsan eget faucibus non, aliquam a purus. Aenean posuere massa vel lobortis tincidunt. Quisque bibendum varius laoreet. Nam dictum erat odio, a luctus lectus cursus a. Nam scelerisque turpis in nibh tempus, ac gravida justo lobortis. ",
price: 2105.25,
quantity: 3
},
{
id: 65486,
image:
"https://images.unsplash.com/photo-1589578228447-e1a4e481c6c8?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&h=500&q=80",
name: "Keyboard",
description:
"Duis feugiat placerat est, a dignissim sapien dictum nec. Suspendisse eu ante eget nulla tempor tempor. Suspendisse ac nunc sollicitudin, eleifend nulla quis, pharetra enim. Pellentesque viverra tristique bibendum. Praesent vulputate at risus nec tempor. Praesent lorem neque, accumsan eget faucibus non, aliquam a purus. Aenean posuere massa vel lobortis tincidunt. Quisque bibendum varius laoreet. Nam dictum erat odio",
price: 140.73,
quantity: 2
},
{
id: 5546,
image:
"https://images.unsplash.com/photo-1569696863868-45f498a50ef1?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MzN8fGhlYWRwaG9uZXN8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&h=500&q=60",
name: "Headphones",
description:
"Praesent lorem neque, accumsan eget faucibus non, aliquam a purus. Aenean posuere massa vel lobortis tincidunt. Quisque bibendum varius laoreet. Nam dictum erat odio",
price: 140.73,
quantity: 2
}
];
@customElement("shopping-cart")
export class ShoppingCart extends LitElement {
static styles = css`
:host {
--shopping-cart-spacer: 0.75em;
}
.wrapper {
container-type: inline-size;
}
input {
font-size: 1rem;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
text-align: left;
}
th,
td {
border-bottom: 1px solid var(--gray);
padding: calc(var(--shopping-cart-spacer) / 1);
}
td:first-child,
th:first-child {
padding-inline-start: 0;
}
td:last-child,
th:last-child {
padding-inline-end: 0;
}
tfoot tr:last-child td,
tfoot tr:last-child th {
border-bottom: 0;
}
.image-cell {
display: none;
}
.image {
max-width: 6em;
max-height: 6em;
}
.content-cell {
width: 100%;
}
.title {
display: block;
}
.description {
display: none;
line-height: 1.5;
}
.unit-price-cell,
.subtotal-cell {
text-align: right;
}
.total-price-label {
text-align: right;
}
.total-price-value {
text-align: right;
font-size: 1.25em;
font-weight: 800;
}
@container (min-width: 500px) {
:host {
--shopping-cart-spacer: 1.5em;
}
.image-cell {
display: table-cell;
}
.total-price-value {
font-size: 1.5em;
}
}
@container (min-width: 1050px) {
.image {
max-width: 12em;
max-height: 12em;
}
.title {
font-size: 1.5em;
margin-bottom: 0.375em;
}
.description {
display: block;
}
}
`;
@property()
products: Product[] = products;
@property()
vat: number = 0.21;
get totalPrice() {
return this.products.reduce((acc, product) => {
return acc + this.getProductSubTotal(product);
}, 0);
}
getProductSubTotal(product: Product) {
return product.quantity * product.price;
}
formatPrice(price: number) {
return new Intl.NumberFormat("nl-BE", {
style: "currency",
currency: "EUR"
}).format(price);
}
handleQuantityChange(event: InputEvent, productId: number) {
const value = Number((event.composedPath()[0] as HTMLInputElement).value);
const changedProductIndex = this.products.findIndex(
({ id }) => id === productId
);
if (changedProductIndex !== -1) {
this.products[changedProductIndex] = {
...this.products[changedProductIndex],
quantity: value
};
}
this.requestUpdate();
}
render() {
return html`
<div class="wrapper">
<table class="products">
<thead>
<tr>
<th>Product</th>
<th class="image-cell"></th>
<th>Quantity</th>
<th class="unit-price-cell">Price</th>
<th class="subtotal-cell">Subtotal</th>
</tr>
</thead>
<tbody>
${products.map(
(product) => html`
<tr>
<td class="image-cell">
<img
src="${product.image}"
alt="${product.name}"
class="image"
/>
</td>
<td class="content-cell">
<strong class="title">${product.name}</strong>
<div class="description">${product.description}</div>
</td>
<td>
<input
class="product-quantity-input"
value="${product.quantity}"
type="number"
id="${product.name}_quantity"
name="${product.name}_quantity"
min="0"
max="100"
@input="${(e) => this.handleQuantityChange(e, product.id)}"
/>
</td>
<td class="unit-price-cell">
${this.formatPrice(product.price)}
</td>
<td class="subtotal-cell">
${this.formatPrice(this.getProductSubTotal(product))}
</td>
</tr>
`
)}
</tbody>
<tfoot>
<tr>
<th class="image-cell"></th>
<th colspan="3" class="total-price-label">Total price</th>
<td class="total-price-value">
${this.formatPrice(this.totalPrice)}
</td>
</tr>
</tfoot>
</table>
</div>
`;
}
}
@customElement("shopping-cart-toggle")
export class ShoppingCartToggle extends LitElement {
static styles = css`
.dropdown {
position: relative;
}
.dropdown-content {
position: absolute;
inset-block-start: calc(100% + 0.5rem);
inset-inline-end: -0.5em;
background-color: var(--light);
color: var(--dark);
padding: calc(var(--spacer) / 2) var(--spacer);
box-shadow: var(--box-shadow);
z-index: 1;
inline-size: 29rem;
}
button {
-webkit-appearance: none;
padding: calc(var(--spacer) / 3) calc(var(--spacer) / 2);
border: 1px solid var(--link-color);
background: transparent;
color: var(--light);
font-weight: 800;
font-size: 1rem;
cursor: pointer;
}
button:hover,
button:focus {
background-color: var(--light);
color: var(--primary);
}
`;
@state()
protected isOpen = false;
handleDropdownToggle() {
this.isOpen = !this.isOpen;
this.requestUpdate();
}
render() {
return html`
<div class="dropdown">
<button @click="${this.handleDropdownToggle}">
<slot name="toggle" />
</button>
${this.isOpen
? html`
<div class="dropdown-content">
<slot name="content"></slot>
</div>
`
: ""}
</div>
`;
}
}
Also see: Tab Triggers