Is it possible to change the color of a text element or the background of a div in a collection list in Webflow based on whether a switch is on or off?

Published on
September 22, 2023

Yes, it is possible to change the color of a text element or the background of a div in a collection list in Webflow based on whether a switch is on or off. You can achieve this by using conditional visibility and custom code.

Here's a step-by-step guide on how to accomplish this:

  1. Add a switch field to your CMS collection: Go to your CMS collection settings and add a switch field. This field will be used to toggle the color change.

  2. Create a symbol for your collection item: Select the text element or div that you want to change the color of and turn it into a symbol. This will allow you to use the same symbol across all collection items.

  3. Set up conditional visibility: With the symbol selected, go to the Settings panel and click on "Conditional Visibility." Set the condition to the switch field and choose "is on".

  4. Style the symbol for the on state: With the symbol still selected, apply the desired styling for when the switch is on. This can include changing the text color or background color.

  5. Style the symbol for the off state: Duplicate the symbol and apply the styling for when the switch is off. You can change the text color or background color to a different value.

  6. Add custom code: Go to the page where your collection list is displayed and add custom code. Within the code, target the switch field and apply a class to the collection item based on the switch state. For example, you can use JavaScript to add a class of "on" or "off" to the collection item.

  7. Write custom CSS: Add custom CSS to your project and style the text element or div based on the class applied in the previous step. For example, you can write CSS rules to change the color of the text element or background of the div based on the class.

By following these steps, you can dynamically change the color of a text element or background of a div in a collection list based on the state of a switch.

Example code for step 6:

// Replace "collection-item" with the class name of your collection item// Replace "switch-field" with the class name of your switch fielddocument.addEventListener("DOMContentLoaded", function() {  var collectionItems = document.querySelectorAll(".collection-item");    collectionItems.forEach(function(item) {    var switchField = item.querySelector(".switch-field");        if (switchField.checked) {      item.classList.add("on");    } else {      item.classList.add("off");    }  });});

Example CSS for step 7:

.collection-item {  /* default styles for when switch is off */}.collection-item.on {  /* styles for when switch is on */}.collection-item.off {  /* styles for when switch is off */}

By using this approach, you can customize the color of text elements or div backgrounds within a collection list based on the state of a switch field in Webflow. This provides the flexibility to create dynamic and visually appealing content based on user preferences.

Additional questions:

  1. Can I change the color of a text element in Webflow based on user input?
  2. How do I use conditional visibility in Webflow to show/hide elements based on specific conditions?
  3. Is it possible to customize the styling of collection items in Webflow using custom code?