Skip to content

Code Blocks

Learn how to use code blocks to display syntax-highlighted code examples, configuration files, and terminal commands in your documentation.

Code blocks are essential components for displaying code examples, configuration files, terminal commands, and other technical content in your documentation. The Six Starlight Theme provides enhanced code block styling with syntax highlighting, copy functionality, and responsive design. Code blocks help developers understand how to implement features and configure their projects.

Code blocks use markdown syntax and don’t accept traditional props. They’re implemented using triple backticks with optional language specification.

ElementSyntaxDescription
Basic code block````markdown```language
code here
``````
Code block with syntax highlighting
Language identifierjs, ts, css, html, json, yaml, bash, etc.Specifies the programming language for syntax highlighting
Inline code`code`Short code snippets within text

The simplest form of code blocks displays code with basic syntax highlighting.

// JavaScript code block
function example() {
const message = 'Hello, World!';
console.log(message);
return message;
}
// TypeScript example
interface Theme {
name: string;
version: string;
}
const theme: Theme = {
name: 'six-starlight',
version: '1.0.0'
};
console.log('Theme loaded:', theme.name);

Code blocks are perfect for displaying terminal commands and shell scripts.

Terminal window
# Install the theme
npm install @six/starlight-theme
# Add to configuration
npm run build
# Start development server
npm run dev
Terminal window
# Alternative installation methods
pnpm add @six/starlight-theme
yarn add @six/starlight-theme

Code blocks support various configuration file formats with appropriate syntax highlighting.

{
"theme": "six-starlight",
"version": "2.0.0",
"features": {
"darkMode": true,
"customization": true,
"components": true
}
}
# YAML configuration
theme: six-starlight
version: 2.0.0
features:
darkMode: true
customization: true
components: true
/* CSS code block */
.example {
color: var(--sl-color-accent);
font-size: 1rem;
padding: 1rem;
}
/* Custom CSS variables */
:root {
--sl-color-accent: #0066cc;
--sl-color-accent-low: #e6f0ff;
--sl-color-accent-high: #004499;
}

Use inline code formatting for short code snippets, file names, and technical terms.

This theme supports inline code formatting for short snippets. You can reference files like astro.config.mjs or use technical terms like npm install inline.


The theme supports syntax highlighting for many programming languages:

LanguageIdentifierUse Case
JavaScriptjs or javascriptJavaScript code examples
TypeScriptts or typescriptTypeScript code examples
CSScssStylesheet examples
HTMLhtmlHTML markup examples
JSONjsonConfiguration files
YAMLyamlYAML configuration
TOMLtomlTOML configuration
Bashbash or shTerminal commands
Markdownmd or markdownMarkdown examples

Code blocks include a copy button that allows users to easily copy code to their clipboard.

Code blocks automatically adapt to different screen sizes:

  • Desktop: Full-width display with optimal readability
  • Mobile: Scrollable content with touch-friendly controls

Code blocks include proper accessibility features:

  • High contrast syntax highlighting
  • Proper semantic markup
  • Keyboard navigation support

  • Always specify the language for proper syntax highlighting
  • Use descriptive file names when relevant
  • Keep code examples focused and relevant
  • Include comments to explain complex logic
  • Test code examples to ensure they work correctly
  • Use consistent formatting and indentation
  • Provide context for code examples

  • Don’t use code blocks for non-code content - Use regular text formatting instead
  • Don’t include unnecessary code - Keep examples focused and relevant
  • Don’t forget to specify the language - This ensures proper syntax highlighting
  • Don’t use code blocks for simple text - Use inline code formatting for short snippets
  • Don’t create overly long code examples - Break them into logical sections
  • Don’t use code blocks without context - Always explain what the code does
  • Don’t include broken or outdated code - Ensure examples are current and functional