Adding Code Blocks to Squarespace Sites
TL;DR:
- Code blocks let you add custom HTML, CSS, and Markdown to any Squarespace page or post
- JavaScript and iframes only work on Core plans and higher
- Each code block has a 400KB size limit
- Always test your code in Preview Mode before publishing
- Use Safe Mode to troubleshoot code that doesn't display properly
Code blocks are one of the most useful features in Squarespace. They let you add custom code directly to your pages, blog posts, sidebars, and footers when the standard blocks don't cut it.
Understanding Code Blocks
Code blocks work differently depending on what you're trying to achieve. You can use them to display formatted code snippets for tutorials, or to actually run custom HTML, CSS, and JavaScript on your site.
The key difference is the "Display Source" toggle. When it's on, your code appears formatted but doesn't execute. When it's off, the code runs and affects your page.
Adding Code Blocks
Here's how to add a code block to any page:
- Click the edit pencil on your page
- Click the + icon where you want to add the block
- Select "Code" from the block menu
- Click the pencil icon to open the code editor
Once you're in the editor, you can choose between HTML and Markdown formats. Most custom code uses HTML format.
Writing Code That Works
For CSS: Wrap your styles in <style></style> tags:
<style>
.my-custom-class {
color: red;
font-size: 18px;
}
</style>
For JavaScript: Wrap your scripts in <script></script> tags:
<script>
console.log("Hello world");
</script>
For HTML: Just paste your HTML directly into the block.
Remember that JavaScript and iframes only work on Core plans and higher. Basic plans can only use HTML, CSS, and Markdown.
Size Limits and Best Practices
Each code block can hold up to 400KB of data. That's roughly 300,000 characters, which is plenty for most use cases. If you're hitting this limit, consider breaking your code into multiple blocks or hosting larger files externally.
Always test your code in Preview Mode before publishing. This shows you exactly what visitors will see.
Troubleshooting Code Issues
Code blocks can be finicky, especially on index pages or when Ajax loading is enabled. Here are the most common fixes:
Code doesn't display at all: Check if the page is part of an index page. Index pages sometimes interfere with custom code.
Code works in edit mode but not live: Try viewing your site in Safe Mode. This disables some Squarespace features that might conflict with your code.
JavaScript doesn't run: Make sure you're on a Core plan or higher. Basic plans don't support JavaScript.
Code looks different than expected: Different browsers handle code differently. Test your site in Chrome, Firefox, and Safari to catch any issues.
Version Differences
These instructions apply to Squarespace 7.1 sites. If you're still on version 7.0, the process is similar but the interface looks different. Code blocks work the same way in both versions.
FAQs
Can I add JavaScript to any Squarespace plan?
JavaScript and iframes only work on Core, Plus, Advanced, Business, Commerce Basic, and Commerce Advanced plans. Basic plans are limited to HTML, CSS, and Markdown.
What's the size limit for code blocks?
Each code block can hold up to 400KB of data, which equals roughly 300,000 characters.
Why doesn't my code work when I'm logged in?
Squarespace sometimes interferes with custom code when you're logged in. Try viewing your site in Safe Mode or in an incognito browser window.
Can I use code blocks in blog posts?
Yes, code blocks work in blog posts exactly the same way they work on regular pages.
Jargon Buster
Code Blocks: Squarespace's feature that lets you add custom HTML, CSS, JavaScript, and Markdown to your site
JavaScript: A programming language that makes websites interactive and dynamic
Iframes: A way to embed content from other websites into your own pages
Ajax Loading: A technique that loads new content without refreshing the entire page
Safe Mode: A Squarespace feature that disables certain functions to help troubleshoot issues
Wrap-up
Code blocks give you serious flexibility when building your Squarespace site. Whether you're adding a simple bit of styling or integrating complex third-party tools, they're often the best solution when standard blocks fall short.
The key is testing everything thoroughly and understanding the limitations of your plan. Start small, test often, and don't be afraid to experiment.
Ready to take your Squarespace skills further? Join Pixelhaze Academy for more advanced tutorials and expert support.