Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
    <div class="container gradient-bg">
      <div class="threedee-container">
        <div class="box background"></div>

        <div class="box back">
          <div class="content grid-content">
            <p class="body">
              This is a fun little 3D card using JS to modulate CSS custom
              properties on mouse move and frame number with some constraints.
            </p>
          </div>
        </div>

        <div class="box middle">
          <div class="content grid-content">
            <button class="action">3D-ify</button>
          </div>
        </div>

        <div class="box front">
          <div class="content grid-content">
            <h2 class="title">3D Card</h2>
          </div>
        </div>
      </div>
    </div>
    <!--<script src="main.js"></script>-->

              
            
!

CSS

              
                html,
body {
  margin: 0;
  background-color: rgb(28, 17, 42);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  overflow: hidden;
}

body {
  --primary-highlight-400: hsl(178, 88%, 53%);
  --primary-highlight-500: hsl(178, 88%, 38%);
  --secondary-highlight-300: hsl(358, 88%, 71%);
  --secondary-highlight-400: hsl(358, 88%, 53%);
  --secondary-highlight-500: hsl(358, 88%, 38%);

  --primary-white-500: hsla(0, 0%, 100%, 0.9);
  --primary-white-400: hsla(0, 0%, 100%, 0.8);
  --primary-white-300: hsla(0, 0%, 100%, 0.6);
  --primary-white-200: hsla(0, 0%, 100%, 0.3);
  --primary-white-150: hsla(0, 0%, 100%, 0.05);
  --primary-white-100: hsla(0, 0%, 100%, 0);

  --primary-black-300: rgba(0, 0, 0, 0.3);
  --primary-black-200: rgba(0, 0, 0, 0.2);
  --primary-black-100: rgba(0, 0, 0, 0.1);

  --action-glow-spread-400: 1rem;
  --action-glow-spread-500: 2rem;
}

h1,
h2,
h3,
p,
button {
  all: unset;
  font-family: inherit;
  font-size: inherit;
}

.container {
  --bg-gradient-angle: 0deg;
  --bg-gradient-hue-start: 299;
  --bg-gradient-hue-end: 29;
  display: grid;
  justify-content: center;
  align-items: center;

  inline-size: 100vw;
  block-size: 100vh;
  block-size: 100dvh;
}

.gradient-bg {
  background-image: radial-gradient(
      circle at center,
      var(--primary-white-100),
      var(--primary-black-200)
    ),
    linear-gradient(
      var(--bg-gradient-angle),
      hsl(var(--bg-gradient-hue-start), 54%, 19%),
      hsl(var(--bg-gradient-hue-end), 56%, 19%)
    );
}

.threedee-container {
  --perspective: 2000px;

  --x-rotate: 0deg;
  --y-rotate: 0deg;
  --z-rotate: 0deg;

  --stack-scale: 1rem;

  --blur: 0;

  --glass-rotation: 67deg;
  --glass-stop-start: 30%;
  --glass-stop-middle: 50%;
  --glass-stop-end: 70%;
  --glass-alpha-low: 0;
  --glass-alpha-high: 1;

  perspective: var(--perspective);
}

.box {
  position: absolute;

  inline-size: 20rem;
  block-size: 20rem;

  border: 0.5px solid var(--primary-black-100);
  border-radius: 1rem;
  background-color: var(--primary-white-150);
  background-image: linear-gradient(
    var(--glass-rotation),
    rgba(255, 255, 255, var(--glass-alpha-low)) var(--glass-stop-start),
    rgba(255, 255, 255, var(--glass-alpha-high)) var(--glass-stop-middle),
    rgba(255, 255, 255, var(--glass-alpha-low)) var(--glass-stop-end)
  );
}

.background {
  background-image: linear-gradient(
    148deg,
    hsla(267, 20%, 20%, 0.6),
    hsla(340, 20%, 30%, 0.6)
  );

  transform: translate(-50%, -50%) scale(1.1) rotateX(var(--x-rotate))
    rotateY(var(--y-rotate)) rotateZ(var(--z-rotate))
    translateZ(calc(var(--stack-scale) * -1));
}

.back {
  pointer-events: none;

  transform: translate(-50%, -50%) rotateX(var(--x-rotate))
    rotateY(var(--y-rotate)) rotateZ(var(--z-rotate))
    translateZ(calc(var(--stack-scale) * 0));

  filter: blur(var(--blur));
}

.middle {
  transform: translate(-50%, -50%) rotateX(var(--x-rotate))
    rotateY(var(--y-rotate)) rotateZ(var(--z-rotate))
    translateZ(calc(var(--stack-scale) * 1));

  filter: blur(var(--blur));
}

.front {
  pointer-events: none;

  transform: translate(-50%, -50%) rotateX(var(--x-rotate))
    rotateY(var(--y-rotate)) rotateZ(var(--z-rotate))
    translateZ(calc(var(--stack-scale) * 2));

  filter: blur(var(--blur));
}

.content {
  padding: 1rem;
}

.grid-content {
  display: grid;
  grid-template-areas:
    "title title title title"
    "text text text text"
    "text text text text"
    "action action action action";
  grid-template-rows: repeat(4, 1fr);

  block-size: 100%;
}

.title {
  grid-area: title;
  align-self: center;

  margin: 0;

  font-weight: 100;
  font-size: 3rem;

  color: var(--primary-highlight-400);
}

.body {
  grid-area: text;

  font-size: 1.2rem;
  line-height: 1.5;
  font-weight: 300;

  color: var(--primary-white-400);
}

.action {
  cursor: pointer;
  outline: none;

  padding: 1rem 2rem;

  text-transform: uppercase;
  font-size: 0.85rem;

  grid-area: action;
  justify-self: end;
  align-self: start;

  color: rgba(0, 0, 0, 0.6);
  background-color: var(--primary-highlight-400);
  border: 0.5px solid var(--primary-black-200);
  border-radius: 5px;
  box-shadow: 0 0 var(--action-glow-spread-400) 0 var(--primary-white-100);

  transition: background-color 150ms ease-in-out, color 150ms ease-in-out,
    box-shadow 500ms ease-in-out, border 500ms ease-in-out;
}

.action:is(:hover, :focus) {
  color: var(--primary-white-400);
  background-color: var(--primary-highlight-500);
  border: 0.5px solid var(--secondary-highlight-400);
  box-shadow: 0 0 var(--action-glow-spread-400) 0 var(--primary-white-300);
}

.action:active {
  color: var(--primary-white-500);
  border: 0.5px solid var(--secondary-highlight-300);
  box-shadow: 0 0 var(--action-glow-spread-500) 0 var(--primary-white-400);

  transition: box-shadow 75ms ease-in-out;
}

              
            
!

JS

              
                (function () {
  let width, height;

  // set initial sizing
  height = window.innerHeight;
  width = window.innerWidth;

  // For the devs who like to mess with the screen ;)
  window.addEventListener("resize", () => {
    height = window.innerHeight;
    width = window.innerWidth;
  });

  // In this example, we just grab the mouse for some animation fun
  let screenX, screenY;
  let endDelta = {x: 0, y: 0};
  let startDelta = {x: 0, y: 0};
  document.addEventListener("mousemove", (e) => {
    const { x, y } = e;
    screenY = y / height;
    screenX = x / width;
  });

  // Adding some touch events for mobile functionality... comming soon, need to get my head around it. :/
  window.addEventListener("touchmove", (e) => {
    const touch = e.touches[0];
    const { clientX: x, clientY: y } = touch;
    screenY = y / height;
    screenX = x / width;
  });
  
  window.addEventListener("touchstart", (e) => {
    const touch = e.touches[0];
    const {clientX: x, clientY: y} = touch
    startDelta = {x, y};
  })
  
  window.addEventListener("touchend", (e) => {
    const touch = e.touches[0];
    const {clientX: x, clientY: y} = touch
    endDelta = {x, y};
  })

  const containerHandle = document.querySelector(".container");
  const cardHandle = document.querySelector(".threedee-container");

  // Little helper to register and set css properties for html element styles
  function cssPropertyModifier(element) {
    const propertyControllers = [];

    function registerProperties(...propsTuple) {
      propsTuple.forEach((propTuple) => propertyControllers.push(propTuple));
    }
    function setProperties(t) {
      const styleString = propertyControllers.reduce((str, controller) => {
        return (str += `${controller[0]}:${controller[1](t)};`);
      }, "");
      element.style = styleString;
    }
    return {
      registerProperties,
      setProperties
    };
  }

  // Helper to set some sensible boundaries, assumes 0 <= t <= 1
  const boundaries = (min = 0, max = 1, t = 0) => {
    return min + t * (max - min);
  };

  // Lots of secret nunbers – this is the creative bit. Animate all the things!
  // Property Modifier Tuple – [css-property-name, frame-number => do fun stuff here...]
  // Make sure the units returned by the modifier function make sense to css

  const onMouseMoveColorCycle = cssPropertyModifier(containerHandle);
  onMouseMoveColorCycle.registerProperties(
    [
      "--bg-gradient-angle",
      (t) => `${boundaries(-60, 167, Math.sin(t / 500))}deg`
    ],
    [
      "--bg-gradient-hue-start",
      (t) =>
        `${boundaries(279, 330 + 45 * Math.cos(t / 300), screenX * screenY)}`
    ],
    [
      "--bg-gradient-hue-end",
      (t) => `${boundaries(10 + 30 * Math.sin(t / 400), 70, screenX * screenY)}`
    ]
  );

  const onMouseMoveRotate = cssPropertyModifier(cardHandle);
  onMouseMoveRotate.registerProperties(
    ["--x-rotate", () => `${boundaries(-35, 35, screenY)}deg`],
    ["--y-rotate", () => `${boundaries(35, -35, screenX)}deg`],
    ["--blur", () => `${boundaries(0, 0.6, Math.pow(2 * screenX - 1, 2))}px`],
    [
      "--glass-rotation",
      (t) =>
        `${boundaries(
          -12,
          95 - 22 * Math.sin(t / 120),
          Math.pow(2 * screenX - 1, 3)
        )}deg`
    ],
    [
      "--glass-alpha-low",
      () => `${boundaries(0, 0.03, Math.pow(2 * screenX - 1, 2))}`
    ],
    [
      "--glass-alpha-high",
      () => `${boundaries(0.1, 0.04, Math.pow(2 * screenX - 1, 2))}`
    ],
    [
      "--glass-stop-start",
      () => `${boundaries(4, 20, Math.pow(2 * screenX - 1, 2))}%`
    ],
    [
      "--glass-stop-middle",
      () => `${boundaries(20, 90, Math.pow(2 * screenX - 1, 2))}%`
    ],
    [
      "--glass-stop-end",
      () => `${boundaries(8, 110, Math.pow(2 * screenX - 1, 2))}%`
    ]
  );

  // Build Animator initalised with functions to animate then returns an updater
  // update takes a frame limit (perf maybe?) and returns an animation cancel (reduce motion?)

  function buildAnimator(...fns) {
    let frame = 0;
    return function update(frameLimit = 1) {
      if (frame % frameLimit === 0) {
        fns.forEach((fn) => {
          fn(frame);
        });
      }
      frame = requestAnimationFrame(() => update(frameLimit));
      return function cancel(
        cleanUp = ({ frame, fns }) =>
          console.info("animation cancelled", { frame, fns })
      ) {
        // pass in some clean up actions
        cancelAnimationFrame(frame);
        cleanUp({ frame, fns });
      };
    };
  }

  // Animate the things!
  const update = buildAnimator(
    onMouseMoveRotate.setProperties,
    onMouseMoveColorCycle.setProperties
  );

  // Set frame limiting, run animator fns every "n" RAF's
  const cancel = update(4);
})();

              
            
!
999px

Console