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="example">
  <blockquote>"Your challenge is to build a(n) <strong>Rating</strong> component. You need to use the <code>:first selector</code> at least once. You also need to use the <code>overflow-anchor</code>, <code>contain</code>, and <code>border-image-source</code> properties each at least once."</blockquote>

  <div class="rating-wrap">
    <ul class="rating">
      <li><button class="selected"></button></li>
      <li><button class="selected"></button></li>
      <li><button class="selected"></button></li>
      <li><button></button></li>
      <li><button></button></li>
    </ul>
  </div>
  
  <p><button class="print">Print</button></p>
  
  <p>A an actual usage for <code>overflow-anchor: none;</code> is to keep the scrollbar at the bottom when items are constantly being added, like in a chatroom. Click a rating a few times to update this list.</p>
  <div class="rating-realtime">
    <div>Someone rated this 5/5.</div>
    <div>Someone rated this 5/5.</div>
    <div>Someone rated this 5/5.</div>
    <div>Someone rated this 5/5.</div>
    <div>Someone rated this 5/5.</div>
    <span id="anchor"></span>
  </div>
</div>


              
            
!

CSS

              
                
/* A silly way to do this border, but our challenge must make use of 'contain'. */
.rating-wrap { 
  display: inline-block;
  contain: layout;
}
.rating-wrap::before {
  content: "";
  position: absolute;
  display: block;
  border: 1px solid black;
  inset: -8px;
  z-index: -1;
}

.rating {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: row;
  list-style-type: none;
  gap: 0.5rem;
}

.rating li button {
  background: lightgrey;
  aspect-ratio: 1;
  width: 3.5rem;
  border: 1.35rem solid lightgrey;
  box-sizing: border-box;
}

.rating li button.selected {
  border-image-slice: 21;
  border-image-source: repeating-linear-gradient(45deg, transparent, lightseagreen 14px);
}

/* Not a whole lot you can do with this besides adjust print spacing. */
@page :first {
  margin-top: 200px;
}

/* Usage for overflow-anchor: none; */
.rating-realtime {
  padding: 0.5rem 0.75rem;
  list-style-type: none;
  height: 70px;
  border: 5px solid black;
  overflow: scroll;
  color: rgb(155,155,155);
}
.rating-realtime * {
  overflow-anchor: none;
}
.rating-realtime div:last-of-type {
  color: black;
}
#anchor {
  display: block;
  height: 1px;
  overflow-anchor: auto;
}

/* General display / notes */
.example { 
  max-width: 75ch;
  margin-inline: auto;
  font-family: sans-serif;
}

blockquote {
  margin-bottom: 2.0rem;
  font-style: italic;
  font-size: 1.25rem;
}

@media print {
  .print { display: none; }
}
              
            
!

JS

              
                const buttons = document.querySelectorAll(".rating button");
const feed = document.querySelector(".rating-realtime");
const feedAnchor = document.querySelector('#anchor');

buttons.forEach((button) => {
  button.addEventListener("click", () => {
    button.classList.toggle('selected');

    // Add to feed.
    const rating = button.closest('.rating').querySelectorAll(".rating button.selected").length;
    let item = document.createElement('div');
    item.textContent = `Someone rated this ${rating}/5.`;
    feed.insertBefore(item, feedAnchor);
  });
});

// Kick things off at the start by scrolling to bottom.
feed.scroll(0, 100);

// Print to test :first selectors.
document.querySelector(".print").addEventListener("click", () => {
  window.print();
});
              
            
!
999px

Console