DEV Community

Arhan Khan
Arhan Khan

Posted on

Lit for Beginners: Build Web Components With Almost No Framework

Lit is a tiny library for building web components — reusable custom HTML elements that work in any framework, or in no framework at all. If you have ever wanted to build a <my-card> or <date-picker> element that you can drop into any page, Lit is one of the simplest ways to do it.

A web component is a custom element with its own encapsulated styles and behavior, powered by the browser's native Custom Elements and Shadow DOM APIs. Lit adds a thin layer on top: declarative templates, reactive properties, and efficient re-rendering. You write a class, Lit handles the rest.

Here is a complete Lit component:

import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';

@customElement('hello-card')
export class HelloCard extends LitElement {
  static styles = css`
    .card { padding: 16px; border: 1px solid #ddd; border-radius: 8px; }
  `;

  @property() name = 'World';

  render() {
    return html`<div class="card">Hello, ${this.name}!</div>`;
  }
}
Enter fullscreen mode Exit fullscreen mode

Use it anywhere with <hello-card name="Arhan"></hello-card>. The @property() decorator makes name reactive — change the attribute and the component re-renders automatically. The html template tag handles escaping and efficient updates, so you only re-render what changed.

To start, install Lit with npm install lit and import it in any project — Vite, plain HTML with an import map, or an existing app. There is no build step required for the basics, though a bundler helps for production.

Lit is a great fit if you build design systems, widgets embedded in other people's pages, or components that must survive framework changes. Because the output is a standard web component, it works in React, Vue, Angular, or plain HTML without wrappers.

The limits are honest ones. Lit gives you components, not an app framework — there is no built-in router or state store, so you bring your own for large apps. The decorator syntax needs a build step or a modern browser setup, and the ecosystem is smaller than the big frameworks.

Start small: convert one repeated piece of UI in your current project — a button, a card, a modal — into a Lit component. If you enjoy how portable it feels, you have found your use case.

Top comments (1)

Collapse
 
dannyengelman profile image
Danny Engelman •

Or without Lit in native Web Components:

customElements.define('hello-card', class extends HTMLElement {
  constructor() {
    const createElement = (tag, props = {}) => 
                             Object.assign(document.createElement(tag), props);
    super() // sets AND returns 'this' scope
      .attachShadow({ mode: 'open' }) // sets AND returns the shadow root
      .append(
        createElement('style', {
          textContent: `:host{padding:16px;border:1px solid #ddd;border-radius:8px}`
        }),
        "Hello, ",
        this.name = createElement('span')
      )
  }
  connectedCallback() {
    this.name.textContent = this.getAttribute('name') || 'World';
  }
});
Enter fullscreen mode Exit fullscreen mode