CRO / Conversion2 min read
How to Close a Popup with the Esc Key in Webflow
Learn how to close a popup or modal in Webflow with the Esc key using a JavaScript keydown listener.
In this tutorial, Sandro, cofounder of the Gemeos Webflow agency, shows you how to close a popup or modal in Webflow with the Escape key.
Example
Close it with the button, a click outside, or the Escape key
| CSS class | Element | Role |
|---|---|---|
| modal-overlay | Popup div | Element to hide when Escape is pressed |
| is-visible | Active class | Class added when the popup is open |
The script
Add this script in the Before </body> tag of your page. Replace .modal-overlay with your popup class.
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
var modal = document.querySelector('.modal-overlay');
if (modal) {
modal.style.display = 'none';
document.body.style.overflow = '';
}
}
});Adapt it for multiple popups
To close all open popups at once with Escape:
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
document.querySelectorAll('.modal-overlay').forEach(function(m) {
m.style.display = 'none';
});
document.body.style.overflow = '';
}
});Conclusion
One line of code that significantly improves keyboard accessibility. See also: create a native modal in Webflow.


