Styling code-input elements with CSS
Contributors: 2025 Oliver Geer
code-input elements can be styled like textarea elements in most cases; however, there are some exceptions:
- The CSS variable
--paddingshould be used rather than the propertypadding(e.g.<code-input style="--padding: 10px;">...), or--padding-left,--padding-right,--padding-topand--padding-bottominstead of the CSS properties of the same names. For technical reasons, the value must have a unit (i.e.0px, not0). To avoid overcomplicating the code, this padding is always included in any width/heights ofcode-inputelements, so if you want to styletextareas andcode-inputelements with best consistency setbox-sizing: border-boxon them. - Background colours set on
code-inputelements will not work with highlighters that set background colours themselves - use(code-input's selector) pre[class*="language-"]for Prism.js or.hljsfor highlight.js to target the highlighted element with higher specificity than the highlighter’s theme. You may also set thebackground-colorof the code-input element for its appearance when its template is unregistered / there is no JavaScript. - The caret and placeholder colour by default follow and give good contrast with the highlighted theme. Setting a CSS rule for
colorand/orcaret-colorproperties on the code-input element will override this behaviour. - For technical reasons,
code-input:focuswon’t match anything. Usecode-input:has(textarea:focus)instead. - For now, elements on top of
code-inputelements should have a CSSz-indexat least 3 greater than thecode-inputelement.
Please do not use className in JavaScript referring to code-input elements, because the code-input library needs to add its own classes to code-input elements for easier progressive enhancement. You can, however, use classList and style as much as you want - it will make your code cleaner anyway.