Install Lit
Add Lit to your project using your package manager of choice.
Lit
3.x targets modern browsers and requires ES2019+ support. If you need older browser support, see the installation guide.Create your component
Create a file named A few things to note:
my-greeting.ts and define your first Lit component.my-greeting.ts
@customElement('my-greeting')registersmy-greetingas a custom element and is equivalent to callingcustomElements.define('my-greeting', MyGreeting).@property()declaresnameas a reactive property. Any change tonameautomatically schedules a re-render.static stylesuses thecsstagged template to define Shadow DOM-scoped styles.render()returns ahtmltagged template that Lit efficiently updates on each change.
Use the component in HTML
Import your component and use it like any HTML element.The
index.html
name attribute maps directly to the name property because Lit observes declared properties and reflects attribute changes automatically.Update properties reactively
Reactive properties trigger a re-render whenever they change — whether set from an attribute, via JavaScript, or from within the component itself.Clicking the button updates
my-greeting.ts
this.name, which schedules an efficient re-render — only the changed parts of the DOM are updated.Next steps
You now have a working Lit component with reactive properties and scoped styles. Explore the core concepts to go further:Components
Understand the LitElement lifecycle, shadow root, and component anatomy.
Templates
Learn how
html tagged templates work and how to bind expressions.Reactive properties
Explore the full
@property and @state options for fine-grained reactivity.Styles
Use
css tagged templates, CSS custom properties, and dynamic styles.