Skip to content

Code Block

Multiline text in a monospace font, with its whitespace and line breaks kept, and a button that copies it.

Code blockPoint at the block, or Tab to its copy button, to copy the text.
Thymeleaf source
<sl:code-block>java.lang.IllegalStateException: Order 1042 has no lines
at com.example.orders.OrderService.submit(OrderService.java:42)
at com.example.orders.OrderController.submit(OrderController.java:27)</sl:code-block>
Rendered HTML
<div class="code-block" x-data="slCodeBlock">
<pre class="code-block-content" tabindex="0">
<code>java.lang.IllegalStateException: Order 1042 has no lines
at com.example.orders.OrderService.submit(OrderService.java:42)
at com.example.orders.OrderController.submit(OrderController.java:27)</code>
</pre>
<span class="tooltip" x-data="slTooltip">
<button class="button code-block-copy" type="button" data-variant="outline" data-size="icon-sm" hidden aria-label="Copy">
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copy-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect>
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copied-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<path d="M20 6 9 17l-5-5"></path>
</svg>
</button>
<span class="tooltip-content" role="tooltip" popover="manual" aria-hidden="true">
Copy
<span class="tooltip-arrow" aria-hidden="true"></span>
</span>
</span>
<span class="code-block-status sl-sr-only" role="status" data-message="Copied"></span>
</div>
  • A command or a configuration snippet for people to copy.
  • A stack trace or a log on an error page or in a details view.
  • A piece of code, an API key or a token to paste somewhere else.

Use something else for:

  • A key or a shortcut: a kbd.
  • A short value in a list of details, such as an id: an item.

The text goes between the tags, as written: start it right after the opening tag, or the block starts with an empty line. Text from the model goes in with [[${...}]], which escapes it, so markup in it shows as written. th:text on the component would replace its copy button too.

Text from the model[[${...}]] escapes the text, so markup in it shows as written.
Thymeleaf source
<sl:code-block th:with="snippet='<sl:button>Save</sl:button>'">[[${snippet}]]</sl:code-block>
Rendered HTML
<div class="code-block" x-data="slCodeBlock">
<pre class="code-block-content" tabindex="0">
<code>&lt;sl:button&gt;Save&lt;/sl:button&gt;</code>
</pre>
<span class="tooltip" x-data="slTooltip">
<button class="button code-block-copy" type="button" data-variant="outline" data-size="icon-sm" hidden aria-label="Copy">
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copy-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect>
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copied-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<path d="M20 6 9 17l-5-5"></path>
</svg>
</button>
<span class="tooltip-content" role="tooltip" popover="manual" aria-hidden="true">
Copy
<span class="tooltip-arrow" aria-hidden="true"></span>
</span>
</span>
<span class="code-block-status sl-sr-only" role="status" data-message="Copied"></span>
</div>

The copy button sits in the block’s corner. With a mouse it shows while the pointer is over the block; with the keyboard, when Tab reaches it; on a touch screen it always shows. A click copies the text and shows a check mark for a moment, and screen readers hear “Copied”.

The button needs Alpine and the browser’s Clipboard API, which browsers only offer on HTTPS and on localhost. Without either, the button is not shown, and people select the text themselves. Leave it out with copy="false".

Without the copy button
Thymeleaf source
<sl:code-block copy="false">mvn spring-boot:run</sl:code-block>
Rendered HTML
<div class="code-block" x-data="slCodeBlock">
<pre class="code-block-content" tabindex="0">
<code>mvn spring-boot:run</code>
</pre>
</div>

The button’s name and the “Copied” announcement are the messages sl.code-block.copy and sl.code-block.copied; see Translating.

Long lines scroll horizontally, so the block never widens the page. The block takes the keyboard focus, so it can be scrolled with the arrow keys too. Add wrap to break long lines instead.

Wrapping long lineswrap breaks long lines instead of scrolling them.
Thymeleaf source
<sl:code-block wrap>java.lang.IllegalArgumentException: The delivery date 2026-12-24 is after the last date the warehouse in Ghent accepts orders for this year
at com.example.orders.delivery.DeliveryDateValidator.validate(DeliveryDateValidator.java:58)</sl:code-block>
Rendered HTML
<div class="code-block" x-data="slCodeBlock" data-wrap="true">
<pre class="code-block-content" tabindex="0">
<code>java.lang.IllegalArgumentException: The delivery date 2026-12-24 is after the last date the warehouse in Ghent accepts orders for this year
at com.example.orders.delivery.DeliveryDateValidator.validate(DeliveryDateValidator.java:58)</code>
</pre>
<span class="tooltip" x-data="slTooltip">
<button class="button code-block-copy" type="button" data-variant="outline" data-size="icon-sm" hidden aria-label="Copy">
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copy-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<rect width="14" height="14" x="8" y="8" rx="2" ry="2"></rect>
<path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" class="icon code-block-copied-icon" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false">
<path d="M20 6 9 17l-5-5"></path>
</svg>
</button>
<span class="tooltip-content" role="tooltip" popover="manual" aria-hidden="true">
Copy
<span class="tooltip-arrow" aria-hidden="true"></span>
</span>
</span>
<span class="code-block-status sl-sr-only" role="status" data-message="Copied"></span>
</div>

To limit the height of a long block, give the text a max-height in your own CSS; it scrolls vertically past it:

.code-block-content {
max-height: 20rem;
}

No syntax highlighting and no line numbers. For highlighted code, run a highlighter (such as highlight.js) over .code-block-content > code in your own script.

AttributeValuesDefaultDescription
wrapboolean—Wrap long lines instead of scrolling them horizontally.
copybooleantrueShow the copy button. false leaves it out.

Every prop can also be set from an expression: th:wrap="${...}". Any other attribute passes through to the rendered element, and class is appended.

The classes and attributes your own CSS can select to restyle the component, as explained in your own CSS rules.

Parts

ClassElement
.code-block<div>
.code-block-content<pre>
.code-block-copied-icon<svg>
.code-block-copy<button>
.code-block-copy-icon<svg>

Props

PropSelectorValues
wrap.code-block[data-wrap="…"]
.code-block:not([data-wrap])
true
false (default)

States

SelectorValues
.code-block-copy[data-copied]present or absent