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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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
main
article.article
h2 Dynamic Sticky Sidebar Component
p Click on the elements below to add them to the sidebar component.
ul.items.grid
- for (let i = 0; i < 12; i++)
li
button.item
.thumbnail
.info
.title
.subtitle
p Added this extra content for spacing purposes. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed porta arcu, vitae hendrerit magna. Nullam nec mi augue. Morbi non odio mauris. Vivamus accumsan lectus ac libero ornare lacinia. Etiam quis purus nec augue fermentum interdum. Aliquam sit amet dui rutrum, aliquam augue ut, iaculis arcu. Nunc euismod, magna sit amet gravida gravida, erat diam scelerisque massa, id dignissim sapien elit quis mauris. In interdum lectus id mattis aliquam. Ut pellentesque accumsan consectetur. Curabitur varius mollis lacus, nec rhoncus massa facilisis ac.
p Maecenas fringilla consectetur blandit. Phasellus pulvinar, eros eleifend fermentum accumsan, leo massa rutrum lectus, nec cursus orci turpis at libero. Quisque dolor ex, suscipit id felis ac, bibendum aliquam enim. Curabitur mollis ligula a ipsum fermentum, in volutpat arcu mattis. Nulla facilisi. Duis mattis venenatis erat non sollicitudin. Phasellus nec augue eget magna eleifend laoreet. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Sed bibendum dolor euismod erat rhoncus, et mattis enim facilisis. Quisque arcu est, molestie sed massa nec, gravida feugiat massa. Donec id velit mi. Curabitur accumsan elit in magna mollis, quis pharetra felis faucibus.
p Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In molestie magna sit amet lobortis convallis. Etiam at magna eget risus venenatis luctus quis nec justo. Pellentesque elementum imperdiet mi. Vivamus nec fringilla est. Suspendisse tincidunt euismod turpis quis ornare. Donec et dui in massa porttitor malesuada. Aliquam erat volutpat.
aside.sidebar
.component
.header Selected Items
.content
.empty-text Your cart is currently empty
ul.items.list
.footer
button.button Checkout
button.button.empty-cart Empty Cart
footer
:root {
--space: 2rem;
--space-xs: calc(var(--space) / 3);
--space-sm: calc(var(--space) / 2);
--space-md: calc(var(--space) * 2);
--color-primary: lightgray;
--color-accent: whitesmoke;
--color-dark: black;
--color-mid: gray;
--color-light: white;
--color-highlight: dodgerblue;
--radius: 0.125rem;
}
/* Here are the base styles for the main layout and sticky component */
* {
box-sizing: border-box;
}
main {
display: flex;
flex-wrap: wrap;
}
.article {
flex-basis: 0;
flex-grow: 999;
min-width: 40%;
}
.sidebar {
--offset: var(--space);
flex-grow: 1;
flex-basis: 300px;
align-self: start;
position: sticky;
top: var(--offset);
}
.component {
display: grid;
grid-template-rows: auto 1fr auto;
}
.component .content {
max-height: 500px;
overflow-y: auto;
}
.sidebar .component {
max-height: calc(100vh - var(--offset) * 2);
}
/*
* Other styles
**/
.visually-hidden {
clip: rect(0 0 0 0);
clip-path: inset(50%);
height: 1px;
overflow: hidden;
position: absolute;
white-space: nowrap;
width: 1px;
}
html,
body {
height: 100%;
}
[hidden] {
display: none;
}
body {
margin: 0 auto;
padding: 0 var(--space);
font-family: "Helvetica Neue", sans-serif;
font-size: 1.25rem;
line-height: 1.4;
max-width: 1400px;
}
h2 {
font-size: clamp(2rem, 4vw, 3.5rem);
font-weight: 600;
line-height: 1.2;
}
p {
max-width: 70ch;
font-size: clamp(1rem, 4vw, 1.25rem);
}
header,
footer {
display: flex;
align-items: center;
justify-content: center;
margin: var(--space-md) 0;
padding: var(--space);
height: 40vh;
background: repeating-linear-gradient(
45deg,
transparent,
transparent 12px,
var(--color-accent) 12px,
var(--color-accent) 24px
);
}
/* Until flexbox gap is supported in all modern browsers, we use the negative margin trick to create space between the children elements */
main {
margin-bottom: calc(var(--space-md) * -1);
margin-left: calc(var(--space-md) * -1);
}
main > * {
margin-bottom: var(--space-md);
margin-left: var(--space-md);
}
article > * + * {
margin-top: var(--space);
}
.component {
position: relative;
border: 1px solid var(--color-primary);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 4px rgba(0, 0, 0, 0.04),
0 4px 8px rgba(0, 0, 0, 0.03), 0 8px 16px rgba(0, 0, 0, 0.03),
0 16px 32px rgba(0, 0, 0, 0.02), 0 32px 64px rgba(0, 0, 0, 0.02);
}
.component .header,
.component .footer {
padding: var(--space-sm);
text-align: center;
}
.component .header {
border-bottom: inherit;
}
.component .footer {
display: flex;
justify-content: space-between;
border-top: inherit;
}
.component .content {
padding: var(--space-sm);
max-height: 500px;
overflow-y: auto;
color: var(--color-dark);
}
.empty-text {
padding: var(--space);
text-align: center;
color: var(--color-primary);
}
.item {
position: relative;
appearance: none;
display: flex;
align-items: center;
padding: var(--space);
outline: none;
width: 100%;
background-color: var(--color-accent);
border: 1px solid var(--color-accent);
border-radius: var(--radius);
cursor: pointer;
}
.item:active {
transform: translateY(1px);
}
.item:focus {
outline: 2px solid var(--color-highlight);
}
.item > * {
pointer-events: none;
}
.info {
width: 100%;
}
.title,
.subtitle {
width: 100%;
height: 0.5rem;
background-color: var(--color-primary);
border-radius: var(--radius);
}
.title {
margin-bottom: var(--space-xs);
max-width: 15ch;
}
.subtitle {
max-width: 12ch;
}
.thumbnail {
flex-shrink: 0;
width: var(--size);
height: var(--size);
background: var(--color-primary);
border-radius: var(--radius);
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
grid-gap: var(--space-sm);
}
.grid .item {
flex-direction: column;
text-align: center;
}
.grid .info {
display: flex;
flex-direction: column;
align-items: center;
}
.grid .thumbnail {
--size: 100px;
margin-bottom: var(--space);
}
.list > * + * {
margin-top: var(--space-xs);
}
.list .item {
padding: var(--space-sm);
}
.list .item::before {
content: "×";
position: absolute;
top: 0;
right: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
padding-right: var(--space-sm);
font-size: 2rem;
text-align: center;
text-transform: uppercase;
letter-spacing: 0.05rem;
color: var(--color-primary);
pointer-events: none;
}
.list .thumbnail {
--size: 50px;
margin-right: var(--space-sm);
}
.button {
appearance: none;
padding: var(--space-xs) var(--space-sm);
font-family: inherit;
font-size: 1rem;
color: white;
background-color: var(--color-dark);
border: 1px solid var(--color-dark);
cursor: pointer;
border-radius: var(--radius);
box-shadow: rgba(black, 0.1) 4px 4px;
}
.button:active {
transform: translateY(1px);
box-shadow: none;
}
.empty-cart {
color: var(--color-mid);
border-color: var(--color-mid);
background-color: var(--color-light);
}
const items = document.querySelector(".items");
const componentItems = document.querySelector(".component .items");
const emptyText = document.querySelector(".empty-text");
const emptyBtn = document.querySelector(".empty-cart");
items.addEventListener("click", (e) => {
if (emptyText !== null) {
emptyText.setAttribute("hidden", true);
}
if (e.target.matches(".item")) {
const item = e.target.cloneNode(true);
componentItems.append(item);
}
});
componentItems.addEventListener("click", (e) => {
if (e.target.matches(".item")) {
e.target.remove();
}
if (componentItems.children.length < 1) {
emptyText.removeAttribute("hidden");
}
});
emptyBtn.addEventListener("click", (e) => {
while (componentItems.lastElementChild) {
componentItems.removeChild(componentItems.lastElementChild);
}
emptyText.removeAttribute("hidden");
});
Also see: Tab Triggers