Webflow Elements

Code Block

No items found.
Overview
Layout Elements
Layout Elements Overview
100
Section
101
Container
102
Grid
103
Columns
104
Basic Elements
Basic Elements Overview
200
Typography Elements
Typography Elements Overview
300
Text Block
301
Paragraph
302
Rich Text
306
Block Quote
306
Typography Elements
CMS Elements Overview
400
Users Elements
Users Elements Overview
500
Media Elements
Media Elements Overview
600
Background Video
601
Forms Elements
Forms Elements Overview
700
Advanced & Navigation Elements
Advanced Elements Overview
800
Code Block
801
Navigation Elements
Dropdown
803
Third-Party Elements
Third-Party Elements Overview
850
Table of Contents
851
Other Custom Elements
Accordion
901
Breadcrumbs
902
No items found.

The Code Block element allows you to display formatted code.

Features

  • It can be used as a standalone element, or within a Rich Text Block.
  • It supports multiple syntaxes
    • Always? defaults to JS
  • Light mode / dark mode appearance
  • Works in the content editor
  • Works with the CMS

Limitations

Updated 24-Apr-2024
  • Limited margins & padding support ( margins affect padding )
    • No global support as there is no All Code Blocks archetype element
    • No margins & padding support for code blocks that are within CMS-bound rich text blocks
  • ? Scrolling
    • ? Maximum size
  • No control over the syntax highlighting styling
    • Restricted to light-mode or dark-mode styling
  • No native copy-code button feature

Extending Code Blocks

Adding Margins & Padding

When used in a Rich Text Block, by default the Code Block element is uncomfortably close to the paragraphs above and below it.

The Code Block element offers margin settings, however changing the margin setting also affects the padding within the code block.

What gives?

The HTML that Webflow generates for a code block look something like this;

<pre style="...">
  <code>
    ...
  </code>
</pre>

And <pre> element have some unusual layout and formatting constraints.

However, the Code Block element also does not yet have an All Code Blocks styling element, which means that a stylesheet approach won't work for your CMS-bound RTB's that contain Code Blocks.

Solution-

We'll go with a fully CSS solution here.

The following CSS targets all code blocks within any rich text element. You can adjust the margins of the code block itself in relation to adjacent content in the first rule, and the padding within the code block in the second rule.

IMPORTANT: This works great in Chrome, but has not been tested on any other browsers.
<style>
.w-richtext pre.w-code-block {
    margin: 2rem 0;
}
.w-richtext pre.w-code-block code {
  margin: 0;
  padding: 1rem; 
} 
</style>

Table of Contents
Did we just make your life better?
Passion drives our long hours and late nights supporting the Webflow community. Click the button to show your love.