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

              
                <!-- Reverse engineering from https://www.toni.li/about -->

<activities-widget>

  <div class="activity">
    <div class="img"><img src='https://images.unsplash.com/photo-1723274565296-2945e2ebc306?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MzI0NzE1OTF8&ixlib=rb-4.0.3&q=80&w=400' alt=''></div>
    <div class="text">
      <h3>Ice Catch</h3>
      <p>It's like regular catch, but it's on ice so it's cooler literally.</p>
    </div>
  </div>

  <div class="activity">
    <div class="img"><img src='https://images.unsplash.com/photo-1607930232028-f01079639b00?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MzI0NzIxMjV8&ixlib=rb-4.0.3&q=80&w=400' alt=''></div>
    <div class="text">
      <h3>Mud Touching</h3>
      <p>Mud isn't going to touch itself.</p>
    </div>
  </div>

  <div class="activity">
    <div class="img"><img src='https://images.unsplash.com/photo-1677757103853-a304b6a182f5?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MzI1NjgyODB8&ixlib=rb-4.0.3&q=80&w=400' alt=''></div>
    <div class="text">
      <h3>BMX Football</h3>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus enim hic quo amet?</p>
    </div>
  </div>

  <div class="activity">
    <div class="img"><img src='https://images.unsplash.com/photo-1676312830459-f6f13dfdd899?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MzI3MjQ5Nzh8&ixlib=rb-4.0.3&q=80&w=400' alt=''></div>
    <div class="text">
      <h3>Shoe Tying</h3>
      <p>I love tying shoes. Bunny style. The other bunny style. All the styles.</p>
    </div>
  </div>

</activities-widget>
              
            
!

CSS

              
                :root {
  color-scheme: light dark;
}

@property --imgRotate {
  syntax: "<angle>";
  inherits: true;
  /* would be a cool use case for random() */
  initial-value: 0deg;
}

body {
  height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
  font-family: system-ui, sans-serif;
  line-height: 1.5;
}

activities-widget {
  container: activities-widget / inline-size;

  inline-size: min(100%, 425px);
  margin-inline: auto;

  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1lh auto auto;
  gap: 0.5rem 2rem;

  > * {
    grid-area: 1 / 1 / 1 / 1;
  }

  > .activity {
    display: grid;
    grid-template-rows: subgrid;
    grid-template-columns: subgrid;

    grid-column: 1 / -1;
    grid-row: 1 / -1;

    /* Using a div wrapper here because Firefox was blowing out the grid when making the image itself aspect-ratio: 1; */
    .img {
      grid-column: 1 / 2;
      grid-row: 1 / 4;
      width: 100%;
      aspect-ratio: 1;

      overflow: clip;
      border-radius: 8px;
      border: 3px solid light-dark(white, #333);
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);

      rotate: var(--imgRotate, 0deg);
      transition: 0.2s;

      > img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        display: block;
      }
    }

    &:nth-child(1) {
      .img {
        --imgRotate: 4deg;
      }
    }
    &:nth-child(2) {
      .img {
        --imgRotate: -2deg;
      }
    }
    &:nth-child(3) {
      .img {
        --imgRotate: -9deg;
      }
    }
    &:nth-child(4) {
      .img {
        --imgRotate: 7deg;
      }
    }

    .text {
      grid-column: 2 / 3;
      grid-row: 2 / 3;

      color: light-dark(rgba(30, 30, 30, 0.6), rgba(240, 240, 240, 0.6));
      opacity: 0;

      translate: 0 20px;
      transition: 0.5s cubic-bezier(0.85, 0, 0.15, 1);

      pointer-events: none;

      h3 {
        color: light-dark(black, white);
        margin: 0;
      }
    }

    &.active {
      .text {
        opacity: 1;
        translate: 0 0;
        pointer-events: all;
      }

      .img {
        animation: 0.66s moveOutIn cubic-bezier(0.34, 1.56, 0.64, 1);
        animation-fill-mode: forwards;
      }
    }

    @container activities-widget (width < 300px) {
      display: block;
      padding: 1rem;

      .activity {
        display: block;
        margin-block-end: 3rem;
        .img {
          max-inline-size: 125px;
          margin-inline: auto;
          margin-block-end: 0.5rem;
        }
        .text {
          opacity: 1;
          translate: 0 0;
          pointer-events: all;
          background: none;
        }
      }
    }
  }

  &.children-animating {
    .img {
      animation: 0.75s straightenImages;
    }
  }

  .activities-navigation {
    grid-column: 2 / 3;
    grid-row: 3 / 4;

    user-select: none;
    place-self: end;
    z-index: 99;

    > button {
      border: 1px solid light-dark(black, white);
      background: 0;
      border-radius: 50px;
      aspect-ratio: 1;

      &:hover {
        background: CanvasText;
        color: Canvas;
      }
    }

    @container activities-widget (width < 300px) {
      display: none;
    }
  }
  .activities-count {
    grid-column: 2 / -1;
    place-self: start;
    z-index: 99;
    color: light-dark(rgba(30, 30, 30, 0.6), rgba(240, 240, 240, 0.6));

    @container activities-widget (width < 300px) {
      display: none;
    }
  }
}

@keyframes straightenImages {
  50% {
    --imgRotate: 0deg;
  }
}

@keyframes moveOutIn {
  /* image */
  50% {
    translate: -100% 0;
    scale: 1.15;
  }
  100% {
    translate: 0 0;
    z-index: 1;
  }
}

              
            
!

JS

              
                // Why use lit?
// 1. Nice click handlers with reactive data
// 2. Easy CSS injection
// 3. Combo light dom and component-rendered

import { LitElement, html, css } from "https://esm.sh/lit";

class ActivitiesWidget extends LitElement {
  // Types without TypeScript? I guess??
  static properties = {
    count: { type: Number },
    activeActivity: { type: Number }
  };

  constructor() {
    super(); // makes `this` work
    this.activeActivity = 1;
  }

  // DOM-releated stuff needs DOM to be ready.
  connectedCallback() {
    super.connectedCallback();
    this.allActivities = this.querySelectorAll(".activity");
    this.allActivities[0].classList.add("active");
    this.count = this.allActivities.length;
  }

  _makeActive(index) {
    this.allActivities.forEach((activity, i) => {
      activity.classList.remove("active");
    });
    this.allActivities[index].classList.add("active");
    this.classList.add("children-animating");
    this.allActivities[index].addEventListener(
      "animationend",
      () => {
        this.classList.remove("children-animating");
      },
      { once: true }
    );
  }

  _movePrevious(e) {
    this.activeActivity--;
    if (this.activeActivity < 1) {
      this.activeActivity = this.count;
      this._makeActive(this.count - 1);
    } else {
      this._makeActive(this.activeActivity - 1);
    }
  }

  _moveNext(e) {
    this.activeActivity++;
    if (this.activeActivity > this.count) {
      this.activeActivity = 1;
      this._makeActive(0);
    } else {
      this._makeActive(this.activeActivity - 1);
    }
  }

  // Light DOM!
  createRenderRoot() {
    return this;
  }

  // Inject additional stuff into DOM (stays Light DOM), and allow Lit-style reactivity and event handling.
  render() {
    return html`
      <div class="activities-count">${this.activeActivity}/${this.count}</div>

      <nav class="activities-navigation">
        <button aria-label="previous" @click="${this._movePrevious}">⭠</button>
        <button aria-label="next" @click="${this._moveNext}">⭢</button>
      </nav>
    `;
  }
}

customElements.define("activities-widget", ActivitiesWidget);

              
            
!
999px

Console