Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

What Is Lit?

Meet Lit, the 5 KB library for building fast web components. See how it compares with vanilla custom elements, what its packages do, and run a counter in seconds.

Lit is a small JavaScript library for building web components. It was created at Google by the team behind Polymer, and it is now an open-source project used for design systems and applications at many companies. Its core idea is simple: you describe what a component looks like for the current data, and Lit updates only the pieces of the page that need to change.

Lit does not replace the browser's component model. Every Lit component is a real custom element with a real shadow root. That means a Lit component works in any page, and you can mix it freely with components written by hand or with other libraries. If you have not met those platform features yet, read What Are Web Components? first.

What Lit gives you

Compare a hand-written element with a Lit element. The vanilla version below has to remember to re-render itself and rebuilds its whole innerHTML each time. The Lit version declares a property and a template, and Lit takes care of the rest.

Vanilla element vs Lit element

Click both buttons. They look alike, but there is a real difference. The vanilla version throws away the button and creates a new one on every click, including a new event listener. Lit keeps the same <button> and changes only the text node that holds the number. In a larger component with inputs, focus and scroll positions, that difference matters a lot.

The three ideas inside Lit

Lit in three ideas

Templates html... describes the UI
Reactive properties Change data, UI re-renders
Scoped styles css... in a shadow root

All three are plain JavaScript

1 / 2
  1. Templates. The html tag turns a JavaScript template literal into efficient DOM. Expressions inside ${...} become live bindings. You will study this in Lit Templates and Bindings.
  2. Reactive properties. Declare a property once and assigning to it schedules a render. Updates are batched, so setting five properties in a row causes one render.
  3. Scoped styles. The css tag creates a stylesheet that is attached to the component's shadow root. It is parsed once and shared by every instance.

No virtual DOM

Many frameworks build a virtual copy of the page and compare old and new copies on every update. Lit works differently. The first time a template renders, Lit remembers where each ${...} expression lives in the real DOM. On later renders it checks only those spots and updates a spot only if its value changed. Static parts of the template are never touched again.

flowchart TD
  A["Property changes"] --> B["Update is queued<br/>(one microtask)"]
  B --> C["render() returns a template result"]
  C --> D{"First render?"}
  D -- yes --> E["Clone template, create<br/>expression parts"]
  D -- no --> F["Compare each expression value<br/>with the previous one"]
  E --> G["DOM updated"]
  F --> G

Lit's update cycle: batch, render, and touch only the expressions whose values changed.

The Lit package family

The lit package you install is a convenience bundle. Knowing what is inside helps you read documentation and error messages.

Package Role
lit The main entry: LitElement, html, css, decorators, and the built-in directives
lit-html The template engine. It works without custom elements, for example to render into any div
@lit/reactive-element The base class with reactive properties and the update lifecycle
@lit/context Passing data down a component tree without passing it through every level
@lit/task Handling asynchronous data such as fetch with loading and error states
@lit/localize Internationalization
@lit-labs/ssr Server-side rendering (labs packages may change)

You can render a Lit template without a component at all. That is handy for small widgets or for adding Lit to an existing page.

lit-html without a component

Calling render again with a new template result updates only what changed: existing <li> elements stay in place and Lit appends the new one.

How Lit compares with other options

  • Vanilla custom elements. No dependency, but you write all rendering and update logic yourself. Fine for tiny elements.
  • Stencil, FAST, Svelte (custom element mode). Other ways to produce web components. Stencil is a compiler, Lit is a runtime library. All of them output standard elements.
  • React, Vue, Angular. Application frameworks with their own component models. They can consume Lit components, and Lit components can live inside them.

Lit's trade-off is a small, focused scope. It does not include a router, a state-management system or an application structure. You pick those separately, which suits component libraries and design systems very well.

Lit 3 is the current major version used in this track (the examples load Lit 3.3.1 from a CDN). Lit 2 components mostly keep working in Lit 3, so older articles are still useful.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Lit component extend?

    LitElement extends HTMLElement and adds reactive properties plus the render cycle.
  2. 2. How does Lit update the DOM after a property changes?

    Lit remembers the location of each expression in the real DOM and updates only those that changed.
  3. 3. Which package renders templates without needing a custom element?

    lit-html is the template engine; its render function can target any DOM container.
  4. 4. Is a Lit component a standard custom element?

    Lit builds on the standards, so Lit components work anywhere ordinary custom elements work.

Summary

  • Lit is a small library for building web components with declarative templates, reactive properties and scoped styles.
  • Lit components are standard custom elements with shadow DOM, usable in any page or framework.
  • Lit has no virtual DOM: it updates only the expression locations whose values changed.
  • lit-html can render templates anywhere, even without a component.
  • Lit stays small on purpose; routing and app-wide state come from other tools.

Next: Your First Lit Component