data-style-class
) to each CMS item using a dynamic CMS field value.You can dynamically add class names to Webflow CMS collection items using custom JavaScript by leveraging a CMS field value as the class modifier. This allows for styling variants without duplicating collection lists.
div
wrapper).data-style-class
collection-field
), pulled in dynamically.data-style-class = [Color Variant]
style-type
with values like red
, blue
, green
, this stores them per item.section of Page Settings.
data-style-class
, and attach it as a class.<script> document.addEventListener("DOMContentLoaded", function() { const items = document.querySelectorAll("[data-style-class]"); items.forEach(function(item) { const style = item.getAttribute("data-style-class"); if (style) { item.classList.add(style.trim().toLowerCase().replace(/\s+/g, '-')); } }); });</script>
.primary-blue
, .red
, etc.)..red-bg
, .green-text
),<style>
tags via an Embed block and define CSS rules directly.By passing CMS field values via data-*
attributes and applying them with custom JS, you can dynamically assign class names to each item in a collection list. This approach delivers full styling flexibility across list items without needing multiple Collection Lists or static class lists.