Code Block
Multiline text in a monospace font, with its whitespace and line breaks kept, and a button that copies it.
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>When to use
Section titled “When to use”- 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:
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.
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><sl:button>Save</sl:button></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
Section titled “The copy button”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".
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
Section titled “Long lines”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.
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;}Left out
Section titled “Left out”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.
Attributes
Section titled “Attributes”| Attribute | Values | Default | Description |
|---|---|---|---|
wrap | boolean | — | Wrap long lines instead of scrolling them horizontally. |
copy | boolean | true | Show 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.
Styling hooks
Section titled “Styling hooks”The classes and attributes your own CSS can select to restyle the component, as explained in your own CSS rules.
Parts
| Class | Element |
|---|---|
.code-block | <div> |
.code-block-content | <pre> |
.code-block-copied-icon | <svg> |
.code-block-copy | <button> |
.code-block-copy-icon | <svg> |
Props
| Prop | Selector | Values |
|---|---|---|
wrap | .code-block[data-wrap="…"].code-block:not([data-wrap]) | truefalse (default) |
States
| Selector | Values |
|---|---|
.code-block-copy[data-copied] | present or absent |