How do I add a code block in Webflow?
Add a Webflow code block to show formatted code. It highlights a snippet and does not run it. A code embed is the element that runs code.
Copy the snippet you want to show. Open Add panel, Other, and drag a code block onto the canvas, into a rich text element, or into a rich text field in the CMS. Paste the code, then click Save and close. The block displays formatted code. It does not run it. Language, theme, and line numbers are in Code block settings. The snippet cannot exceed 50,000 characters.
Add the block and paste the snippet
A code block shows formatted code on the page. It does not run that code. Use it when a docs page, a tutorial, or a job post should let someone read a snippet. If the snippet should load a widget or change the page, that is a code embed, not this element.
Copy the code first. Open the Add panel, go to Other, and drag a code block onto the canvas. You can also drop it into a rich text element, or into the rich text field on a Collection item. Paste into the code editor, then click Save and close.
Webflow says the element supports most programming languages, and not every language. C++ and GraphQL are two it does not support. The full list is in Settings panel, Code block settings, Language. The block tries to recognize the language when you paste. You can change that choice.
Example: a help article should show a short HTML button, and the button should not appear as a real control. Drag the code block under the paragraph, paste the HTML, and save. Visitors see the formatted lines. They do not get a second button.
Set the language, the theme, and the line numbers
Select the block. Code block settings has three controls:
- Language: the highlighting syntax. Change it if the guess is wrong.
- Theme: light or dark.
- Show line numbers: whether each line shows a number.
Open the editor again from the settings icon above the block, then Open code editor. You can also open it from Settings panel, Code block settings. Edit, then Save and close.
The editor can search. Press Command + F on Mac or Control + F on Windows, or click the search button. You can match case, match a whole word, use a regular expression, and replace one match or all of them. Autocomplete suggests HTML tags, styles, and variables, and it closes brackets and tags. Prettify formats the code and checks HTML, CSS, and JavaScript for syntax errors before you publish.
The limit is 50,000 characters. A warning appears as you get close. If the snippet is longer, Webflow says you can minify it with a tool outside Webflow, or split it across more than one code block. Closing with unsaved changes asks you to save or discard.
Style the pre tag, or one block only
Select the code block. In the Style panel Selector, choose All pre tags. Changes there format the displayed code: size, font size, font color, background color, padding, margin, and line height. Webflow names text-wrap and white-space as styles this tag does not take from the Style panel.
Styling All pre tags affects pre elements that are not code blocks too. Size, type, color, padding, margin, and line height on that tag are shared. Set the look you want on every snippet, not a private copy for one block.
A dark theme in Code block settings is the highlighting theme. A background color on All pre tags is the Style panel. If they fight, check which one you set last, and preview. The canvas should show the formatted lines, not a blank box, after you save the editor.
Do not use it when the code must run
A code embed is the element that runs HTML, CSS inside a style tag, or JavaScript inside a script tag. Site and page custom code is the other place for a snippet that should load outside one block. The code embed guide is the element you place in a section. The custom code guide is the head and footer. This page is only the block that displays code.
Both the block and the embed use a 50,000 character limit. The job is still different. Pasting a chat widget into a code block shows the widget's source. It does not start the chat. Paste that widget into an embed if it should run, then publish.
A rich text element can hold a code block, including the rich text field on a Collection item. The rich text guide is the element itself. Drop the code block inside it when one article should show a snippet and the next article should not. A block on the static page template shows on every item.
Publish and read the lines
Save the editor, then publish the page. Open the live URL and read the snippet. The language highlight should match the code, line numbers should show only if you turned them on, and the page should not gain a new button, form, or widget from that paste.
If the live page does run something, you added an embed or custom code, not only this block. Remove that running snippet if the page was only supposed to display the lines.
If the paste is missing, open the editor and confirm you clicked Save and close. A discard on the way out leaves the previous code, or an empty block.
Showing a snippet on a page you already have is ordinary content work. The membership is one plan for every existing Webflow site: $999 per month, including 10 hours of Webflow work. Additional work is $75 per hour and starts only after you approve a written estimate.
Questions & answers
The snippet never runs on the live page. Is the block broken?
No. A code block only displays code. To run HTML, CSS, or JavaScript, use a code embed, or site custom code, and publish.
Why is C++ missing from the language list?
Webflow says the code block supports most languages and does not include every one. C++ and GraphQL are examples it does not support. The full list is in Code block settings, Language.
I styled one block and every snippet changed. What did I select?
All pre tags styles every pre element, not one block. Reset that tag and set the shared look you want. Those tag styles are not a private setting on a single snippet.
The editor closed and my paste disappeared. What should I do?
Save and close keeps the paste. If you close with unsaved changes, Webflow asks you to save or discard. Choose save. A discarded edit is not in the block.
Sources & further reading
Need a hand with your Webflow site?
View membership
View membership