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="product-area">
<div class="section-header">
<p>Clothing - The Winter Edit</p>
</div>
<div class="product-list four-by">
<div class="product-card-container" style="--detail: new">
<div class="product-card">
<div class="media-img">
<img src="https://images.unsplash.com/photo-1574201635302-388dd92a4c3f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=765&q=80" alt="">
<div class="comment-block"></div>
</div>
<div class="meta">
<p class="brand">Unicorn</p>
<p class="name">Magic Sweater</p>
<p class="price">$148</p>
<div class="colors">
<ul class="color-list">
<li data-color="rgb(x,x,x)">white</li>
<li data-color="rgb(x,x,x)">beige</li>
<li data-color="rgb(x,x,x)">maroon</li>
<li data-color="rgb(x,x,x)">black</li>
<li data-color="rgb(x,x,x)">brown</li>
<li data-color="rgb(x,x,x)">deeppurple</li>
</ul>
</div>
</div>
</div>
</div>
<div class="product-card-container" style="--detail: low-stock">
<div class="product-card">
<div class="media-img">
<img src="https://images.unsplash.com/photo-1579954568250-8b7abc7743e7?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=685&q=80" alt="">
<div class="comment-block"></div>
</div>
<div class="meta">
<p class="brand">Markky</p>
<p class="name">On The Go Sweater</p>
<p class="price">$68</p>
<div class="colors">
<ul class="color-list">
<li>green</li>
<li>beige</li>
<li>deeppink</li>
<li>darkblue</li>
<li>orange</li>
<li>brown</li>
<li>gold</li>
<li>white</li>
<li>beige</li>
<li>copper</li>
</ul>
</div>
</div>
</div>
</div>
<div class="product-card-container">
<div class="product-card">
<div class="media-img">
<img src="https://images.unsplash.com/photo-1601762319935-963f5b1869ad?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=723&q=80" alt="">
<div class="comment-block"></div>
</div>
<div class="meta">
<p class="brand">Chaide</p>
<p class="name">Slouch Trench</p>
<p class="price">$368</p>
<div class="colors">
<ul class="color-list">
<li>beige</li>
<li>tan</li>
<li>gold</li>
<li>brown</li>
<li>black</li>
</ul>
</div>
</div>
</div>
</div>
<div class="product-card-container">
<div class="product-card">
<div class="media-img">
<img src="https://images.unsplash.com/photo-1641932481849-9ac1cacf360c?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=731&q=80" alt="">
<div class="comment-block"></div>
</div>
<div class="meta">
<p class="brand">Marley & Co</p>
<p class="name">Essential Blazer</p>
<p class="price">$198</p>
<div class="colors">
<ul class="color-list">
<li>white</li>
<li>black</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
/* style query demo */
/* want to see if the --detail var is not blank, then set the content to the content of its value */
@container style(--detail: new) {
/* Can i reduce redundancy here? */
.comment-block {
display: block;
}
.comment-block::after {
content: 'New';
/* I shouldn't have to have this here */
border: 1px solid currentColor;
background: white;
position: absolute;
bottom: 0.5rem;
right: 0.5rem;
padding: 0.1rem 0.3rem;
}
}
@container style(--detail: low-stock) {
/* Can i reduce redundancy here? */
.comment-block {
display: block;
}
.comment-block::after {
content: 'Low Stock';
border: 1px solid currentColor;
background: white;
position: absolute;
bottom: 0.5rem;
right: 0.5rem;
padding: 0.1rem 0.3rem;
}
.media-img {
border: 2px solid #c17474;
}
}
/* CQ size adjustments */
@container (min-width: 400px) {
.product-card p {
font-size: 1.2rem;
}
.color-list li {
width: 1rem;
height: 1rem;
}
.comment-block:after {
bottom: 1rem;
right: 1rem;
font-size: 1.2rem;
}
}
/* MQ changes */
@media (max-width: 800px) {
.view-buttons {
display: none;
}
.four-by {
grid-template-columns: 1fr 1fr;
}
}
.product-card {
width: 100%;
}
@layer product-demo-base {
.comment-block {
display: none;
}
}
@layer product-base {
.product-area {
max-width: 1200px;
margin: 0 auto;
}
.product-list {
display: grid;
gap: 1rem;
}
.media-img {
position: relative;
aspect-ratio: 3 / 4;
width: 100%;
box-sizing: border-box;
}
.media-img img {
width: 100%;
height: 100%;
display: block;
object-fit: cover;
}
.meta {
padding: 0.5rem 0.5rem 0;
}
.meta p {
margin: 0;
}
.color-list {
margin-top: 1rem;
padding: 0;
display: flex;
gap: 0.25rem;
}
.color-list li {
font-size: 0;
width: 0.75rem;
height: 0.75rem;
display: inline-block;
border-radius: 50%;
position: relative;
border: 1px solid;
}
.color-list li:hover,
.color-list li:focus {
border-color: white;
}
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.section-header button {
border: none;
background: none;
font-family: Roboto Mono, monospace;
}
.section-header button:hover,
.section-header button:focus {
text-decoration: underline;
}
.four-by {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
/* attr() color interpolation or getting value from a data attr would be really useful for these color lists */
.color-list li {
background-color: red;
}
/* Mini :has() demo */
/* Only show 5 colors, but hint at more */
/* If the color list has 6 or more children, apply a (+ more) indicator to the 5th child and hide the rest */
.color-list:has(:nth-child(n + 6)) li:nth-child(5)::after {
content: '+ more colors';
display: inline;
position: absolute;
left: calc(100% + 0.5rem);
width: max-content;
font-size: 0.75rem;
}
.color-list:has(:nth-child(n + 6)) li:nth-child(n + 6) {
display: none;
}
}
@layer page-base {
body {
font-family: Roboto Mono, monospace;
}
}
// Inspired by the products on aritzia.com
// Images from Unsplash
// Style Queries: Apply "new" or "low stock" tags, style media image if "low stock" tag
// :has(): showcases quantity queries
// what I would like to do instead is use data attributes to set the colors in HTML and apply them in CSS
const colors = [...document.querySelectorAll('.color-list li')]
colors.forEach((color) => {
// using innerText for ease but would likely use data attributes for more specific colors
const colorValue = color.innerText;
color.style.background = colorValue;
})
Also see: Tab Triggers