Webflow explained

Custom code in Webflow, explained

Where custom code can be inserted, and the associated performance implications. Plus tips to help manage code. For a quick summary of what Webflow can and cannot do, please see [Does Webflow support custom code?](/answers/does-webflow-support-custom-code/).

In brief

  • Site settings code is always available. Page settings code is available on that page. Code Embed code is made available according to where it is inserted.
  • There is a maximum of 50,000 characters allowed for each field.
  • Supported code types are HTML, CSS and JavaScript.
  • A Site plan must be purchased, and/or a Core, Growth, Agency or Freelancer Workspace.
  • Webflow does not provide support for custom code.

Areas

Site settings – Head and Footer code. Page settings – head field, pre-body tag field plus post-site code. Code Embeds can be inserted in the design and within rich text. Code can be personalised for different items using CMS fields. Link, meta and style tags are added in the head. Scripts can be included in the head and/or footer.

Issues

Sites published from August 2020 include jQuery 3.5.1. Adding any alternative version will produce a conflict. Event scripts (such as for clicking and hovering) can result in highly unusual results. Note that code is executed in the browser so there is no PHP, Python, Perl or Ruby.

Performance

All scripts are published with every page they are added to. A standard script tag causes the browser to pause whilst it downloads. Therefore place page relevant scripts in page settings. Webflow’s Site Usage page identifies JS and CSS files larger than 250KB. Async should be used for self-contained scripts such as analytics. Deferred should be used for scripts which are either page specific or need to execute in a set sequence. Async should never be used for interdependent scripts.

Webflow also has an option to ‘Asynchronously load JavaScript’ within Site settings > Publishing > Advanced publishing options. Again this is a premium feature. Should your custom code depend on Webflow’s JavaScript then you may need to add defer to it or enclose your inline code in a window load function, before it will function correctly.

Editor

As well as a search and replace function, the editor also provides auto-complete. There is also a Prettify function which will tidy up and validate your HTML, CSS and JavaScript prior to publishing. It will also alert you when you are nearing the character limit. If you exceed this then you must either externally host your code and link to it, minimise it or insert it into several Code Embeds. Custom code can be seen in the preview. Publishing your site makes this code live. Scripts in a Code Embed are represented by a dummy box on the canvas. To prevent code being shown in preview and comment views, enclose it between these HTML lines: <!-- EXCLUDE FROM PREVIEW: START --> and <!-- EXCLUDE FROM PREVIEW: END -->.

Management

Webflow suggests that you document what code has been inserted, together with reasons for and owners of the code. Over time and between members of a team, it is common for code to build up. Before inserting code snippets always verify if Webflow already provides an integration for the relevant tool.

Ecommerce

Should you be using Webflow Ecommerce then again, added scripts are beyond Webflow’s remit. You are therefore responsible for evaluating any PCI requirements which may apply.

Permissions

According to Webflow’s site permissions table, only Site Managers and Designers can access a site’s custom code. Marketers, Content Editors and Reviewers cannot. Furthermore, only the Site Manager can alter the default display setting for custom code in Site settings > Custom code.

For the meeting

Questions teams ask

Why is my script not functioning?

Please refer to Custom code not running on a Webflow site. Brief points to consider are: that your site has been published to the correct domain; that you are on a suitable plan; that there are no html, head or body tags in your code; and that there are no errors shown in the browser console. Also, if ‘Asynchronously load JavaScript’ is enabled then your inline code should not depend on Webflow’s JavaScript.

Can I pull in a code library from a CDN?

Yes. Simply link to it. However, again take care with the jQuery version.