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="box">
<div class="image-wrap">
<div class="image image-0"></div>
</div>
<div class="border"></div>
<div class="text">
<span>Sugar plum</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-1"></div>
</div>
<div class="border"></div>
<div class="text">
<span>apple pie</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-2"></div>
</div>
<div class="border"></div>
<div class="text">
<span>gummy bears</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-3"></div>
</div>
<div class="border"></div>
<div class="text">
<span>sesame snaps</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-4"></div>
</div>
<div class="border"></div>
<div class="text">
<span>chupa chups</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-5"></div>
</div>
<div class="border"></div>
<div class="text">
<span>chocolate cookie</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-6"></div>
</div>
<div class="border"></div>
<div class="text">
<span>cheescake muffin</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-7"></div>
</div>
<div class="border"></div>
<div class="text">
<span>sweet toffee</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-8"></div>
</div>
<div class="border"></div>
<div class="text">
<span>carrot cake</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-9"></div>
</div>
<div class="border"></div>
<div class="text">
<span>lollipop</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-10"></div>
</div>
<div class="border"></div>
<div class="text">
<span>macaroon</span>
</div>
</div>
<div class="box">
<div class="image-wrap">
<div class="image image-11"></div>
</div>
<div class="border"></div>
<div class="text">
<span>lemon tart</span>
</div>
</div>
<div class="unsplash-info">all images in this demo are from <a target="_blank" href="https://unsplash.com">unsplash.com</a></div>
html, body {
width: 100%;
}
body {
font-size: 40px;
font-family: 'Bungee Shade', cursive;
color: #222;
background: #222;
}
.box {
position: relative;
overflow: hidden;
cursor: pointer;
float: left;
&:hover {
.border {
transform: scale(0.94);
transition-duration: 140ms;
}
.text {
opacity: 1;
transform: translate3d(0,0,0);
transition-duration: 140ms;
}
.image-wrap {
transform: scale(1);
opacity: 1;
transition-duration: 140ms;
}
}
}
.border, .text, .image {
width: 100%;
height: 100%;
position: absolute;
pointer-events: none;
}
.image-wrap {
position: absolute;
width: 130%;
height: 130%;
left: -15%;
top: -15%;
transform: scale(0.8);
transition: 280ms ease-out;
pointer-events: none;
opacity: 0.74;
}
.border {
left: -30px;
top: -30px;
border: 30px solid #222;
box-sizing: content-box;
transition: 360ms ease-in-out;
}
.text {
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
transform: translate3d(0, -4%, 0);
transition: 280ms ease-out;
text-align: center;
}
.unsplash-info {
background: #111;
color: #fff;
width: 100%;
position: fixed;
bottom: 0px;
font-family: Arial, sans-serif;
font-size: 14px;
padding: 2px 8px;
a {
text-decoration: none;
color: #eee;
}
}
.image {
background-size: cover;
}
/* box sizing */
.box {
width: 100%;
height: 100vw;
float: left;
}
@media (min-width: 500px) {
.box {
width: 50%;
height: 50vw;
}
}
@media (min-width: 800px) {
.box {
width: 33.333333%;
height: 33.33333vw;
}
}
@media (min-width: 1200px) {
.box {
width: 25%;
height: 25vw;
}
}
/* image backgrounds */
.image-0 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_0.jpg);
}
.image-1 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_1.jpg);
}
.image-2 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_2.jpg);
}
.image-3 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_3.jpg);
}
.image-4 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_4.jpg);
}
.image-5 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_5.jpg);
}
.image-6 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_6.jpg);
}
.image-7 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_7.jpg);
}
.image-8 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_8.jpg);
}
.image-9 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_9.jpg);
}
.image-10 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_10.jpg);
}
.image-11 {
background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/53148/box-image_11.jpg);
}
// for storing mouse x / y position
var mousePos = {
x: -10,
y: -10
};
// select the .box DOM elements
var boxElements = document.getElementsByClassName('box');
// create an array of objects to store the box elements and their image
// positions
var boxes = [];
for (var i = 0; i < boxElements.length; i++) {
boxes.push({
el: boxElements[i],
targetX: 0,
targetY: 0,
prevX: 0,
prevY: 0,
x: 0,
y: 0,
left: boxElements[i].offsetLeft,
top: boxElements[i].offsetTop,
size: boxElements[i].offsetWidth
})
}
function mousemove(e) {
// update mouse position
mousePos.x = e.pageX;
mousePos.y = e.pageY;
}
function updateBox(box) {
// check if mouse is in box area
if (mousePos.x > box.left && mousePos.x < (box.left+box.size) &&
mousePos.y > box.top && mousePos.y < (box.top+box.size)) {
// the mouse is in the space over the box - update the box image target position dependent on how far the mouse position is from the center of the box (box size/2)
box.targetX = (box.size/2 - (mousePos.x - box.left)) * 0.1;
box.targetY = (box.size/2 - (mousePos.y - box.top)) * 0.1;
} else {
// otherwise the box isn't being hovered, its target is 0
box.targetX = 0;
box.targetY = 0;
}
// update the image element position by lerping position to target
// https://codepen.io/rachsmith/post/animation-tip-lerp
box.x += (box.targetX - box.x)*0.2;
box.y += (box.targetY - box.y)*0.2;
// check that the values aren't really small already, to overcome javascripts poor handling of high precision math
if(Math.abs(box.x) < .001) box.x = 0;
if(Math.abs(box.y) < .001) box.y = 0;
// only update CSS if the position has changed since last loop
if (box.prevX !== box.x && box.prevY !== box.y) {
// update css of image element
box.el.children[0].children[0].style.transform = 'translate3d('+box.x+'px, '+box.y+'px, 0)';
}
// update prev values for next comparison
box.prevX = box.x;
box.prevY = box.y;
}
function loop() {
// in the loop - updated each of the boxes
for (var i = 0, l = boxes.length; i < l; i++) {
updateBox(boxes[i]);
}
requestAnimationFrame(loop);
}
function resize() {
// the box positions/sizes have updated on resize, so they need to be
// reset
for (var i = 0; i < boxes.length; i++) {
boxes[i].left = boxes[i].el.offsetLeft;
boxes[i].top = boxes[i].el.offsetTop;
boxes[i].size = boxes[i].el.offsetWidth;
}
}
// attach the mouse event listener to the document
document.addEventListener('mousemove', mousemove);
// listen for resize event, so box sizes can be updated
window.addEventListener('resize', resize);
// run the animation loop
loop();
Also see: Tab Triggers