.no-items-message) to the Webflow CMS “No Items” element and to its parent (e.g., .collection-wrapper). .no-items-message is visible (display: block), and hides the .collection-wrapper if true.Yes, you can use custom JavaScript in Webflow to detect when a child element is set to display: block (such as when a CMS Collection returns no items) and hide its parent DIV. This is a common use case when working with CMS-driven visibility logic.
no-items-message, for targeting via custom code.
Here's how to do it using JavaScript without any HTML tags:
no-items-message and parent div have class names.collection-wrapper.
<script>
document.addEventListener("DOMContentLoaded", function () {
// Select the element that shows when there are no CMS items
const noItems = document.querySelector('.no-items-message');
// Check if it is visible
if (noItems && window.getComputedStyle(noItems).display === 'block') {
// Get the parent wrapper and hide it
const parent = noItems.closest('.collection-wrapper');
if (parent) {
parent.style.display = 'none';
}
}
});
</script>
DOMContentLoaded to ensure elements are in place before querying.display === 'block': This confirms the “No Items” element was triggered to show..no-items-message) are unique and not reused elsewhere.
You can hide a parent DIV in Webflow using JavaScript by checking if the “No Items” element inside a CMS Collection is visible (display: block). Use querySelector and getComputedStyle to conditionally hide the parent wrapper based on CMS content state.