I wanted to share a simple and effective method for conditionally removing content across localised Webflow projects by using data-hide attributes.
How it works:
Add the code snippet below to your site-wide custom code (before the </body> tag) and customise the locales to fit your project structure.
In the example code:
Elements with data-hide="global" will be removed from the primary locale.
Elements with data-hide="us,au" will be removed from /us and /au locales.
Code snippet:
<!-- Dynamically hides elements based on `data-hide` attributes and the locale -->
<script>
document.addEventListener("DOMContentLoaded", function () {
// Get the current path from the URL
const currentPath = window.location.pathname;
// Function to check if an element should be removed based on its `data-hide` attribute
const shouldRemoveElement = (element) => {
const hideLocales = element.getAttribute('data-hide');
if (hideLocales) {
const localesToHide = hideLocales.split(',').map(locale => locale.trim());
const currentLocale =
currentPath.startsWith('/nz') ? 'nz' :
currentPath.startsWith('/au') ? 'au' :
currentPath.startsWith('/us') ? 'us' :
'global'; // Default to global for non-prefixed paths
return localesToHide.includes(currentLocale);
}
return false;
};
// Find and remove elements with `data-hide` attributes
document.querySelectorAll('[data-hide]').forEach(element => {
if (shouldRemoveElement(element)) {
element.remove();
}
});
});
</script>
Example
To hide a banner for US and AU locales, add the attribute data-hide="us,au" to that element in the Webflow Designer.
The code will automatically detect the current locale and remove the element for /us and /au pages.
TL;DR: Use this code snippet with a data-hide attribute like data-hide="global,us" to dynamically hide content for specific locales on your Webflow project.
Hope this helps! Please me know if you have any questions or improvements to suggest.
This method also allows for creating a data-hide CMS field and binding it to CMS items e.g. articles on a blog page so we can hide/show these across locales.
Although these articles are now hidden on the blog page, if we link to them externally they will still be reachable (unlike if set as draft or archived in Webflow CMS).
This is important for maintaining CMS items only relevant to specific locales while still being able to share article links globally.
The above code also removes the element(s) rather than setting to display none.
There are some scenarios where I’d use a scripted approach, but currently they’re quite rare because the native system is quite solid.
One of my favorite approaches is to use a component with visibility bound to a property, because on locales where you hide the component, the page is published without it. That can be used on collection pages and in collection lists as well.
It’s slightly better for SEO than script-removal of the hidden elements is, simply as Google doesn’t always run all scripts depending on the page speed, and other search engines don’t generall run script at all.
In terms of the article localization example, you can draft a specific locale for a specific item, so a site that’s EN DE JP locales could have a blog article that’s primary-EN, translated-DE, and 404’s in JP.
I think the main problem though is that these features aren’t obvious- it took me too long to discover them as well.
Either way it’s excellent to have multiple paths to the top of the same mountain, so thank you for sharing! It will certainly come in useful for some users who need a specialized setup.