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.
<h1>A demo of new CSS to accomplish a Masonry-style layout</h1>
<h3>Compare Display modes:</h3>
<input type="radio" name="display" value="masonry" id="masonry" checked> <label for="masonry">Masonry (in Safari Technology Preview or other <a href="https://caniuse.com/?search=masonry">supported browsers</a>)</label><br>
<input type="radio" name="display" value="grid" id="grid"> <label for="grid">Grid 1 (also the fallback)</label><br>
<input type="radio" name="display" value="multicolumn" id="multicolumn"> <label for="multicolumn">Multicolumn</label><br>
<input type="radio" name="display" value="flexbox" id="flexbox"> <label for="flexbox">Flexbox (row)</label><br>
<input type="radio" name="display" value="flexbox2" id="flexbox2"> <label for="flexbox2">Flexbox (column)</label>
<p>Be sure to <a href="#note">read the note</a> about this demo. Also, I made <a href="https://codepen.io/jensimmons/full/vYNeRZw" target="_blank">another demo</a> that shows off what Masonry in CSS Grid can do — far beyond the JS libraries of today.</p>
<main>
<figure><figcaption>1</figcaption><img src="https://images.unsplash.com/photo-1587383378486-83d683d9d02d?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>2</figcaption><img src="https://images.unsplash.com/photo-1588395856595-235a6a1ccca8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>3</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1587825027984-c4476461c8f9.jpg"></figure>
<figure><figcaption>4</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1588503391289-5f7b28258022.jpg"></figure>
<figure><figcaption>5</figcaption><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/10558/photo-1586603234056-2158d916b705.jpg"></figure>
<figure><figcaption>6</figcaption><img src="https://images.unsplash.com/photo-1587462829651-7fd0208d766e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>7</figcaption><img src="https://images.unsplash.com/photo-1585652505968-6266ecf26133?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>8</figcaption><img src="https://images.unsplash.com/photo-1586450463118-8d0cddab713f?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>9</figcaption><img src="https://images.unsplash.com/photo-1587993988198-a3d05e119eb0?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>10</figcaption><img src="https://images.unsplash.com/photo-1587750059642-d55dfc265acd?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>11</figcaption><img src="https://images.unsplash.com/photo-1586692283466-6e08f395f627?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>12</figcaption><img src="https://images.unsplash.com/photo-1586796105950-b885786d8597?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>13</figcaption><img src="https://images.unsplash.com/photo-1587169544748-d21bd810f57e?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>14</figcaption><img src="https://images.unsplash.com/photo-1586943101559-4cdcf86a6f87?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>15</figcaption><img src="https://images.unsplash.com/photo-1586767451503-204b55047cb2?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>16</figcaption><img src="https://images.unsplash.com/photo-1586754102101-36b67e4c5bcf?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>17</figcaption><img src="https://images.unsplash.com/photo-1585791022456-be89ddf09c75?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>18</figcaption><img src="https://images.unsplash.com/photo-1587063749684-d4926a4fc233?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>19</figcaption><img src="https://images.unsplash.com/photo-1586695940356-4b1c61eabbc7?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>20</figcaption><img src="https://images.unsplash.com/photo-1587151711096-23c51f92c920?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>21</figcaption><img src="https://images.unsplash.com/photo-1587068786187-db37681155f8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>22</figcaption><img src="https://images.unsplash.com/photo-1585841960320-f70aa9b7da69?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>23</figcaption><img src="https://images.unsplash.com/photo-1586968099622-45379e907f32?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>24</figcaption><img src="https://images.unsplash.com/photo-1588046883141-44883b3c0f15?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
<figure><figcaption>25</figcaption><img src="https://images.unsplash.com/photo-1587061966530-a50125ba8512?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjE0NTg5fQ"></figure>
</main>
<aside id="note">
<h2>About this demo</h2>
<p>For years, websites (like <a href="https://www.pinterest.com/">Pinterest</a>) have wanted a certain layout that packs together different-sized cards of content, while allowing a source order where the content runs across the columns. This layout keeps closely-ordered content visually together above the fold, and allows lazy-loading of new content to the bottom of the page. As of early 2020, this layout requires a JavaScript solution, such as the popular <a href="https://masonry.desandro.com/">Masonry</a> library.</p>
<p>The CSS Working Group is currently pursuing a way to do this in 100% CSS. Mozilla is proposing one way to do this — and has implemented the idea, so that people can try it out and see what they think. This works in April/May 2020.</p>
<p>You can try it in <a href="http://nightly.mozilla.org/">Firefox Nightly</a>. Type <code>about:config</code> in the URL bar. Then search for “masonry”. And set <code>layout.css.grid-template-masonry-value.enabled</code> to true.</p>
<p>This is an experimental implementation — being discussed as a <em>possible</em> CSS specification. It is NOT yet official, and likely will change. Do not write blog posts saying this is definitely a thing. It's not a thing. Not yet. It's an experiment. A prototype. If you have thoughts, chime in at <a href="https://github.com/w3c/csswg-drafts/issues/4650">the CSSWG</a>.</p>
</aside>
/* --- Masonry --- */
[value="masonry"]:checked ~ main {
display: grid;
grid-template-rows: masonry;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
gap: 1rem;
}
/* --- Grid --- */
[value="grid"]:checked ~ main {
display: grid;
grid-template-rows: auto;
grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr));
gap: 1rem;
}
/* --- Multicolumn --- */
[value="multicolumn"]:checked ~ main {
display: block;
columns: 12rem;
gap: 1rem;
}
[value="multicolumn"]:checked ~ main figure {
margin-bottom: 1rem;
break-inside: avoid;
}
/* --- Flexbox, row direction --- */
[value="flexbox"]:checked ~ main {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
[value="flexbox"]:checked ~ main figure {
flex: 1 1 12rem;
}
/* --- Flexbox, column direction --- */
[value="flexbox2"]:checked ~ main {
display: flex;
flex-wrap: wrap;
flex-direction: column;
max-height: 200vh;
justify-content: start;
gap: 1rem;
}
[value="flexbox2"]:checked ~ main figure {
width: 100%;
max-width: 16rem;
}
/* --- The rest of the styling --- */
* {
box-sizing: border-box;
}
body {
font-family: avenir, roboto, helvetica, sans-serif;
margin: 2rem calc(2vw + 2rem);
}
main {
margin: 2rem 0;
}
img {
width: 100%;
display: block;
border-radius: 0 0 1rem 1rem;
}
figure {
margin: 0;
}
figcaption {
padding: 0.5rem 1rem 0.4rem;
background: #ddd;
color: #333;
border-radius: 1rem 1rem 0 0;
text-align: end;
}
h1 {
margin: 0;
}
h2 {
margin: 0;
}
h3 {
margin: 0.5rem 0 0;
font-size: 1rem;
}
aside {
margin: 6rem 0;
max-width: 80ch;
padding: 2rem 2rem 1rem;
border: 1px solid #000;
}
p {
margin: 1rem 0;
color: #444;
font-weight: 400;
}
code {
font-size: 1.07rem;
color: #d45b2c;
}
input {
margin: 0;
}
label {
margin: 0 1.1rem 0 0.1rem;
}
Also see: Tab Triggers