lit-html is Lit’s HTML templating library. It lets you write HTML templates in JavaScript using tagged template literals and efficiently renders and updates DOM.
Template tags
html
Interprets a template literal as an HTML template.
HTMLTemplateResult. Templates are lazy — no DOM is created until render() is called. Subsequent renders to the same container efficiently update only the changed parts.
svg
Interprets a template literal as an SVG fragment.
mathml
Interprets a template literal as a MathML fragment.
Rendering
render(value, container, options?)
Renders a value to a DOM container. The first render appends content to the container; subsequent renders efficiently update in place.
Any renderable value. Typically a
TemplateResult from html or svg, but also strings, numbers, nothing, DOM nodes, arrays, or iterables.The DOM container to render into. Only the options passed during the first render to a
container + renderBefore pair are used for the lifetime of that render.Controls rendering behavior. See
RenderOptions below.A
RootPart for the rendered tree. Call part.setConnected(false) before discarding to allow AsyncDirectives to dispose of resources.RenderOptions
Options that control how render behaves. Only options passed during the first render to a given container + renderBefore pair take effect.
Object used as
this when calling event listener functions. Commonly set to the host component instance.A DOM node before which to render content in the container. Use to control insertion position.
Node used for cloning templates via
importNode. Controls the ownerDocument of the rendered DOM and any inherited context. Defaults to the global document.Initial connected state for the top-level part. Defaults to
true. Set to false when the initial render occurs in a disconnected tree so that AsyncDirectives see isConnected === false on their first render.Sentinels
noChange
noChange from a directive leaves the previously committed value in place.
nothing
| Context | Effect |
|---|---|
| Child expression | Renders no nodes (same as null, undefined, '') |
| Attribute expression | Removes the attribute entirely |
| Property expression | Sets the property to undefined |
nothing over other falsy values for consistent behavior across expression types.
TemplateResult types
TemplateResult<T>
The return type of html, svg, and mathml. Holds the template strings, expression values, and result type. Does not create DOM on its own — DOM is created when the result is passed to render().
Specializations
| Type | Tag |
|---|---|
HTMLTemplateResult | html |
SVGTemplateResult | svg |
MathMLTemplateResult | mathml |
CompiledTemplateResult
A TemplateResult that has been pre-processed by @lit-labs/compiler, skipping the template preparation step at runtime.
Part types
Parts represent dynamic binding sites within a rendered template. Each binding expression in a template corresponds to one part.ChildPart
Represents a binding in child node position — the content between two elements or inside text content.
${value}
AttributePart
Represents a binding in an HTML attribute value position.
attr="${value}" or attr="prefix-${value}-suffix"
PropertyPart
Extends AttributePart. Sets a DOM property on the element instead of an attribute.
.propName="${value}"
BooleanAttributePart
Extends AttributePart. Toggles an attribute using toggleAttribute based on the truthiness of the value. When the value is falsy or nothing, the attribute is removed.
?attr="${boolValue}"
EventPart
Extends AttributePart. Manages an event listener via addEventListener / removeEventListener. Accepts a function or an EventListenerObject. Event options (capture, once, passive) are taken from the listener object.
@eventname="${handler}"
ElementPart
Represents a binding on an element itself (used by element directives like ref).
${directive(element)}
RootPart
A ChildPart returned from a top-level render() call. Adds a setConnected method for managing AsyncDirective connection state across the entire rendered tree.
Directives
Directive
Base class for creating custom directives. Subclass Directive, implement render(), and wrap it with directive().
directive(c)
Creates a user-facing directive function from a Directive class. The returned function has the same parameters as the directive’s render() method.
DirectiveResult<C>
The object returned by a directive factory function. It captures the directive class and its argument values without evaluating the directive.
PartInfo
Information about the part a directive is bound to. Passed to the Directive constructor.
Directive is bound in child position.
AttributePartInfo
{ type: 1 | 3 | 4 | 5; name: string; tagName: string; strings?: ReadonlyArray<string> }
Directive is bound in attribute, property, boolean attribute, or event position.
Directive is bound to an element.
PartType
Numeric constants identifying each part type.
Security
SanitizerFactory
A factory that creates sanitizer functions for DOM writes. Used to implement Content Security Policy enforcement.
ValueSanitizer
A function that sanitizes a value before it is written to the DOM.
render.setSanitizer(factory) (available when security hooks are enabled).