Styling
With setups
Section titled “With setups”If you’re using the setups, most of the styling is handled for you. The only thing you must do is to pick a theme.
Changing the theme
Section titled “Changing the theme”To change the current theme with the setups, use the setOptions() method. Updating the theme is asynchronous, and the old theme is used until the new one loads.
Adding new themes
Section titled “Adding new themes”You can register new themes or override existing ones to use with the setups or web components.
Without setups
Section titled “Without setups”If you’re not using any of the setups, no styles will be injected into the page. Everything needs to be imported manually. This means that if you don’t like some of the default styles, you can import your own stylesheets instead.
Importing styles
Section titled “Importing styles”prism-code-editor/layout.css: Styles for the layout of the component.prism-code-editor/rtl-layout.css: Additional styles needed for thertloption to work.prism-code-editor/search.css. Styles forsearchWidget().prism-code-editor/copy-button.css: Styles forcopyButton().prism-code-editor/invisibles.css: Styles forshowInvisibles().prism-code-editor/guides.css: Styles forindentGuides().prism-code-editor/cursor.css: Styles forcustomCursor().prism-code-editor/code-folding.css: Styles forreadOnlyCodeFolding().prism-code-editor/autocomplete.css: Styles forautoComplete().prism-code-editor/autocomplete-icons.css: Default icons for the autocompletion tooltip.
Importing themes
Section titled “Importing themes”Themes can be imported from prism-code-editor/themes/*.css. There are currently 14 themes you can import. The playground showcases all themes.
You can also import themes into your JavaScript with loadTheme() which is used by the theme switcher in the playground.
Scrollbar styling
Section titled “Scrollbar styling”You can import a stylesheet that adds a custom scrollbar to desktop Chrome and Safari from prism-code-editor/scrollbar.css.
You can change the color of the scrollbar thumb using the custom property --pce-scrollbar. Different alpha values will be set based on the state of the scrollbar thumb.
CSS variables
Section titled “CSS variables”The list below shows most of the CSS variables that can be used to customize the appearance of the editor. CSS variables that aren’t set by themes can also be used with the setups.
--padding-inline: Horizontal padding for the editor. Defaults to0.75em.--number-spacing: Spacing between line numbers and the code. Defaults to0.75em.--pce-bg: Background for the editor. Set by all themes.--pce-cursor: Color of the editor’s cursor. Set by all themes.--pce-selection: Background color for selected text. Set by all themes.--pce-scroll-padding: Scroll padding to ensure the cursor isn’t too close to the edges while typing. Defaults to2ch.--pce-line-number: Text color for the line numbers. Set by all themes.--pce-bg-highlight: Background for the line with the cursor.--pce-border-highlight: Border for the line with the cursor.--pce-selection-match: Background color of selection matches. Set by all themes.--pce-scrollbar: See scrollbar styling. Set by all themes.--pce-guide-indent: Indentation guide color. Set by all themes.--pce-invisibles: Color used by theshowInvisibles()extension. Defaults to#e3e4e229.--pce-ac-match: Color of matched characters in completion options. Defaults to#52b1ff.--pce-tabstop: Color of tab stop indicators. Defaults to#7c7c7c4d.--pce-bg-fold: Color of the chevrons used to fold code. Defaults to#777.--pce-widget-bg: Background for editor widgets. Set by all themes.--pce-widget-color: Text color for editor widgets. Set by all themes.--pce-widget-border: Border color for editor widgets. Set by all themes.--pce-widget-bg-input: Background for input elements. Set by all themes.--pce-widget-bg-active: Background for selected widget buttons. Set by all themes.--pce-widget-bg-hover: Background for hovered widget buttons. Set by all themes.--pce-widget-color-active: Text color for selected widget buttons. Set by all themes.--pce-widget-focus: Focus ring color for widgets. Set by all themes.--pce-widget-error: Border color for invalid input elements. Set by all themes.--pce-widget-bg-error: Background for the error message popup. Set by all themes.--pce-widget-color-options: Text color used by the bottom row in the search widget. Set by all themes.--pce-search-match: Background for search matches. Set by all themes.
Advanced styling
Section titled “Advanced styling”The following document shows the DOM structure of an editor.
State classes
Section titled “State classes”There are many classes added to .prism-code-editor you can use to style the editor depending on its state:
pce-has-selectionif the textarea has a selection, andpce-no-selectionif not.pce-focusif the textarea is focused.show-line-numbersif line numbers are enabled.pce-wrapif word wrap is enabled, andpce-nowrapif not.pce-readonlyif the editor is read-only.pce-rtlif thertloption istrue.
Adding vertical scrolling
Section titled “Adding vertical scrolling”By default, .prism-code-editor has overflow: auto. This means the editor will scroll horizontally if needed, but since the editor doesn’t have height or max-height defined, it will grow to fit the content. To allow vertical scrolling, you need to limit the height of .prism-code-editor by e.g. defining height or max-height.
Changing the padding
Section titled “Changing the padding”Default padding is 0.75em left/right and 0.5em top/bottom. To change horizontal padding, use the custom property --padding-inline. Vertical padding can be changed by changing the margin on .pce-wrapper.
Scroll padding
Section titled “Scroll padding”The default scroll padding is intentionally small. This is because it’s also used when dragging with the mouse to select text, and large scroll padding makes it harder to select text close to the edges. If you want to increase the scroll padding, it’d be suggested to add the following event listeners to remove or decrease the scroll padding when dragging to select text.
Creating a theme
Section titled “Creating a theme”It’s likely that none of the themes perfectly fit your website. A great solution is to modify one of the included themes to better suit your website. Alternatively, you can import one of the themes and override some of the styles in your own stylesheets.