The State of Docs Report 2025 is live! Dive in and see why docs matter more than ever:
Read the report
LogoLogo
ProductPricingLog inSign up
  • Documentation
  • Developers
  • Guides
  • Changelog
  • Help Center
  • Getting Started
    • GitBook Documentation
    • Quickstart
    • Importing content
    • GitHub & GitLab Sync
      • Enabling GitHub Sync
      • Enabling GitLab Sync
      • Content configuration
      • GitHub pull request preview
      • Commit messages & Autolink
      • Monorepos
      • Troubleshooting
  • Creating Content
    • Formatting your content
      • Inline content
      • Markdown
    • Content structure
      • Spaces
      • Pages
      • Collections
    • Blocks
      • Paragraphs
      • Headings
      • Unordered lists
      • Ordered lists
      • Task lists
      • Hints
      • Quotes
      • Code blocks
      • Files
      • Images
      • Embedded URLs
      • Tables
      • Cards
      • Tabs
      • Expandable
      • Stepper
      • Drawings
      • Math & TeX
      • Page links
    • Reusable content
    • Broken links
    • Searching content
      • Search & Quick find
      • GitBook AI
    • Writing with GitBook AI
    • Version control
  • API References
    • OpenAPI
      • Add an OpenAPI specification
      • Insert API reference in your docs
    • Guides
      • Structuring your API reference
      • Adding custom code samples
      • Managing API operations
      • Describing enums
      • Integrating with CI/CD
  • Extensions reference
  • Publishing Documentation
    • Publish a docs site
      • Public publishing
      • Private publishing with share links
    • Site structure
      • Content variants
      • Site sections
    • Site customization
      • Icons, colors, and themes
      • Layout and structure
      • Extra configuration
    • Set a custom domain
    • Setting a custom subdirectory
      • Configuring a subdirectory with Cloudflare
      • Configuring a subdirectory with Vercel
    • Site settings
    • Site insights
    • Site redirects
    • Visitor authentication
      • Enabling visitor authentication
      • Setting up Auth0
      • Setting up Azure AD
      • Setting up AWS Cognito
      • Setting up Okta
      • Setting up OIDC
      • Setting up a custom backend
  • LLM-ready docs
  • Collaboration
    • Live edits
    • Change requests
    • PDF export
    • Inviting your team
    • Comments
    • Notifications
  • Integrations
    • Install and manage integrations
    • GitHub Copilot
  • Account management
    • Plans
      • Legacy pricing
      • Sponsored site plan
      • Non-profit plan
      • Billing policy
    • Subscription cancellations
    • Personal settings
    • Organization settings
    • Member management
      • Invite or remove members
      • Roles
      • Teams
      • Permissions and inheritance
    • SSO & SAML
      • SSO Members vs non-SSO
  • Resources
    • GitBook UI
    • Keyboard shortcuts
    • Glossary
Powered by GitBook
LogoLogo

Resources

  • Showcase
  • Enterprise
  • Status

Company

  • Careers
  • Blog
  • Community

Policies

  • Subprocessors
  • Terms of Service
On this page
  • Example of a code block
  • Code block options
  • Code block actions
  • Representation in Markdown

Was this helpful?

Edit on GitHub
  1. Creating Content
  2. Blocks

Code blocks

Add a code block to a page to include sample code, configurations, code snippets and more

Last updated 4 months ago

Was this helpful?

You can add code to your GitBook pages using code blocks.

When you add a code block, you can choose to , , , and . It’s also easy to , so you can use it elsewhere

A code block may be useful for:

  • Sharing configurations

  • Adding code snippets

  • Sharing code files

  • Showing usage examples of command line utilities

  • Showing how to call API endpoints

  • And much more!

Example of a code block

index.js
‌import * as React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, window.document.getElementById('root'));

You can also combine code blocks with a to offer the same code example in multiple different languages:

let greeting = function (name) {
  console.log(`Hello, ${name}!`);
};
greeting("Anna");
greeting = lambda {|name| puts "Hello, #{name}!"}
greeting.("Anna")
greeting = fn name -> IO.puts("Hello, #{name}!") end
greeting.("Anna")

Code block options

Set syntax…

You can set the syntax in your code block to any of the supported languages. This will enable syntax highlighting in that language, too.

filename.txt
// Some code

With line numbers

This will toggle line numbers for your code on and off.

Showing line numbers is useful when the code represents the contents of a file as a whole, or when you have long code blocks with lots of lines. Hiding line numbers is useful for snippets, usage instructions for command line or terminal expressions and similar scenarios.

With caption

This will toggle a caption that sits at the top of the block, above your lines of code.

Wrap code

This will toggle code wrapping on and off, so long lines of code will wrap to all be visible on the page at once.

Wrapping lines is useful when your code is long and you want to avoid having the viewer scroll back and forth to read it. If you toggle Wrap code on, you may also want to show line numbers — this will make it easier to read the code and understand where new lines start.

Code block actions

As well as the options above, you can also change the language the code block displays, and copy your code instantly.

Copy the code

Hover over a code block and a number of icons will appear. Click the middle icon to copy the contents of the code block to your clipboard.

Representation in Markdown

{% code title="index.js" overflow="wrap" lineNumbers="true" %}

```javascript
‌import * as React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, window.document.getElementById('root'));
```

{% endcode %}

You can make code blocks by clicking on the Options menu next to the block and choosing Full width.

When you click on the Options menu next to the code block, or the Actions menu in the block itself, you’ll see a number of options you can set.

We use for syntax highlighting. You can use to check which languages Prism supports. If you notice a mismatch between GitBook and Prism, there’s a chance we’re a version or two behind. We’ll catch up soon!

The caption is often the name of a file as shown in , but you can also use it as a title, description, or anything else you’d like.

Prism
Test Drive Prism
tabs block
set the syntax
show line numbers
show a caption
wrap the lines
copy the contents of a code block to the clipboard
our example above
span the full width of your window