How can I close a modal in Webflow when clicking outside of it, without closing the modal when clicking on the modal itself?

TL;DR
  • Create a full-screen modal container with the modal content inside and ensure it doesn't fill the entire container to allow clickable "outside" space.
  • Use Webflow Interactions to set up a "Mouse Click" trigger on the modal container to close the modal on the first click.
  • Prevent the modal from closing when clicking inside by setting up a separate click interaction on the modal content or using JavaScript to cancel click event propagation.
  • Optionally, add a script in the page settings to enhance conditional click recognition for more control.

To close a modal in Webflow when clicking outside of it without closing it when clicking on the modal itself, follow these steps for a smooth experience.

1. Set Up the Modal Structure

  • Create a modal container that covers the entire display area. This will be the element that detects clicks outside of the modal.
  • Place the actual modal content inside the modal container, ensuring it doesn't fill the entire container. This allows for clickable "outside" space.

2. Use Interactions for Close Action

  • Select the modal container in the Webflow Designer.
  • Go to the Interactions panel and create a new interaction trigger for "Mouse Click (Tap)".
  • Set the first click to close the modal by changing its display setting to "none" or by hiding it through opacity and visibility animations.

3. Prevent Close When Clicking Inside

  • Inside the modal container, create a separate click interaction.
  • Select the modal content (not the container) and ensure you have proper padding or spacing to differentiate between clicks inside and outside the modal content area.
  • For this separate interaction, make sure nothing happens or you can use JavaScript to cancel the propagation of the click event if needed.

4. Use Conditional Click Recognition (Optional)

  • If default Webflow interactions don't fully cover your needs, utilize a small script in the "Custom Code" section:
  • Add a script in the page settings under "Before </body> tag" area.
  • Use jQuery or vanilla JavaScript to listen for click events on the modal container, and only trigger close if the target is not the modal content itself.

Summary

By organizing your modal with a full-screen container and using Webflow's interactions, you can ensure the modal only closes when a user clicks outside of it. Optionally, adding custom scripts can offer finer control over this behavior for more complex scenarios.

Rate this answer

Other Webflow Questions