tag to detect if the empty child is visible and hide the parent container accordingly.
Yes, you can write custom JavaScript in Webflow to hide a parent div
when a child element is visible (e.g., set to display: block
), typically to account for empty CMS Collections.
Here’s how to do it:
data-empty-parent
)data-empty-child
)Use this inline logic:
display: block
Example:
<script> document.addEventListener("DOMContentLoaded", function () { var emptyChild = document.querySelector('[data-empty-child]'); var parentContainer = document.querySelector('[data-empty-parent]'); if (emptyChild && parentContainer) { var isVisible = window.getComputedStyle(emptyChild).display === "block"; if (isVisible) { parentContainer.style.display = "none"; } } });</script>
display: flex
or grid
in the empty child, adjust the condition accordingly.To hide a parent element when a Webflow CMS Collection is empty, use JavaScript to detect the empty state child element's visibility and then hide the parent accordingly. Tag elements with custom attributes or classes for specificity and apply the script at the end of the page body.