Need help with htm?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

7.2K Stars 148 Forks Apache License 2.0 398 Commits 31 Opened issues


Hyperscript Tagged Markup: JSX alternative using standard tagged templates, with compiler support.

Services available


Need anything else?

Contributors list

HTM (Hyperscript Tagged Markup) npm

hyperscript tagged markup demo

is JSX-like syntax in plain JavaScript - no transpiler necessary.

Develop with React/Preact directly in the browser, then compile

away for production.

It uses standard JavaScript Tagged Templates and works in all modern browsers.

by the numbers:

🐣 < 600 bytes when used directly in the browser

⚛️ < 500 bytes when used with Preact (thanks gzip 🌈)

🥚 < 450 byte


🏅 0 bytes if compiled using babel-plugin-htm

Syntax: like JSX but also lit

The syntax you write when using HTM is as close as possible to JSX:

  • Spread props:
    instead of
  • Self-closing tags:
  • Components:
     instead of 
    is a component reference)
  • Boolean attributes:
  • Improvements over JSX

    actually takes the JSX-style syntax a couple steps further!

    Here's some ergonomic features you get for free that aren't present in JSX:

    • No transpiler necessary
    • HTML's optional quotes:
    • Component end-tags:
      footer content/>
    • Syntax highlighting and language support via the lit-html VSCode extension and vim-jsx-pretty plugin.
    • Multiple root element (fragments):
    • Support for HTML-style comments:
    • Installation

      is published to npm, and accessible via the CDN:

      via npm:

      npm i htm

      hotlinking from unpkg: (no build tool needed!)

      import htm from ''
      const html = htm.bind(React.createElement);
      // just want htm + preact in a single file? there's a highly-optimized version of that:
      import { html, render } from ''


      If you're using Preact or React, we've included off-the-shelf bindings to make your life easier. They also have the added benefit of sharing a template cache across all modules.

      import { render } from 'preact';
      import { html } from 'htm/preact';
      render(html`Hello!`, document.body);

      Similarly, for React:

      import ReactDOM from 'react-dom';
      import { html } from 'htm/react';
      ReactDOM.render(html`Hello!`, document.body);

      Advanced Usage


      is a generic library, we need to tell it what to "compile" our templates to. You can bind
      to any function of the form
      h(type, props, ...children)
      (hyperscript). This function can return anything -
      never looks at the return value.

      Here's an example

      function that returns tree nodes:
      function h(type, props, ...children) {
        return { type, props, children };

      To use our custom

      function, we need to create our own
      tag function by binding
      to our
      import htm from 'htm';

      const html = htm.bind(h);

      Now we have an

      template tag that can be used to produce objects in the format we created above.

      Here's the whole thing for clarity:

      import htm from 'htm';

      function h(type, props, ...children) { return { type, props, children }; }

      const html = htm.bind(h);

      console.log( html<h1 id="hello">Hello world!</h1> ); // { // type: 'h1', // props: { id: 'hello' }, // children: ['Hello world!'] // }

      If the template has multiple element at the root level the output is an array of



      `); // [ // { // type: 'h1', // props: { id: 'hello' }, // children: ['Hello'] // }, // { // type: 'div', // props: { class: 'world' }, // children: ['world!'] // } // ]


      The default build of

      caches template strings, which means that it can return the same Javascript object at multiple points in the tree. If you don't want this behaviour, you have three options:
      • Change your
        function to copy nodes when needed.
      • Add the code
        this[0] = 3;
        at the beginning of your
        function, which disables caching of created elements.
      • Use
        , which disables caching by default.


      Curious to see what it all looks like? Here's a working app!

      It's a single HTML file, and there's no build or tooling. You can edit it with nano.

        htm Demo

      ⚡️ See live version

      ⚡️ Try this on CodeSandbox

      How nifty is that?

      Notice there's only one import - here we're using the prebuilt Preact integration since it's easier to import and a bit smaller.

      The same example works fine without the prebuilt version, just using two imports:

      import { h, Component, render } from 'preact';
      import htm from 'htm';

      const html = htm.bind(h);

      render(html``, document.body);

      Other Uses


      is designed to meet the same need as JSX, you can use it anywhere you'd use JSX.

      Generate HTML using vhtml:

      import htm from 'htm';
      import vhtml from 'vhtml';

      const html = htm.bind(vhtml);

      console.log( html<h1 id="hello">Hello world!</h1> ); // '

      Hello world!


      Webpack configuration via jsxobj: (details here) (never do this)

      import htm from 'htm';
      import jsxobj from 'jsxobj';

      const html = htm.bind(jsxobj);

      console.log(html <webpack watch mode="production"> <entry path="src/index.js"></entry> </webpack>); // { // watch: true, // mode: 'production', // entry: { // path: 'src/index.js' // } // }

      Demos & Examples

      Project Status

      The original goal for

      was to create a wrapper around Preact that felt natural for use untranspiled in the browser. I wanted to use Virtual DOM, but I wanted to eschew build tooling and use ES Modules directly.

      This meant giving up JSX, and the closest alternative was Tagged Templates. So, I wrote this library to patch up the differences between the two as much as possible. The technique turns out to be framework-agnostic, so it should work great with any library or renderer that works with JSX.

      is stable, fast, well-tested and ready for production use.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.