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="product-area">
  <div class="section-header">
    <h2>attr() demo</h2>
    <p>A semantic attr() or value() would let me style the preview colors via a data attribute. I currently have to do this in JS, since attr() only recognized strings and can only really be used with the content property.</p>
  </div>
  
  <div class="product-list two-by">
    <div class="product-card-container" style="--detail: new">
      <div class="product-card">
        <div class="media-img">
          <img src="https://aritzia.scene7.com/is/image/Aritzia/hi-res/f22_01_a03_82571_16042_on_c.jpg" alt="">
          <div class="comment-block"></div>
        </div>
        <div class="meta">
          <p class="brand">Babaton</p>
          <p class="name">Guell Sweater</p>
          <p class="price">$168</p>
          <div class="colors">
            <ul class="color-list">
              <li data-color="rgb(255, 255, 255)" data-name="Snow">white</li>
              <li data-color="rgb(220, 180, 110)" data-name="Beige">beige</li>
              <li data-color="rgb(160, 0, 30)" data-name="Maroon Red">maroon</li>
              <li data-color="rgb(0, 0, 0)" data-name="Nighthawk">black</li>
              <li data-color="rgb(145, 60, 50)" data-name="Tree Bark">brown</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <div class="product-card-container">
      <div class="product-card">
        <div class="media-img">
          <img src="https://aritzia.scene7.com/is/image/Aritzia/f22_04_a04_101467_11420_on_a?wid=1500" alt="">
          <div class="comment-block"></div>
        </div>
        <div class="meta">
          <p class="brand">Wilfred</p>
          <p class="name">Heartbreaker Blazer</p>
          <p class="price">$228</p>
          <div class="colors">
            <ul class="color-list">
              <li data-color="white" data-name="Snow White">white</li>
              <li data-color="black" data-name="Midnight">black</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                  /*  This is what I want to do:  */
.color-list li {
  background-color: attr(data-color); /* data attr as color */
}

/* This is what you can curently do */
.color-list li::after {
  content: attr(data-name); /* data attr as string */
  display: none;
}

.color-list li:hover::after,
.color-list li:focus::after{
  display: block
}

@layer base-demo {
  .product-area {
    max-width: 600px;
    margin: 0 auto;
  }

  .product-card img {
    width: 100%;
  }

  .product-list {
    display: grid;
    gap: 1rem;
  }

  .media-img {
    position: relative;
  }

  .meta {
    padding: 0.5rem 0.5rem 0;
  }

  .meta p {
    margin: 0;
  }

  .color-list {
    margin-top: 1rem;
    padding: 0;
    display: flex;
    gap: 0.25rem;
    position: relative;
  }

  .color-list li {
    font-size: 0;
    width: 0.75rem;
    height: 0.75rem;
    display: inline-block;
    border-radius: 50%;
    border: 1px solid;
  }
  
  .color-list li::after {
    position: absolute;
    bottom: 100%;
    left: 0;
    font-size: .75rem;
    color: gray;
  }

  .two-by {
     grid-template-columns: 1fr 1fr;
  }

  /* Rest of Demo */
  @media (max-width: 800px) {
    .view-buttons {
      display: none;
    }

    .product-card-container {
      container-type: inline-size;
    }

    .product-card {
      width: 100%;
    }
  }

  body {
    font-family: Roboto Mono, monospace;
  }
}

              
            
!

JS

              
                // Products from aritzia.com

// What I would like to do instead of this is use data attributes to set the colors in HTML and apply them in CSS
const colors = [...document.querySelectorAll('.color-list li')]

colors.forEach((color) => {
  //   using innerText for ease but would likely use data attributes for more specific colors
  const colorValue = color.getAttribute('data-color');
  color.style.backgroundColor = colorValue;
})
              
            
!
999px

Console