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.
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.
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.
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.
<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template lang="pug">
#app
.mondrian__container(v-bind:style="{'--initial-height': initialHeight}")
.mondrian
.mondrian__block(v-for="(block, index) in blocks" v-bind:style="{'--index': index, '--row': block.rowSpan, '--col': block.colSpan, '--hue': block.hue, '--saturation': block.saturation, '--lightness': block.lightness, '--delay': block.delay}")
img(v-bind:src="'https://picsum.photos/' + (block.colSpan * 1) + '00/' + (block.rowSpan * 1) + '00?' + index")
.mondrian__trigger
</template>
<script>
export default {
// Do NOT need this!
// el: '#app',
data: {
initialHeight: 0,
blocks: [],
colors: [
// Red
{
hue: 354,
saturation: 100,
lightness: 42
},
// White
{
hue: 105,
saturation: 17,
lightness: 95
},
// Blue
{
hue: 211,
saturation: 87,
lightness: 34
},
// Yellow
{
hue: 51,
saturation: 94,
lightness: 57
},
]
},
methods: {
generateBlocks: function(limit = 3) {
for (let i = 0; i < 5; i++) {
const randoColor = this.colors[Math.floor(Math.random() * this.colors.length)]
this.blocks.push({
colSpan: Math.floor(Math.random() * limit + 1),
rowSpan: Math.floor(Math.random() * limit + 1),
delay: i,
// Spread a color onto the data!
...(randoColor)
})
}
},
regenerate: function() {
this.blocks = []
setTimeout(() => this.generateBlocks(), 0)
},
setUpObserver: function() {
let options = {
rootMargin: '0px',
threshold: 1.0
}
const callback = () => {
if ( window.scrollY !== 0) {
this.generateBlocks()
}
}
const observer = new IntersectionObserver(callback, options)
const target = document.querySelector('.mondrian__trigger')
observer.observe(target)
}
},
updated: function () {
if (!this.initialHeight) {
this.initialHeight = document.querySelector('.mondrian').offsetHeight
this.setUpObserver()
}
},
mounted: function () {
this.generateBlocks(2)
}
};
</script>
<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style lang="stylus">
:root
--black hsl(150, 13%, 3%)
--yellow hsl(51, 94%, 57%)
--white hsl(105, 17%, 95%)
--blue hsl(211, 87%, 34%)
--red hsl(354, 100%, 42%)
--block 50
--height calc(var(--block) * 6)
--width calc(var(--block) * 5 + (6 * 10))
*
box-sizing border-box
body
html
background hsl(0, 0%, 93%)
display flex
align-items center
justify-content center
min-height 100vh
font-family sans-serif
text-align center
// This is our container, we need this to be full height
// with a little piece on the bottom that triggers the observer
.mondrian__container
min-height 100vh
display flex
align-items center
justify-content center
padding calc(50vmin - ((var(--initial-height) / 2) * 1px)) 0
.mondrian__trigger
height 10px
.mondrian
--block-size calc(var(--block) * 1px)
background var(--black)
// padding 10px
border 10px solid var(--black)
box-shadow 5px 10px 10px #aaa
display grid
// grid-auto-columns var(--block-size)
grid-auto-flow dense
grid-auto-rows minmax(50px, 10vmin)
grid-gap 10px
grid-template-columns repeat(5, minmax(50px, 1fr))
min-width calc(var(--width) * 1px)
width calc(50vmin + 60px)
&__block
animation scaleIn 0.15s calc(var(--delay) * 0.1s) ease both
background 'hsl(%s, %s, %s)' % (var(--hue, 105) calc(var(--saturation, 17) * 1%) calc(var(--lightness, 95) * 1%))
grid-row span var(--row)
grid-column span var(--col)
position relative
overflow hidden
img
width 100%
height 100%
object-fit cover
@keyframes scaleIn
from
transform scale(0)
</style>
Also see: Tab Triggers