# Code Blocks

> Display inline code and code blocks.

## Basic

### Inline Code

Use inline code to display code snippets within text paragraphs. It's ideal for referencing code elements directly in sentences.

<code-preview className="[\"[&>div]:*:my-0\"]">

`inline code`



#undefined
```mdc
`inline code`
```



</code-preview>

### Code Blocks

Use code blocks to display multi-line code snippets with syntax highlighting. Code blocks are essential for presenting code examples clearly.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">

```ts
export default defineNuxtConfig({
  modules: ['@nuxt/ui']
})
```



#undefined
```mdc
```ts
export default defineNuxtConfig({
  modules: ['@nuxt/ui']
})
```
```



</code-preview>

When writing a code-block, you can specify a filename that will be displayed on top of the code block. An icon will be automatically displayed based on the extension or the name.
Filenames help users understand the code's location and purpose within a project.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/ui']
})
```



#undefined
```mdc
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/ui']
})
```
```



</code-preview>

<tip>

Some icons are already defined by default, but you can add more in your `app.config.ts` through the `ui.prose.codeIcon` key:



```ts [app.config.ts]
export default defineAppConfig({
  ui: {
    prose: {
      codeIcon: {
        terminal: 'i-ph-terminal-window-duotone'
      }
    }
  }
})
```



</tip>

Every code-block has a built-in copy button that will copy the code to your clipboard.

<tip>

You can change the icon in your `app.config.ts` through the `ui.icons.copy` and `ui.icons.copyCheck` keys:



```ts [app.config.ts]
export default defineAppConfig({
  ui: {
    icons: {
      copy: 'i-lucide-copy',
      copyCheck: 'i-lucide-copy-check'
    }
  }
})
```



</tip>

#### Highlight Line

To highlight lines of code, add `{}` around the line numbers you want to highlight.
Line highlighting is useful for focusing users on important parts of code examples.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">

```ts [nuxt.config.ts] {4-5}
export default defineAppConfig({
  ui: {
    icons: {
      copy: 'i-lucide-copy',
      copyCheck: 'i-lucide-copy-check'
    }
  }
})
```



#undefined
```mdc
```ts [nuxt.config.ts]{4-5}
export default defineAppConfig({
  ui: {
    icons: {
      copy: 'i-lucide-copy',
      copyCheck: 'i-lucide-copy-check'
    }
  }
})
```
```



</code-preview>

## Advanced

### CodeGroup

Group code blocks in tabs using `code-group`. `code-group` is perfect for showing code examples in multiple languages or package managers.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">
<code-group className="[\"w-full\"]">

```bash [pnpm]
pnpm add @nuxt/ui
```



```bash [yarn]
yarn add @nuxt/ui
```



```bash [npm]
npm install @nuxt/ui
```



```bash [bun]
bun add @nuxt/ui
```



</code-group>

#undefined
```mdc
:::code-group

```bash [pnpm]
pnpm add @nuxt/ui
```

```bash [yarn]
yarn add @nuxt/ui
```

```bash [npm]
npm install @nuxt/ui
```

```bash [bun]
bun add @nuxt/ui
```

::
```



</code-preview>

<note to="#pre">

Like the `ProsePre` component, the `CodeGroup` handles filenames, icons and copy button.



</note>

### CodeTree

Display code blocks in a file tree view using `code-tree`. `code-tree` is excellent for showcasing project structures and file relationships.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">
<code-tree default-value="app/app.config.ts">

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/ui'],

  css: ['~/assets/css/main.css']
})
```



```css [app/assets/css/main.css]
@import "tailwindcss";
@import "@nuxt/ui";
```



```ts [app/app.config.ts]
export default defineAppConfig({
  ui: {
    colors: {
      primary: 'sky',
      colors: 'slate'
    }
  }
})
```



```vue [app/app.vue]
<template>
  <UApp>
    <NuxtPage />
  </UApp>
</template>
```



```json [package.json]
{
  "name": "nuxt-app",
  "private": true,
  "type": "module",
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare",
    "lint": "eslint .",
    "lint:fix": "eslint --fix ."
  },
  "dependencies": {
    "@iconify-json/lucide": "^1.2.63",
    "@nuxt/content": "^3.6.3",
    "@nuxt/ui": "^4.0.0",
    "nuxt": "^4.1.0"
  },
  "devDependencies": {
    "@nuxt/eslint": "^1.9.0",
    "eslint": "^9.34.0",
    "typescript": "^5.9.2",
    "vue-tsc": "^3.0.6"
  }
}
```



```json [tsconfig.json]
{
  "extends": "./.nuxt/tsconfig.json"
}
```



```md [README.md]
# Nuxt Minimal Starter

Look at the [Nuxt documentation](https://nuxt.com/docs/getting-started/introduction) to learn more.

## Setup

Make sure to install the dependencies:

```bash
# npm
npm install

# pnpm
pnpm install

# yarn
yarn install

# bun
bun install
```

## Development Server

Start the development server on `http://localhost:3000`:

```bash
# npm
npm run dev

# pnpm
pnpm run dev

# yarn
yarn dev

# bun
bun run dev
```

## Production

Build the application for production:

```bash
# npm
npm run build

# pnpm
pnpm run build

# yarn
yarn build

# bun
bun run build
```

Locally preview production build:

```bash
# npm
npm run preview

# pnpm
pnpm run preview

# yarn
yarn preview

# bun
bun run preview
```

Check out the [platform guides](/categories/general) for more information.
```



</code-tree>
</code-preview>

<note to="/categories/general">

Learn more about code organization in our guides.



</note>

### `CodePreview`

Use `code-preview` to show code output alongside the code. `code-preview` is ideal for interactive examples and demonstrating code results.
Write the code to be previewed in a the `default` slot and the actual code in the `code` slot.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]" label="Preview">
<code-preview className="[\"[&>div]:*:my-0\"]">

`inline code`



#undefined
```mdc
`inline code`
```



</code-preview>

#undefined
```mdc
::code-preview
`inline code`

#code
```mdc
`inline code`
```
::
```



</code-preview>

### `CodeCollapse`

Use `code-collapse` for long code blocks to keep pages clean. `code-collapse` allows users to expand code blocks only when needed, improving readability.

<code-preview className="[\"[&>div]:*:my-0\",\"[&>div]:*:w-full\"]">
<code-collapse className="[\"[&>div]:my-0\"]">

```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";

@theme {
  --font-sans: 'Inter', system-ui, sans-serif;

  --breakpoint-3xl: 1920px;

  /* Intrasistema brand palette */
  --color-brand-50: #f0f4ff;
  --color-brand-100: #e0e9fe;
  --color-brand-200: #c7d7fe;
  --color-brand-300: #9ebafd;
  --color-brand-400: #60a5fa;
  --color-brand-500: #3b82f6;
  --color-brand-600: #2563eb;
  --color-brand-700: #1d4ed8;
  --color-brand-800: #1e3a8a;
  --color-brand-900: #0d2366;
  --color-brand-950: #061033;
}
```



</code-collapse>

#undefined
```mdc
::code-collapse

```css [main.css]
@import "tailwindcss";
@import "@nuxt/ui";

@theme {
--font-sans: 'Inter', system-ui, sans-serif;

--breakpoint-3xl: 1920px;

/* Intrasistema brand palette */
--color-brand-50: #f0f4ff;
--color-brand-100: #e0e9fe;
--color-brand-200: #c7d7fe;
--color-brand-300: #9ebafd;
--color-brand-400: #60a5fa;
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
--color-brand-700: #1d4ed8;
--color-brand-800: #1e3a8a;
--color-brand-900: #0d2366;
--color-brand-950: #061033;
}
```

::
```



</code-preview>
