Skip to main content Home About the Design SystemRoadmap OverviewDesignersDevelopers OverviewColorGridIconographyInteractionsSpacingTypography Overview Global colorBox shadowTypographyBorderOpacitySpaceLengthIconBreakpointsMedia queries All elements Accordion Alert Announcement Audio player Avatar Back to top Badge Blockquote Breadcrumb Button Card Chip Code block Call to action Dialog Disclosure Footer Health index Icon Jump links Menu dropdown Navigation link Navigation (primary) Navigation (secondary) Navigation (vertical) Pagination PopoverPlanned Progress stepper Scheme toggle Site status Skeleton Skip link Spinner Statistic Subnavigation Surface Switch Table Tabs Tag Tile Timestamp Tooltip Video embed OverviewColor PalettesCustomizingDevelopers All PatternsAccordionCall to ActionCardFilterFormLink with iconLogo wallSearch barSticky bannerSticky cardTabsTagTile All Personalization PatternsAnnouncement FundamentalsAccessibility toolsAssistive technologiesCI/CDContentContributorsDesignDevelopmentManual testingResourcesScreen readers Design/code status Release notes Get support

Code block

OverviewStyleGuidelinesCodeAccessibilityDemos
ImportingUsagerh-code-blockImportingUsagerh-code-block

The content of code-block snippets must be contained within a non-executable <script> tag. By "non-executable", we mean that the script element must have a type attribute which is not module, importmap javascript, or any of the executable javascript mimetypes. With the exception of javascript, use the correct mime type for your content, e.g. text/html for HTML content.

<rh-code-block>
  <script type="text/html">
<!DOCTYPE html>
<title>Title</title>
<style>body {width: 500px;}</style>
<body>
  <p checked class="title" id="title">Title</p>
    <!-- here goes the rest of the page -->
</body>
  </script>
</rh-code-block>

Warning

When slotting HTML content into the code-block, if that HTML content contains a </script> end tag, you must escape it.

One approach to escaping script tags that is to close the containing <script type="text/html"> within the sample's closing script tag, then immediately open a new sample script tag containing the rest of the snippet. Another method is to insert a zero-width-joiner (or some other unusual unicode character) in the closing script tag, and use javascript to remove it before copying the content to the clipboard. Each method has benefits and drawbacks.

<rh-code-block>
  <script type="text/html">
<p>Script tags in HTML must be escaped</p>
<script>console.log('Success!');<</script><script type="text/html">/script>
  </script>
</rh-code-block>

To add javascript content, use the text/sample-javascript type.

<rh-code-block>
  <script type="text/sample-javascript">
  import '@rhds/elements/rh-code-block/rh-code-block.js';
  </script>
</rh-code-block>

Copy Button

Add the value copy to the actions attribute, and a helpful copy button will appear next to the code content. You may listen for the copy event and modify its content property to change the text copied to the clipboard. For example, to remove a shell prompt ($ ) from the copied text, use this listener function:

import {RhCodeBlockCopyEvent} from '@rhds/elements/rh-code-block/rh-code-block.js';

document.body.addEventListener('copy', function(event) {
  if (event instanceof RhCodeBlockCopyEvent) {
    // remove prompt and surrounding whitespace from the start of the string
    event.content = event.content.replace(/^\s*\$|#\s*/, '');
  }
});

Importing

Add rh-code-block to your page with this import statement:

<script type="module">
  import '@rhds/elements/rh-code-block/rh-code-block.js';
</script>
Copy to Clipboard Wrap lines

To learn more about installing RHDS elements on your site using an import map read our getting started docs.

Lightdom CSS

This element requires you to load "Lightdom CSS" stylesheets for styling deeply slotted elements.

Note

Replace /path/to/ with path to the CSS file, whether local or CDN.

<link rel="stylesheet" href="/path/to/rh-code-block/rh-code-block-lightdom.css">

Usage

<rh-code-block>
<script type="text/html"><!DOCTYPE html>
<title>Title</title>
<style>body {width: 500px;}</style>
<script type="application/javascript">
  function $init() {return true;}
<</script><script type="text/html">/script>
<body>
  <p checked class="title" id="title">Title</p>
  <!-- here goes the rest of the page -->
</body></script>
</rh-code-block>

<script type="module">
  import '@rhds/elements/rh-code-block/rh-code-block.js';
</script>
Copy to Clipboard Wrap lines

rh-code-block

A code block applies special formatting to sections of code.

Slots 6
Slot Name Summary Description
[default]

A non-executable script tag containing the sample content. JavaScript samples should use the type text/sample-javascript. HTML samples containing script tags must escape the closing </script> tag. Can also be a <pre> tag.
Note: [default] unnamed slots do not have a slot="name" attribute.

action-label-copy

Tooltip content for the copy action button

action-label-wrap

Tooltip content for the wrap action button

show-more

text content for the expandable toggle button when the code block is collapsed.

show-less

text content for the expandable toggle button when the code block is expanded.

legend

<dl> element containing rh-badges in the <dt> and legend text in the <dd> elements

Attributes 8
Attribute DOM Property Description Type Default
actions actions

Space- or comma-separated list of code block action buttons to display, containing either 'copy', 'wrap', or both. 'copy' adds a button that copies the text content to the clipboard. 'wrap' adds a button that toggles line wrap.

To override the default labels, e.g. for purposes of internationalization, use the action-label-copy and action-label-wrap slots. Each slot may receive two elements, one for the action's default state (e.g. "Copy to clipboard"), and one for the actions alternative state, e.g. "Copied!". The active-state element must have the attributes hidden data-code-block-state="active"

html``` Copy to Clipboard Toggle word wrap
</figure>
('copy' | 'wrap')[]
[]
highlighting highlighting

When set to "client", <rh-code-block> will automatically highlight the source using Prism.js When set to "Prerendered", <rh-code-block> will apply supported RHDS styles to children with prismjs classnames in the element's root.

'client' | 'prerendered'
unknown
language language

When set along with highlighting="client", this grammar will be used to highlight source code

| 'html' | 'css' | 'javascript' | 'typescript' | 'bash' | 'ruby' | 'yaml' | 'json'
unknown
compact compact

When set, the code block displays with compact spacing

boolean
false
dedent dedent

When set, the code block source code will be dedented

boolean
false
resizable resizable

When set, the code block is resizable

boolean
false
full-height fullHeight

When set, the code block occupies it's full height, without scrolling

boolean
false
line-numbers lineNumbers

When set to hidden, the code block's line numbers are hidden

'hidden'
unknown
Methods 2
Method Name Description
fromAttribute()
toAttribute()
Events 1
Event Name Description
copy

fired when the user requests to copy the code block text. Modify the event.content field to change the copied text (e.g. to remove a prompt from a shell command)

CSS Shadow Parts 0
None
CSS Custom Properties 2
CSS Property Description Default
--rh-code-block-callout-size var(--rh-size-icon-02, 24px)
--rh-code-block-border-block-start-width var(--rh-border-width-sm, 1px)
Design Tokens 44
Token Summary Copy
--rh-length-3xl
Full CSS Variable Permalink to this token
--rh-color-surface
Full CSS Variable Permalink to this token
--rh-size-icon-02
Full CSS Variable Permalink to this token
--rh-color-surface-lighter
Full CSS Variable Permalink to this token
--rh-color-surface-dark
Full CSS Variable Permalink to this token
--rh-space-xl
Full CSS Variable Permalink to this token
--rh-color-blue-10
Full CSS Variable Permalink to this token
--rh-color-gray-40
Full CSS Variable Permalink to this token
--rh-color-gray-20
Full CSS Variable Permalink to this token
--rh-color-gray-95
Full CSS Variable Permalink to this token
--rh-color-red-10
Full CSS Variable Permalink to this token
--rh-color-purple-50
Full CSS Variable Permalink to this token
--rh-color-teal-20
Full CSS Variable Permalink to this token
--rh-color-red-40
Full CSS Variable Permalink to this token
--rh-color-teal-60
Full CSS Variable Permalink to this token
--rh-color-yellow-60
Full CSS Variable Permalink to this token
--rh-color-blue-40
Full CSS Variable Permalink to this token
--rh-color-blue-60
Full CSS Variable Permalink to this token
--rh-color-orange-40
Full CSS Variable Permalink to this token
--rh-color-red-60
Full CSS Variable Permalink to this token
--rh-color-yellow-40
Full CSS Variable Permalink to this token
--rh-color-purple-30
Full CSS Variable Permalink to this token
--rh-color-orange-60
Full CSS Variable Permalink to this token
--rh-color-green-40
Full CSS Variable Permalink to this token
--rh-border-radius-default
Full CSS Variable Permalink to this token
--rh-size-icon-06
Full CSS Variable Permalink to this token
--rh-font-family-code
Full CSS Variable Permalink to this token
--rh-color-text-secondary
Full CSS Variable Permalink to this token
--rh-font-weight-code-regular
Full CSS Variable Permalink to this token
--rh-border-width-sm
Full CSS Variable Permalink to this token
--rh-color-border-subtle
Full CSS Variable Permalink to this token
--rh-line-height-code
Full CSS Variable Permalink to this token
--rh-color-text-primary
Full CSS Variable Permalink to this token
--rh-font-family-body-text
Full CSS Variable Permalink to this token
--rh-font-size-body-text-sm
Full CSS Variable Permalink to this token
--rh-font-weight-body-text-regular
Full CSS Variable Permalink to this token
--rh-line-height-body-text
Full CSS Variable Permalink to this token
--rh-color-icon-secondary
Full CSS Variable Permalink to this token
--rh-space-sm
Full CSS Variable Permalink to this token
--rh-font-size-code-md
Full CSS Variable Permalink to this token
--rh-space-3xl
Full CSS Variable Permalink to this token
--rh-space-4xl
Full CSS Variable Permalink to this token
--rh-space-lg
Full CSS Variable Permalink to this token
--rh-space-md
Full CSS Variable Permalink to this token
© 2025 Red Hat Deploys by Netlify