Insights and Inspiration – The Hostnicker Blog

How to Create Custom Popups in Webflow with Code

September 20, 2024

To create a custom popup in Webflow with code, you'll need to follow several steps, from setting up the HTML structure to adding styling and functionality with JavaScript. Here's a simple guide to help you through the process:

Step 1: Setting Up Your Popup Structure

Begin by creating a new div block in Webflow that will act as your popup container. Drag a div block from the elements panel into the desired section of your page.

- Select the new div block and give it a class name, such as "custom-popup."
- Inside this div, include elements like a heading, paragraph text, and a close button. Examples of content include:
- A heading, such as "Special Offer!"
- A descriptive paragraph providing relevant information.
- A button or link for user action, such as "Learn More."
- A close button, typically represented as an "X," to allow users to dismiss the popup.

Step 2: Styling Your Popup

Once you've set up the structure, style the popup to match your website's design and make it visually appealing.

- With the "custom-popup" div selected, go to the Style panel.
- Set the position to "fixed" to center it on the screen.
- Adjust properties like background color, padding, border, and box-shadow for aesthetic appeal.
- Use flexbox or grid options to center the content horizontally and vertically within the popup.

Step 3: Making the Popup Hidden by Default

To prevent the popup from appearing immediately, set it to be hidden by default.

- In the Style panel, with the "custom-popup" selected, set the opacity to 0.
- Set the display property to "none" to ensure it doesn't take up space when hidden.

Step 4: Adding Custom Code for Popup Functionality

Now add JavaScript code to make the popup functional.

- Go to the Page Settings for the page where the popup will appear.
- In the custom code section, add the script below in the "Before </body> tag" area:

```javascript
document.addEventListener("DOMContentLoaded", function() {
const popup = document.querySelector('.custom-popup');
const openPopupButton = document.querySelector('.open-popup');
const closePopupButton = document.querySelector('.close-popup');

openPopupButton.addEventListener('click', function() {
popup.style.display = 'block';
setTimeout(() => {
popup.style.opacity = 1;
}, 10);
});

closePopupButton.addEventListener('click', function() {
popup.style.opacity = 0;
setTimeout(() => {
popup.style.display = 'none';
}, 300);
});
});
```

Replace '.open-popup' and '.close-popup' with the classes you used for the trigger and close buttons.

Step 5: Testing Your Popup

After saving changes and publishing your site, test the popup functionality:

- Click the button you created to trigger the popup and ensure it appears correctly.
- Test the close button to confirm the popup disappears smoothly.
- Check the popup's behavior across different devices, including mobile and tablet views in Webflow.

With these steps, you've successfully created a custom popup in Webflow using code. Modify the design, content, and triggers based on your site's needs to enhance user engagement while ensuring the popup complements the overall user experience.