Yes, it's possible to add dynamic class names to each item in a Webflow Collection List using custom JavaScript, based on a CMS field value.
Here’s how to do it step-by-step:
data-style
{{Custom CMS Field}}
(bind the value from your Collection)For example, for a CMS field called “Style”, it might insert “primary”, “secondary”, “alert”, etc.
display: none
.Add a custom code block at the end of the page (before </body>
) in Page Settings or Site Settings.
Use a script like this:
<script> document.addEventListener("DOMContentLoaded", function() { var items = document.querySelectorAll('[data-style]'); items.forEach(function(item) { var className = item.getAttribute('data-style').trim().toLowerCase(); if (className) { item.closest('.w-dyn-item').classList.add(className); } }); });</script>
Key Notes:
item.closest('.w-dyn-item')
assumes your dynamic list items use Webflow’s standard class..w-dyn-item
to the actual container you want to target if necessary..primary
, .secondary
, .alert
) and style them accordingly.You can apply dynamic class names to Webflow CMS items by exposing a CMS field via a hidden data-*
attribute, then using JavaScript to apply that value as a class to the collection item. This gives you flexible styling without duplicating collection lists.