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

              
                <p class="alert"><span aria-hidden="true">⚠️</span> Your browser doesn't support CSS style queries. Please try the demo in the latest Chrome Canary.</p>

<div class="wrapper">
  <div style="text-align: center; margin-bottom: 3rem;">
    <button id="add">Add author</button>
    <button id="remove">Remove author</button>
  </div>

  <div class="post-author" style="margin-bottom: 2rem;">
    <h3>Written by:</h3>
    <div class="post-author__wrapper">
      <div class="avatars-list">
        <img class="avatar" src="https://xsgames.co/randomusers/avatar.php?g=male" alt="">
      </div>
      <p class="authors"><a href="#">Ahmad Shadeed</a></p>
    </div>
  </div>
</div>
              
            
!

CSS

              
                .post-author {
  display: flex;
  flex-flow: column wrap;
  gap: 1rem;

  h3 {
    text-transform: uppercase;
    font-size: 13px;
  }
}

.post-author__wrapper {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;

  a {
    text-decoration: underline;
  }
}

.post-author:has(img:nth-last-child(n + 2)) {
  --multiple-avatars: true;
}

@container style(--multiple-avatars: true) {
  .avatars-list {
    display: flex;
    background-color: #efefef;
    padding: 8px 12px;
    border-radius: 50px;
  }

  img:not(:first-child) {
    border: solid 2px #fff;
    margin-left: -0.25rem;
  }
}

.avatars-list {
  display: flex;
}

.avatar {
  width: 40px;
  height: 40px;
  background-color: green;
  border-radius: 50%;

  @container style(--multiple-avatars: true) {
    width: 30px;
    height: 30px;
  }
}

.wrapper {
  max-width: 600px;
  margin: auto;
  padding: 1rem;
}

a {
  text-decoration: none;
  color: #222;
}

img {
  display: inline-block;
  object-fit: cover;
}

* {
  box-sizing: border-box;
}

body {
  font-family: system-ui;
  line-height: 1.35;
  padding: 2rem;
}

.alert {
  background-color: color-mix(in hsl, yellow 20%, white);
  text-align: center;
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 2rem;
}

              
            
!

JS

              
                const addButton = document.getElementById("add");
const removeButton = document.getElementById("remove");
const avatarsList = document.querySelector(".avatars-list");
const authors = [
  "Ahmad Shadeed",
  "John Smith",
  "Jane Doe",
  "Alex Johnson",
  "Sara Williams"
];

let authorsString = "";

function updateAuthorsString() {
  const numOfAuthors = avatarsList.children.length;

  if (numOfAuthors === 0) {
    authorsString = "";
  } else if (numOfAuthors === 1) {
    authorsString = authors[0];
  } else if (numOfAuthors === 2) {
    authorsString = `${authors[0]} and ${authors[1]}`;
  } else if (numOfAuthors > 2) {
    const authorsArr = [];
    for (let i = 0; i < numOfAuthors - 1; i++) {
      authorsArr.push(authors[i]);
    }
    const authorsStr = authorsArr.join(", ");
    authorsString = `${authorsStr}, and ${authors[numOfAuthors - 1]}`;
  }

  const authorsElement = document.querySelector(".authors");
  authorsElement.innerHTML = `<a href="#">${authorsString}</a>`;
}

addButton.addEventListener("click", function () {
  const avatarItem = document.createElement("img");
  avatarItem.setAttribute(
    "src",
    "https://xsgames.co/randomusers/avatar.php?g=male"
  );
  avatarItem.classList.add("avatar");

  avatarsList.appendChild(avatarItem);

  updateAuthorsString();
});

removeButton.addEventListener("click", function () {
  if (avatarsList.children.length > 0) {
    const lastListItem = avatarsList.lastElementChild;
    avatarsList.removeChild(lastListItem);
    updateAuthorsString();
  }
});

              
            
!
999px

Console